当我运行具有我要填充的列表视图的活动时,它显示为空。但是,在 logcat 中,我可以看到自定义 CursorAdapter 遍历我的数据并返回至少一些视图。
public class JobAdapter extends CursorAdapter {
private String status;
private static final String tag ="TESTING CursorAdapter";
public JobAdapter(Context context, Cursor cur, boolean autoRequery, String s) {
super(context, cur, autoRequery);
Log.d(tag, "Job adapter create");
status = s;
}
@Override
public void bindView(View row, Context context, Cursor cursor) {
Log.d(tag, "Starting a bind");
Cursor c = getCursor();
if(!c.isClosed()){
TextView companyName = (TextView) row.findViewById(R.id.listCompanyName);
TextView positionTitle = (TextView) row.findViewById(R.id.listPositionTitle);
while(!c.isLast() && !c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
Log.d(tag, "bind - moving c to next 1");
c.moveToNext();
}
if(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
Log.d(tag, "found a status and trying to populate the view");
companyName.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));
positionTitle.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_POSITION)));
Log.d(tag, "Company name = "+c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));
if(c.isLast()) {
Log.d(tag, "bind - Closing c 1");
c.close();
}else {
Log.d(tag, "bind - moving c to next 2");
c.moveToNext();
}
}
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
Log.d(tag, "Starting a new");
final LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.job_list_item, parent, false);
if(!c.isClosed()){
TextView companyName = (TextView) row.findViewById(R.id.listCompanyName);
TextView positionTitle = (TextView) row.findViewById(R.id.listPositionTitle);
while(!c.isLast() && !c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
Log.d(tag, "new - moving c to next 1");
c.moveToNext();
}
if(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
Log.d(tag, "new - found a status and trying to populate the view");
companyName.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));
positionTitle.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_POSITION)));
if(c.isLast()) {
Log.d(tag, "new - Closing c 1");
c.close();
}else {
Log.d(tag, "new - moving c to next 2");
c.moveToNext();
}
}
Log.d(tag, "new - returning row");
return row;
}
}
我是否应该检查游标是否已到达数据库末尾?或者这会为我处理吗?
我使用这个适配器的课程很简单
public class AppliedForActivity extends ListActivity {
private JobsDbAdapter mDbHelper;
public void onCreate(Bundle savedInstanceState) {
Log.d("TESTING C", "STARTING THE APPLIED FOR ACTIVITY");
super.onCreate(savedInstanceState);
setContentView(R.layout.job_list);
mDbHelper = new JobsDbAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.fetchAllJobs();
setListAdapter(new JobAdapter(this, c, false, "applied for"));
}
}
请原谅花括号,我可能复制错了。