Monday, 30 September 2013

simple coursor adapter with sqlite database and custmize listview

simple coursor adapter with sqlite database and custmize listview

i m confused here where is the actual error .i want to display the record
from sqlite db in to listview using simple coursor adapter .there is no
fild like _id in my sqlite databas !! but the error is regarding that,
which i mention below: Error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.blundell.tut/com.blundell.tut.ui.phone.update_page}:
java.lang.IllegalArgumentException: column '_id' does not exist
my java files are mention here..thanx in advance and if any suggestion
then plz comment it.
update_page.java
public class update_page extends Activity
{
DatabaseHandler db = new DatabaseHandler(this);
public String tableName = db.TABLE_CONTACTS;
public String databaseName = db.DATABASE_NAME;
public String task_name = db.KEY_TASK;
public String dt = db.KEY_DATETIME;
private ArrayList<String> results = new ArrayList<String>();
private ArrayList<String> results2 = new ArrayList<String>();
Intent op_intent;
SQLiteDatabase database;
ListView myview;
Cursor c;
String o_name,o_no1;
//String o_id1 = null;
HttpClient client;
Integer op_id,status;
String url;
Context ctx;
Intent myintent;
final MainActivity act = new MainActivity();
public void onCreate(Bundle savedInstanceState)
{
ctx=this;
database = act.getInstance().openOrCreateDatabase("contactsManager",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
Toast.makeText(getApplicationContext(), "Database is open",
1500).show();
final String[] columnsone={"id","task_name","date_time"};
String[] columnstwo={"task_name","date_time"};
int to[] = {R.id.lbl_task,R.id.lbl_datetime};
c = database.query("contacts", columnsone, null, null, null,
null, null);
status=c.getCount();
if(status==0)
{
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(ctx);
// set title
alertDialogBuilder.setTitle("No task are created !!");
// set dialog message
alertDialogBuilder
.setMessage("Click cancle to exit!")
.setCancelable(false)
.setNegativeButton("Cancle",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface
dialog,
int id) {
// if this button is clicked, just
close
// the dialog box and do nothing
dialog.cancel();
myintent = new
Intent(update_page.this,MainActivity.class);
startActivity(myintent);
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
else
{
Toast.makeText(getApplicationContext(), "records are
available in courser", 1500).show();
if(c!=null)
{
Toast.makeText(getApplicationContext(), "in courser",
1500).show();
SimpleCursorAdapter adapter=new
SimpleCursorAdapter(this.ctx,R.layout.update_listview,
c, columnstwo, to);
Toast.makeText(getApplicationContext(), "records are in
list", 1500).show();
myview.setAdapter(adapter);
}
}
update_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#404040"
android:fillViewport="true" > -->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:divider="#000000"
android:dividerHeight="2dp"
android:background="#404040">
</ListView>
<!-- </ScrollView> -->
</LinearLayout>
**update_listview.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/lbl_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textColor="#1E90FF"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/lbl_datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

No comments:

Post a Comment