1

我的应用程序有 SQLiteDataBase 表有两个字段

1.sender.... 2.message

messagedb=context.openOrCreateDatabase("message",0, null);
                    messagedb.execSQL("CREATE TABLE IF NOT EXISTS tab2(sender INT(13),body varchar)");
                    mydb.execSQL("INSERT INTO tab2 VALUES('"+sender+"','"+sb+"')");
//sb contains message

我如何从数据库中填充 ListView

http://a6.sphotos.ak.fbcdn.net/hphotos-ak-ash4/483993_349052115164919_754938581_n.jpg

图片空间包含联系人图片,上方的文本字段包含放置消息的发件人姓名(联系人姓名)......我也希望添加消息接收日期和时间......对此有任何想法吗?

4

1 回答 1

0

创建自定义 SimpleCursorAdapter。我认为您足够聪明,可以在数据库中查询所需的列。一旦您的 cusror 准备好,将其传递给自定义适配器。

下面的片段会帮助你。

cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);

contactList.setAdapter(new MyCursorAdapter(this, R.layout.row, cursor,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME,
                        ContactsContract.Contacts.LAST_TIME_CONTACTED },
                new int[] { R.id.name, R.id.email }));

MyCursorAdapter.java

public class MyCursorAdapter extends SimpleCursorAdapter {

    private Cursor cursor;
    private int layout;
    private Context context;

    public MyCursorAdapter(Context context, int layout, Cursor cursor,
            String[] from, int[] to) {
        super(context, layout, cursor, from, to);
        this.context = context;
        this.cursor = cursor;
        this.layout = layout;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        cursor.moveToPosition(position);  <---- This is very important.
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layout, null);
        TextView txtName = (TextView) view.findViewById(R.id.name);
        TextView txtEmail = (TextView) view.findViewById(R.id.email);

        int nameColumnIndex = cursor
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        int frequencyColumnIndex = cursor
                .getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED);

        txtName.setText(cursor.getString(nameColumnIndex));
        txtEmail.setText(cursor.getString(frequencyColumnIndex));
        return view;
    }
}
于 2012-07-01T13:14:37.707 回答