打印SQLite的条目成一个ListView [英] Printing SQLite entries into a ListView
问题描述
我有一些麻烦,学习SQLite的世界的来龙去脉。我有一些code就是让我把数据输入到数据库中。但我想做的事就是把这些信息返回到列表视图。目前,所有我能想出这样做是有举杯打印每行添加新条目之后。是否有人可以告诉我如何改变我的code打印它在ListView?或者,甚至看我的code和看到我在正确的方式去一下吧。谢谢
I am having some trouble learning the ins and outs of the SQLite world. I have some code that is allowing me to enter data into a DB. But what i want to do is return this data into a listview. At the moment all I could figure out to do was to have each row printed in a toast after a new entry is added. Can someone please show me how to alter my code to print it in a listview? Or to even look at my code and see that i am going about it in the right way. Thanks
这是code我使用它调用显示记录功能
This is the code i am using which calls a display record function
//---get all Records---
com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
这是显示记录功能
public void DisplayRecord(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Item: " + c.getString(1) + "\n" +
"Litres: " + c.getString(2),
Toast.LENGTH_SHORT).show();
}
我知道我需要改变第二个函数,但我不知道该怎么做,使之成为打印一个ListView
I know i need to change the second function but i dont know how to do that to make it print into a listview
推荐答案
这是从数据库中获取数据,并插入到ArrayList和插入arrayAdapter比列表视图中显示它的code。
this is the code of getting data from database and insert into Arraylist and insert into arrayAdapter and than display it in listview .
我只是做了一些编辑在现有的code。
i just done some editing in your existing code.
com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
db.open();
ArrayList<String> data_list=new ArrayList<String>();
ListView lv=(ListView)findViewById(R.id.listView1);
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do {
data_list.add(c.getString(0));
DisplayRecord(c);
} while (c.moveToNext());
}
ArrayAdapter<String> aa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data_list);
lv.setAdapter(aa);
LV - 是的ListView对象
lv - is the object of ListView.
这篇关于打印SQLite的条目成一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!