-1

我已经实现了一个应用程序来从本地移动 SQLite db 获取数据并尝试发送到 Custome Simple Cursor 适配器类。我正在从 BroadCast Reciever 接收数据它用于从 DB 获取最新记录。如果使用 SimpleCursorAdapter 那么我可以从 Sqlite DB 获取数据并更新到 UI。但我无法将 DB 内容传递给 CustomeSimpleCursorAdapter。那么如何将 db 内容传递给 CustomeSimpleCursorAdapter?

我已经实现了如下代码:

     private BroadcastReceiver myReceiver = new BroadcastReceiver() 
  {
        @Override
        public void onReceive(Context context, Intent intent) {
        msh = new MySqliteHelper(GetMsgsScreen.this);
        msh.openToWrite();
        lst = ((ListView)findViewById(R.id.listView1));
        cursor = msh.queueAll();
        getFromDB = new String[]{MySqliteHelper.USER_NAME,MySqliteHelper.USER_MESSAGE};
        toView = new int[]{R.id.usrName,R.id.msgText};
        cursor.moveToFirst(); 
          lst.setAdapter(new SimpleCursorAdapter(GetMsgsScreen.this, R.layout.test, cursor, getFromDB, toView));            
       updateList();

        }
    };

相似地,

如果我使用 CustomeSimpleCursorAdapter 而不是 SimpleCursorAdapter 那么如何显示 MySqliteHelper.USER_NAMEMySqliteHelper.USER_MESSAGE 内容以列出?请任何人帮助我。

4

1 回答 1

1

如果要创建 CursorAdapter 的自定义适配器子类,则必须重写bindViewnewView方法。

public class CustomCursorAdapter extends CursorAdapter {
    public CustomCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView username = (TextView)view.findViewById(R.id.username);
        username.setText(cursor.getString(
                cursor.getColumnIndex(MySqliteHelper.USER_NAME)));
        //  Set up other view here
        //  ...
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.listItem, parent, false);
        bindView(v, context, cursor);
        return v;
    }
}
于 2012-03-02T10:04:25.363 回答