2

SimpleCursorAdapter为 my扩展的自定义适配器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);
    }

适配器:

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

}
4

1 回答 1

6

我想我明白了。查看CursorAdapter.getView(). 注意bindView()和在 内是如何newView()被调用的getView()。看起来通过覆盖getView()您确保您CursorAdapter从不调用bindView(). 要么将所有内容都放入getView()或仅放入newView()and bindView(),但不能同时放入。


编辑我:

四件事:

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

  2. 创建时mAdapter,您应该将 null 传递给Cursor构造函数。将CursoronLoadFinishedLoader完成查询时)交付,并将使用 绑定到适配器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;
    }
    
  4. 确保您使用了正确的SimpleCursorAdapter构造函数……您当前使用的构造函数已被弃用!0(作为标志传递......CursorLoader将为您注册一个内容观察者,因此您无需传递任何特殊内容)。


编辑二:

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

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. */
    }
});
于 2012-01-17T22:46:08.667 回答