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