光标未与自定义适配器正确绑定文本

发布于 2024-12-27 21:24:44 字数 2425 浏览 1 评论 0原文

为我的 ListFragment 扩展 SimpleCursorAdapter 的自定义适配器无法正确显示数据库中的项目。它不显示 TextView 中的文本,我还需要对光标执行什么操作才能显示正确的文本?

我以为我必须覆盖 bindView 但这没有效果

设置适配器:

public void populateList(){

        String[] fields = new String[] {BowlersDB.NAME};
        Cursor c = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,
                new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");

mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,c,fields,new int[] {R.id.nameCheckTV});


        setListAdapter(mAdapter);   

        getLoaderManager().initLoader(0,null,this);
    }

适配器:

public class CheckAdapter extends SimpleCursorAdapter{

    Context context;
    Cursor c;

    public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.c = c;
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view,Context context,Cursor cursor){
        String st = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));

        TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
        tv.setText(st);
    }

    @Override
    public View getView(final int position,View convertView,ViewGroup parent){
        if(convertView == null){
            LayoutInflater inflator = getLayoutInflater();
            convertView = inflator.inflate(R.layout.check_listview,null);
        }

        CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);   
            cb.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    CheckBox check = (CheckBox)v.findViewById(R.id.checkBox1);
                    int pos = position;
                    if(check.isChecked()){
                        mCheckedItems.add(position);
                    }else if(!check.isChecked()){
                        for(int i=0;i< mCheckedItems.size();i++){
                            if(mCheckedItems.get(i) == position){
                                mCheckedItems.remove(i);
                            }
                        }
                    }
                }

            });
        return convertView;
    }

}

My Custom Adapter that extends SimpleCursorAdapter for my ListFragment does not display the items in my database correctly. it does not display the text in the TextView, what else do I have to do to the cursor for it to show the correct text?

I thought I had to override the bindView but that had no effect

Setting the adapter:

public void populateList(){

        String[] fields = new String[] {BowlersDB.NAME};
        Cursor c = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,
                new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");

mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,c,fields,new int[] {R.id.nameCheckTV});


        setListAdapter(mAdapter);   

        getLoaderManager().initLoader(0,null,this);
    }

Adapter:

public class CheckAdapter extends SimpleCursorAdapter{

    Context context;
    Cursor c;

    public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.c = c;
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view,Context context,Cursor cursor){
        String st = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));

        TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
        tv.setText(st);
    }

    @Override
    public View getView(final int position,View convertView,ViewGroup parent){
        if(convertView == null){
            LayoutInflater inflator = getLayoutInflater();
            convertView = inflator.inflate(R.layout.check_listview,null);
        }

        CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);   
            cb.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    CheckBox check = (CheckBox)v.findViewById(R.id.checkBox1);
                    int pos = position;
                    if(check.isChecked()){
                        mCheckedItems.add(position);
                    }else if(!check.isChecked()){
                        for(int i=0;i< mCheckedItems.size();i++){
                            if(mCheckedItems.get(i) == position){
                                mCheckedItems.remove(i);
                            }
                        }
                    }
                }

            });
        return convertView;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2025-01-03 21:24:44

我想我明白了。查看 CursorAdapter.getView()。请注意 getView() 中如何调用 bindView()newView()。看起来,通过覆盖 getView(),您可以确保您的 CursorAdapter 永远不会调用 bindView()。要么将所有内容仅放入 getView() 中,要么仅放入 newView()bindView() 中,但不能同时放入两者中。


编辑我:

四件事:

  1. 我已经阅读了 CursorAdapter ,看起来你想重写 bindView() getView() 的。看起来 getView() 对于要显示的每一行都被调用两次,这在某种程度上阻止了光标的更新,因此始终指向第一行。我认为这就是为什么您的 ListView 仅显示第一行。此外,ListView 的默认 CursorAdapter 实现将为您回收视图,因此重写 getView() 确实没有必要(查看此 回答了解更多信息)。

  2. 创建 mAdapter 时,您应该将 null Cursor 传递到构造函数中。 Cursor 将在 onLoadFinished 中传递(当 Loader 完成查询时),并将通过 swapCursor(光标)

  3. 您的 onCreateLoader() 方法将创建(或启动)执行初始查询的 Loader。确保它看起来像这样,

    <前><代码>@Override
    公共加载器onCreateLoader(int id, Bundle args) {
    String[] 投影 = { BowlersDB.ID, BowlersDB.NAME };

    CursorLoader 游标加载器 = new CursorLoader(getActivity(),
    BowlersDB.CONTENT_URI,投影,空,空,空);
    返回游标加载器;
    }

  4. 确保使用正确的 SimpleCursorAdapter 构造函数...您当前使用的构造函数已被弃用! 因此您无需传递任何特殊内容)。


编辑 II:

澄清一下,bindView() 方法是您使用 Cursor 当前位置的数据填充行的小部件的地方。您将被预先定位在正确的行上的光标。您需要从 Cursor 中提取数据并将其倒入您所需的小部件中。换句话说,您需要查询行的 id(而不是使用 getView() 中的“position”参数)。尝试在 bindView() 中设置 onClickListener ,如下所示:

final int rowId = cursor.getInt(cursor.getColumnIndex("_id"));

CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);   
cb.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = rowId;
        /* you now know this row's position. do what you need to do. */
    }
});

I think I got it. Check out the implementation for CursorAdapter.getView(). Notice how bindView() and newView() are called within getView(). It looks like by overriding getView() you are ensuring that your CursorAdapter never calls bindView(). Either put everything ONLY in getView() or ONLY in newView() and bindView(), but not both.


EDIT I:

Four things:

  1. I've read up on CursorAdapters and it looks like you want to override bindView() instead of getView(). It looks like getView() is called twice for each row that's to be displayed and this is somehow preventing the cursor from being updated and thus always points to the first row. I think this is why your ListView only displays the first row. Further, the default CursorAdapter implementation for a ListView will recycle views for you, so overriding getView() really isn't necessary (check out this answer for more info).

  2. When creating your mAdapter, you should pass a null Cursor into the constructor. The Cursor will be delivered in onLoadFinished (when the Loader has finished the query) and will be bound to the adapter with swapCursor(cursor).

  3. Your onCreateLoader() method will create (or start) the Loader that will perform the initial query. Make sure it looks something like this,

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = { BowlersDB.ID, BowlersDB.NAME };
    
        CursorLoader cursorLoader = new CursorLoader(getActivity(),
                BowlersDB.CONTENT_URI, projection, null, null, null);
        return cursorLoader;
    }
    
  4. Make sure you make use of the correct SimpleCursorAdapter constructor... the one you are currently using is deprecated! (pass 0as a flag... the CursorLoader will register a content observer for you so you don't need to pass in anything special).


EDIT II:

To clarify, the bindView() method is where you populate your row's widgets with the data from the Cursor's current position. You are handed the Cursor pre-positioned at the proper row. You need to pull data out of the Cursor and pour it into your desired widgets. In other words, you'll need to query for the row's id (instead of using the "position" argument in getView()). Try setting the onClickListener in bindView() with something like:

final int rowId = cursor.getInt(cursor.getColumnIndex("_id"));

CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);   
cb.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = rowId;
        /* you now know this row's position. do what you need to do. */
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文