数据没有显示在android中的SQLite列表视图中

时间:2015-03-29 12:54:47

标签: java android sqlite listview

我已经成功创建了一个数据库并将数据插入其中。但是当检索数据并将其显示在列表视图中时,数据没有显示。我不确定我的问题在哪里。我已经研究过S / O和其他教程,但似乎无法找到适合我的代码。以下是我尝试过的:

DBHelper.java 扩展 SQLiteOpenHelper

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TITLE = "title";
public static final String CONTACTS_COLUMN_AMOUNT = "amount";
public static final String CONTACTS_COLUMN_DESC = "description";

private HashMap hp;

public DBHelper(Context context)
{
    super(context, DATABASE_NAME , null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table contacts " +
                    "(id integer primary key, title text,amount text,description text)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS contacts");
    onCreate(db);
}

public boolean insertContact  (String title, String amount, String description)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put("title", title);
    contentValues.put("amount", amount);
    contentValues.put("description",description);


    db.insert("contacts", null, contentValues);
    return true;
}
public Cursor getData(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
    return res;
}
public int numberOfRows(){
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
    return numRows;
}
public boolean updateContact (Integer id, String title, String amount, String description)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("title", title);
    contentValues.put("amount", amount);
    contentValues.put("description",description);

    db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
    return true;
}

public Integer deleteContact (Integer id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("contacts",
            "id = ? ",
            new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllContacts()
{
    ArrayList<String> array_list = new ArrayList<>();
    hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from contacts", null );
    res.moveToFirst();
    while(res.isAfterLast() == false){
        array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
        array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
        array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
        System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
        System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
        System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
        System.out.print("List holds:" + array_list);
        res.moveToNext();
    }
    return array_list;
}
}

Details.java 中,我在按钮上插入数据,点击这样:

public void run(View view) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            int Value = extras.getInt("id");
            if (Value > 0) {
                if (mydb.updateContact(id_To_Update, title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), MyBasket.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(), "Not Updated", Toast.LENGTH_SHORT).show();
                }
            } else {
                if (mydb.insertContact(title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
                    showAlertDialog();
                } else {
                    Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
                }

            }
        }
    }

MyBasket.java 是我显示以这种方式添加的数据的列表视图的地方: 在我的OnCreate()

mydb = new DBHelper(this);

        ArrayList array_list = mydb.getAllContacts();

        ArrayAdapter arrayAdapter =
                new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

        //adding it to the list view.
        obj = (ListView)findViewById(R.id.listView1);
        obj.setAdapter(arrayAdapter);

仍然无法显示。我不知道我哪里出错了。任何帮助或建议都会得到很多赞赏。

我的Logcat:

03-29 19:35:54.849    3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteLog﹕ (1) table contacts has no column named amount
03-29 19:35:54.949    3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteDatabase﹕ Error inserting amount=Ksh.8.0 title=District 9 description=Action, Sci-Fi, Thriller
    android.database.sqlite.SQLiteException: table contacts has no column named amount (code 1): , while compiling: INSERT INTO contacts(amount,title,description) VALUES (?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
            at com.snappy.stevekamau.cosmeticsapp.DBHelper.insertContact(DBHelper.java:60)
            at com.snappy.stevekamau.cosmeticsapp.Details.run(Details.java:82)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3602)
            at android.view.View.performClick(View.java:4100)
            at android.view.View$PerformClick.run(View.java:17016)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4838)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

如果在

处放置断点,array_list的值是多少
 ArrayAdapter arrayAdapter =
                new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

答案 1 :(得分:0)

array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));       
System.out.Println("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
System.out.Println("List holds:" + array_list);
res.moveToNext(); 

将它放在getallcontacts函数的代码中,看看输出是否显示实际正确填充的arraylist。