【问题标题】:Android - Displaying all SQLite database entries in a ListViewAndroid - 在 ListView 中显示所有 SQLite 数据库条目
【发布时间】:2012-08-29 01:11:40
【问题描述】:

我想要做的是在 ListView 中显示数据库的内容。该布局包含一个我已经使用和实现的 ListView,但我似乎无法让它们一起工作(甚至是光标),有人可以帮我解释一下为什么我的光标实现不起作用吗?

我有一种方法可以将数据库条目作为光标返回:

public Cursor getAllRecords() {
     return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TEXT}, null, null, null, null, null);
 }

我有一个要插入和显示数据库条目的类:

Button add;
EditText tM;
ListView generalList;
DBAdapter dba = new DBAdapter(this);

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general);

        add = (Button) findViewById(R.id.button1);
        tM = (EditText) findViewById(R.id.editText1);
        generalList = (ListView) findViewById(R.id.generalList);

        Cursor c = dba.getAllRecords();
        c.moveToFirst();

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.id.generalList, c, new String[] {dba.KEY_TEXT}, new int[] {R.id.generalList}, 0);
            // This doesn't seem to work for me, I don't know how to fix it
            // or how to then get it working with the ListView

        generalList.setAdapter(adapter);

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                insertIntoDatabase();
            }
        });
    }

 public void insertIntoDatabase() {
     dba.open();
     dba.insertRecord(textMessage.getText().toString());
     Toast.makeText(this, "Added:\n" + tM.getText().toString(), Toast.LENGTH_LONG).show();
     dba.close();
 }

【问题讨论】:

    标签: java android database sqlite simplecursoradapter


    【解决方案1】:

    当使用带有 CursorAdapter 的 ListView 时,从该列返回的 Cursor 必须包含一个名称为 _id 的列,以唯一标识每一行。我猜您要获取的一列(dba.KEY_TEXT)未命名为“_id”。您可以在表中添加一个名为 _id 的列,或者在执行 SELECT 时让数据库将名称返回为 _id

    SELECT col_name as _id FROM my_table;
    

    SELECT col_name _id FROM my_table;
    

    【讨论】:

      【解决方案2】:

      如果列表仅包含 1 个TextView,您可以使用android.R.layout.simple_list_item_1 而不是您的R.id.generalList

      并将new int[] {R.id.generalList} 更改为new int[] {android.R.id.text1}

      喜欢:

      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[] {dba.KEY_TEXT}, new int[] {android.R.id.text1}, 0);
      

      【讨论】:

      • 使用Cursor的类中出现空指针异常。
      • 只在insertIntoDatabase()方法中打开。是否也需要为光标打开?
      • 哦,是的,我想我把android.R.layout.simple_list_item1 打错了android.R.id.simple_list_item1,改一下
      • 是的,您必须打开数据库才能获取光标及其内容
      • 更改了这两件事,但仍然出现错误! PS:为游标打开数据库后应该在什么时候关闭数据库?
      【解决方案3】:

      在接收 simplecursoraapter 的参数上,记住它接收的布局是实际项目的布局而不是列表视图。所以从那里删除 R.id.generalList ,因为那是标识列表视图的 id 而不是完整布局。所以用包含文本视图的布局替换它。现在,在 int 数组上是 textview 的 id,它将显示您想要的文本,在字符串数组上传递从数据库读取的记录中的字段名称。按照 aprian 所说的去做,并且知道您可以根据需要自定义项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多