带有SQLite数据库的简单游标适配器和自定义列表视图

时间:2013-09-30 11:01:42

标签: android sqlite listview simplecursoradapter

我在这里混淆了实际错误的位置。我想使用简单的coursor适配器将SQLite数据库中的记录显示到listview中。在我的SQLite数据库中没有类似_id的字段!但错误与此有关,我在下面提到:

错误:

  

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

这里提到了我的Java文件......

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>

1 个答案:

答案 0 :(得分:1)

您需要一个名为_id的列。在您的代码中,将您的id列更改为_id。有关类似帖子,请参阅About "_id" field in Android SQLite。另请参阅Android文档中的“data design considerations