0

当我运行具有我要填充的列表视图的活动时,它显示为空。但是,在 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"));
}

}

请原谅花括号,我可能复制错了。

4

1 回答 1

1

CursorAdapter 的想法是从你的代码中封装所有的定位,只留下可视化给你。那就是你不需要使用 moveToNext 和其他定位方法。

正如我在您的代码中看到的那样,您正在尝试实现一些过滤。但是使用数据库的想法是在 SQL 查询中制定您的标准。而不是查询所有作业,您应该只选择那些应该在 ListView 中显示的作业。

bindView 和 newView 应该只适用于光标指向的当前记录,并且不应该调用 moveToNext 或关闭。此外,CursorAdapter 假设您的光标有一个名为“_id”的整数字段。

另请参见此处此处

于 2011-10-29T19:24:00.637 回答