将SQLite数据库中的数据检索到listView中

时间:2013-10-27 19:54:44

标签: android sqlite

而不是重新检索数据,它返回每个列表中的包名称,我没有找到我的错误.Plz帮助我。这是我的DbHelper类,它扩展了SQLiteOpenHelper

 public List<DICTIONARY> getAllcontacts() {
            List<DICTIONARY> meaningList = new ArrayList<DICTIONARY>();

            String selectQuery = "SELECT  * FROM " + Table_Name;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);


            if (cursor.moveToFirst()) {
                do {
                    DICTIONARY dictionary = new DICTIONARY();
                    dictionary.setId(Integer.parseInt(cursor.getString(0)));
                    dictionary.setWord(cursor.getString(1));
                    dictionary.setMeaning(cursor.getString(2));
                    // Adding contact to list
                    meaningList.add(dictionary);
                } while (cursor.moveToNext());
            }

            // return meaning list
            return meaningList;
        }

这是我的MainActivity类

 DbHelper db = new DbHelper(MainActivity.this);
  List<DICTIONARY> dictionary = db.getAllContacts(); 
        ArrayAdapter<DICTIONARY> arrayAdapter = new ArrayAdapter<DICTIONARY>(MainActivity.this,android.R.layout.simple_list_item_1, dictionary);
        lv.setAdapter(arrayAdapter);
        }

1 个答案:

答案 0 :(得分:0)

更改

// Adding contact to list
                contactList.add(dictionary);

                meaningList.add(dictionary);

应该是

if (cursor.moveToFirst()) {
                do {
                    DICTIONARY dictionary = new DICTIONARY();
                    dictionary.setId(Integer.parseInt(cursor.getString(0)));
                    dictionary.setWord(cursor.getString(1));
                    dictionary.setMeaning(cursor.getString(2));
                    // Adding contact to Meaning List
                    meaningList.add(dictionary);//Changed Here
                } while (cursor.moveToNext());
            }