问题
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;
}
}
回答1:
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:
I've read up on
CursorAdapter
s and it looks like you want to overridebindView()
instead ofgetView()
. It looks likegetView()
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 yourListView
only displays the first row. Further, the defaultCursorAdapter
implementation for a ListView will recycle views for you, so overridinggetView()
really isn't necessary (check out this answer for more info).When creating your
mAdapter
, you should pass a nullCursor
into the constructor. TheCursor
will be delivered inonLoadFinished
(when theLoader
has finished the query) and will be bound to the adapter withswapCursor(cursor)
.Your
onCreateLoader()
method will create (or start) theLoader
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; }
Make sure you make use of the correct
SimpleCursorAdapter
constructor... the one you are currently using is deprecated! (pass0
as a flag... theCursorLoader
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. */
}
});
来源:https://stackoverflow.com/questions/8902141/cursor-not-binding-text-correctly-with-custom-adapter