我想以这种方式用游标中的数据填充列表:
myCursor = myDBA.getAllCompanies();
startManagingCursor(myCursor);
myListAdapter = new SimpleCursorAdapter(this, R.layout.company_row, myCursor, FROM, TO);
myListAdapter.setViewBinder(VIEW_BINDER);
companiesListView.setAdapter(myListAdapter);
我使用自定义在每行ViewBinder
中填充两个TextViews
和一个:ImageView
private final ViewBinder VIEW_BINDER = new ViewBinder() {
/**
* Binds the Cursor column defined by the specified index to the
* specified view.
*/
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch (viewId) {
case R.id.company_name:
case R.id.company_desc:
Log.d(TAG, "Binding TextView");
TextView venueText = (TextView) view;
venueText.setText(cursor.getString(columnIndex));
return true;
case R.id.company_icon:
Log.d(TAG, "Binding ImageView");
ImageView venueIcon = (ImageView) view;
String iconName;
iconName = cursor.getString(columnIndex); //Here I get the column name not the field value
Resources resources = myContext.getResources();
Drawable drawable = resources.getDrawable(resources
.getIdentifier("my.package:drawable/"
+ iconName, null, null));
venueIcon.setImageDrawable(drawable);
return true;
}
return false;
}
};
我的问题是cursor.getString()
方法调用返回的是列名而不是字段的值,所以我得到了数百行带有列名的行。
更新:
我getAllCompanie
的 s 包括一个moveToFirst
电话,Cursor
但我仍然得到列名:
public Cursor getAllCompanies(){
Cursor s = myDB.query(TABLE, KEYS, WHERE, null, null, null, null);
s.moveToFirst();
return s;
}