如何在不按任何按钮的情况下将所有联系人显示到布局

时间:2015-10-02 11:50:07

标签: android sqlite listview

我是android的新手,尤其是sqlite,所以请耐心等待。

我想问一下,如何从android DB中检索所有数据并将其显示到android布局而不按任何按钮?

以下是我在数据库类中的代码:

 // Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    String selectQuery = "SELECT  * FROM " + TABLE_CS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setId(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setTime(Integer.parseInt(cursor.getString(2)));
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    return contactList;
}

但我不确定如何将此列表视图添加到我的MainActivity中,我所做的是这样的:

listView = (ListView) findViewById(R.id.list_data);


    new Handler().post(new Runnable() {
        @Override
        public void run() {
            customAdapter = new CustomCursor(MainActivity.this, DB.getAllContacts());
            listView.setAdapter(customAdapter);
        }
    });

当然,这会产生错误,因为我从静态上下文中调用非静态方法。 我该如何从MainActivity调用GetAllContacts?

修改

这是我的CustomCursorAdapter

public class CustomCursor extends CursorAdapter {

public CustomCursor(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // when the view will be created for first time,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.single_row_item, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // here we are setting our data
    // that means, take the data from the cursor and put it in views

    TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv_person_pin);
    textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
}

2 个答案:

答案 0 :(得分:0)

我不知道为什么使用Handler。在MainActivity中你只需要这样做:

listView = (ListView) findViewById(R.id.list_data);
customAdapter = new CustomCursor(MainActivity.this, DB.getAllContacts());
listView.setAdapter(customAdapter);

getAllContacts()必须定义为 static ,如果您想直接从DB访问它,否则您可以创建DB的实例

答案 1 :(得分:-1)

你应该这样做:

customAdapter = new CustomCursor(MainActivity.this, mList);
listView.setAdapter(customAdapter);

然后

DB

还要确保public class ListAdapter extends BaseAdapter { private ArrayList<Contact> list = new ArrayList<>(); private Context context; public ListAdapter(Context context, ArrayList<Contact> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } private static class Holder { ImageView img; // whatever you have here TextView tv; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = new Holder(); if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.item_layout_here, parent, false); holder.img = (ImageView) convertView.findViewById(R.id.someid); holder.tv = (TextView) convertView.findViewById(R.id.yourid); convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } //post your code here whatever you want to show return convertView; } 是一个DAO对象,如here

所示

编辑:

以下是基本适配器的示例:

{{1}}