-3
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. 数据在那里。但我不知道如何调用它并将数据放在列表视图中。

List<Contact> contacts = db.getAllContacts();在我的 java 程序中使用,然后我坚持了这个例子,我只展示了如何查看日志中的数据。

我应该使用什么方法从数据库中调用数据并在列表视图中查看?

4

1 回答 1

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 并初始化它

希望这可以帮助你

于 2012-10-31T11:50:19.690 回答