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
调用数据的方法。数据在那里。但我不知道如何调用它并将数据放在列表视图中。
我在我的java程序中使用List<Contact> contacts = db.getAllContacts();
,然后我坚持了我只展示如何在日志中查看数据的示例。
我应该使用什么方法从数据库中调用数据并在列表视图中查看它?
答案 0 :(得分:0)
如上所述,您必须使用我自己创建的适配器并将其添加到ListView。这大概就是你要做的事情,代码有点粗糙,因为它在这里直接进入文本编辑器:
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并初始化
希望这有助于你