简体   繁体   English

Android-在ListView中显示所有SQLite数据库条目

[英]Android - Displaying all SQLite database entries in a ListView

What I am trying to do is display the contents of the database in a ListView. 我想做的是在ListView中显示数据库的内容。 The layout contains a ListView which I have used and implemented but I can't seem to get them working together (or even the cursor), could someone give me a hand and an explanation of why my cursor implementation doesn't work? 布局包含一个我已使用和实现的ListView,但是我似乎无法使它们一起工作(甚至没有光标),有人可以帮我一下,并解释为什么我的光标实现不起作用吗?

I have a method to return the database entries as a Cursor: 我有一种方法可以将数据库条目作为游标返回:

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

I have the class where I want to insert and display the database entries: 我有要在其中插入和显示数据库条目的类:

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();
 }

if the list contains only 1 TextView , you can use android.R.layout.simple_list_item_1 instead of your R.id.generalList . 如果列表仅包含1个TextView ,则可以使用android.R.layout.simple_list_item_1代替R.id.generalList

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

like : 喜欢 :

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);

on the arguments that receives the simplecursoradapter, remember the layout it receives is the layout for the actual item not the listview. 在接收simplecursoradapter的参数上,请记住它接收的布局是实际项目的布局,而不是listview。 so remove the R.id.generalList from there, cause that is the id that identified the listview not a full layout. 因此请从此处删除R.id.generalList,原因是标识列表视图的ID并非完整布局。 so replace that with a layout that contains a textview. 因此,将其替换为包含textview的布局。 now, on the int array goes the id of the textview that will show the text you want and on the string array pass on the names of the fields in the record read from the database. 现在,在int数组上将显示将显示所需文本的textview的ID,并在字符串数组上传递从数据库读取的记录中的字段名称。 do as mentioned by aprian and know that you can customize items as much as you need. 按照aprian所述进行操作,并且知道您可以根据需要自定义项。

When using a ListView with a CursorAdapter, the Cursor returned from the column must contain a column with the name _id uniquely identifying each row. 当将ListView与CursorAdapter一起使用时,从该列返回的Cursor必须包含名称为_id的列,该列唯一地标识每一行。 I'm guessing that the one column you are fetching (dba.KEY_TEXT) is not named "_id". 我猜您正在获取的一列(dba.KEY_TEXT)没有命名为“ _id”。 You can either add a column to your table named _id or when you perform your SELECT have the database return the name as _id 您可以在表中添加名为_id的列,或者在执行SELECT时让数据库将名称返回为_id

ie

SELECT col_name as _id FROM my_table;

OR 要么

SELECT col_name _id FROM my_table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM