【问题标题】:Add selected listView and editText as a row to SQlite db将选定的 listView 和 editText 作为一行添加到 SQlite db
【发布时间】:2014-03-17 17:37:37
【问题描述】:

我开始了一个新的活动,它应该向数据库添加行(两个字段:ListView 项目标题和来自 editText 的条目)。在 mainActivity 上检索行时,它只显示一列,这意味着只添加了一个字段。 如何将单击的 listView 项目存储到 db? 将editText值添加到db有什么问题?

这里是 addRow 代码:

Intent i = this.getIntent();
dataView.setText(i.getCharSequenceExtra("new item"));
mydManager = new DatabaseManager(this);

    okButton.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent i = ListItemActivity.this.getIntent();        
            mydManager.addRow("Meat",dataView.getText().toString());//replace "meat" with selected listView item
            mydManager.close();
            ListItemActivity.this.setResult(RESULT_OK, i);
            finish();
        }
    });

这是来自 DatabaseManager 类的 addRow 方法:

public boolean addRow(String g, String sub){
    ContentValues newEntry = new ContentValues();
    newEntry.put("Grocery", g); 
    newEntry.put("Sub_grocery", sub);         
    try{db.insertOrThrow(DB_TABLE, null, newEntry);}
    catch(Exception e) {
        Log.e("Error in inserting rows ", e.toString());
        e.printStackTrace();
        return false;           
    }
    db.close();
    return true;
}

这是从 databaseManager 类中检索行的方法:

public ArrayList<String> retrieveRows(String listViewItem){//selected listView as argument "ex: Meat"
    ArrayList<String> productRows=new ArrayList<String>();
    String[] columns = new String[]{"Grocery", "Sub_grocery"};
    String[] whereArgs = new String[]{listViewItem};
    Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {
        productRows.add(cursor.getString(0) + ", "+cursor.getString(1)+", ");
        cursor.moveToNext();
    }  
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return productRows;
}

这里是如何创建表的:

private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (Grocery TEXT, Sub_grocery TEXT);";
@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

【问题讨论】:

    标签: android sqlite listview android-listview


    【解决方案1】:

    尝试像这样检索它,然后检查你的日志猫。

               String selectQuery = "SELECT * FROM " + DB_TABLE;
           SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
    
               if (cursor.moveToFirst())
        {
            do
            {   
                              Log.d("database",cursor.getString(0));
                              Log.d("database",cursor.getString(1));
                         }
                  while (cursor.moveToNext());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多