问题描述
- 如何在常规字符串数组中打印 sqlite 数据?
-
我写下面的代码来填充数据库,现在我想在一个数组中复制数据,然后在 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); }
解决方案
if(allrows !=null){
String[] values = new String[allrows.getCount()];
int id = 0;
while(allrows.hasNext()){
values[id++] = allrows.getString(0); //第一个字段
}
}
解决方案二:
解决方案:
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);
时间: 2024-10-29 05:58:11