从SQLite数据库的列表视图中检索数据
问题描述:
我是Android的初学者,我现在不知道如何从方法DBHelper类填充listview具有相同的方法处理静态查询,我需要从editext进行查询插入值dinnamyc请求插入值。从SQLite数据库的列表视图中检索数据
public void SrchDB(View v) {
ListView p = (ListView) mDBHelper.findProduct(EdtxPoliza.getText().toString());
if (p == null)
{
Toast.makeText(this, "No Match Found!",Toast.LENGTH_LONG).show();
return;
}
}
答
首先
修改您mDBHelper.findProduct(String);
返回Collection
,说ArrayList<String>
public ArrayList<String> findProduct(String vString){
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> myRes = new ArrayList<>();
Cursor cursor = db.rawQuery("YOUR_QUERY", null);
while (cursor.moveToNext()){
myRes.add(cursor.getString(0)); //change the index to your column index in table.
}
return myRes;
}
二
创建适配器
ListAdapter myListAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ResultOf_findProduct);
三
一套适配器yourListView
yourListView.setAdapter(myListAdapter);
就是它,希望它会帮助你。
我高度怀疑你希望'findProduct'返回一个ListView对象。也许你是为了Arraylist?在任何情况下,您都不应该查看CursorAdapter类的文档。 –