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

时间:2012-01-17 21:44:24

标签: android android-listview android-cursoradapter android-loadermanager android-cursor

我的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);
    }

适配器:

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

}

1 个答案:

答案 0 :(得分:6)

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


编辑I:

四件事:

  1. 我已经阅读了CursorAdapter,您似乎想要覆盖bindView()而不是getView()。对于要显示的每一行,看起来getView()被调用两次,这会以某种方式阻止光标被更新,因此始终指向第一行。我想这就是为什么你的ListView只显示第一行的原因。此外,ListView的默认CursorAdapter实现将为您回收视图,因此无需覆盖getView()(请查看此answer以获取更多信息)。

  2. 创建mAdapter时,应将null Cursor传递给构造函数。 Cursor将在onLoadFinished中传递(当Loader完成查询时)swapCursor(cursor)将与onCreateLoader()绑定到适配器。

  3. 您的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; } 。确保它看起来像这样,

    SimpleCursorAdapter
  4. 确保使用正确的0构造函数...您当前使用的构造函数已弃用! (将CursorLoader作为标记传递... bindView()将为您注册内容观察者,以便您不需要传递任何特殊内容。


  5. 编辑II:

    为了澄清,Cursor方法是使用Cursor当前位置的数据填充行的小部件的位置。您将Cursor预先放置在正确的行中。您需要从getView()中提取数据并将其倒入所需的小部件中。换句话说,您需要查询行的ID(而不是使用onClickListener中的&#34; position&#34;参数)。尝试使用以下内容设置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. */ } });

    {{1}}