gpt4 book ai didi

java - 自定义 CursorAdapter 未填充 ListView

转载 作者:行者123 更新时间:2023-12-01 15:41:40 25 4
gpt4 key购买 nike

当我运行包含我尝试填充的 ListView 的 Activity 时,它显示为空。但是,在 logcat 中,我可以看到自定义 CursorAdapter 运行我的数据并至少返回一些 View 。

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

}

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

最佳答案

CursorAdapter 的想法是封装代码中的所有定位,只留给您可视化。也就是说你不需要使用moveToNext等定位方法。

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

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

另请参阅herethere .

关于java - 自定义 CursorAdapter 未填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com