2

大家好,我遇到了这个问题,我在 DB 中太弱了我写了这段代码来填充数据库,现在我想复制数组中的数据以在 arrayadapter 中打印出来,因为我使用 tabactivity,这是我找到的最好的解决方案

public void insertIntoTable(){
        try{
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
            mydb.execSQL("INSERT INTO " + TABLE + "(ID, NAME) VALUES("+Phno.getText()+",'"+PName.getText()+"')"); 

            mydb.close();
        }catch(Exception e){
            Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG);
        }
    }

这就是我想打印出来的方式

 mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
    String[] values = new String[] { "Iwant to put the cursor data here" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }
4

1 回答 1

3

我希望这对你有用

ArrayList<String> values = new ArrayList<String>();
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
allrows.moveFirst()
while(allrows.moveToNext()){ 
      String name= allrows.getString(allrows.getColumnIndex("NAME"));
      values.add(name); 
}
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
于 2012-05-17T09:44:51.853 回答