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

tyc*_*czj 2 android android-listview android-cursor android-loadermanager android-cursoradapter

SimpleCursorAdapter为我扩展的自定义适配器ListFragment无法正确显示数据库中的项目.它不会在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);
    }
Run Code Online (Sandbox Code Playgroud)

适配器:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*ood 6

我想我明白了.查看实施情况CursorAdapter.getView().注意如何bindView()newView()被称为内getView().通过压倒getView()你看起来确保你CursorAdapter永远不会打电话bindView().要么只把所有东西放在getView()或只放入newView()bindView(),而不是两者.


编辑I:

四件事:

  1. 我已经阅读了CursorAdapters,看起来你想覆盖bindView()而不是getView().getView()对于要显示的每一行,它看起来像是两次被调用,这会以某种方式阻止光标被更新,因此总是指向第一行.我想这就是为什么你ListView只显示第一行的原因.此外,CursorAdapterListView 的默认实现将为您回收视图,因此getView()实际上不需要覆盖(有关详细信息,请查看此答案).

  2. 创建时mAdapter,应该将null传递给Cursor构造函数.该Cursor会在交付onLoadFinished时(Loader已完成查询),并会被绑定到适配器swapCursor(cursor).

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

    @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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 确保您使用正确的SimpleCursorAdapter构造函数...您当前使用的构造函数已被弃用!(0作为旗帜传递...... CursorLoader将为您注册内容观察者,因此您不需要传递任何特殊内容).


编辑二:

为了澄清,该bindView()方法是使用Cursor当前位置的数据填充行的小部件.你被Cursor预先安置在适当的行.您需要从中提取数据Cursor并将其倒入所需的小部件中.换句话说,您需要查询行的id(而不是使用"position"参数getView()).尝试设置onClickListenerbindView()喜欢的东西:

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. */
    }
});
Run Code Online (Sandbox Code Playgroud)