从数据库列表视图Android设备上查看数据 [英] android view data from database to list view
问题描述
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setPerkara(cursor.getString(1));
contact.setTarikhKejadian(cursor.getString(2));
contact.setMasaMula(cursor.getString(3));
contact.setMasaAkhir(cursor.getString(4));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
这是我使用的调用从的SQLite
的数据是什么。该数据是there.But我不知道如何调用它,并把数据列表视图。
This is what I am using to call data from SQLite
. The data is there.But I don't know how to call it and put the data in the list view.
我使用列表&LT;联系与GT;接触= db.getAllContacts();
在我的Java程序,然后我坚持的例子,我只是展示如何看到日志中的数据
I am using List<Contact> contacts = db.getAllContacts();
in my java program and then I stuck the example I have only show how to see the data in log.
我应该用什么方法从数据库中调用数据,并查看它在列表视图?
what method should I use to call the data from database and view it in list view?
推荐答案
正如上面所说的,你必须使用一个适配器我想创建自己的,这添加到您的ListView。这大致是你会怎么做呢,code有点粗糙类型它straigh到文本编辑器中的位置:
As said above you have to use an Adapter I would create your own and add this to you ListView. This is roughly how you would do it, code bit rough as type it straigh into text editor here:
public class ContactListAdapter extends ListAdapter<String> {
private final List contacts;
//Call to super to set up the adapter
public ContactListAdapter(Context context, List<Contact> contacts) {
super(context, R.layout.rowlayout, values);
this.contacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get your View
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
//create the emelements you want to add to list
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(contacts.get(position).getId())
return rowView
}
}
那么这个适配器添加到您的ListView和初始化
Then add this adapter to your ListView and Initialize it
希望这有助于你
这篇关于从数据库列表视图Android设备上查看数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!