我有一个getCount返回50的游标。由于需要在ListView中显示内容,我使用了一个光标适配器,如下所示。出于某种奇怪的原因,列表总是包含一个元素。在日志记录中,我看到cursor.getPosition()
始终为0。为什么会这样呢?我使用CursorAdapters已经有一段时间了,以前从来没有遇到过这个问题。所以我很困惑,看不出我做错了什么。
Cursor cursor = getContext().getContentResolver().query(uri, PROJECTION,null,null,null);
MyLog.d(TAG,"cursor count is %s",cursor.getCount());
cursor.moveToFirst();
CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,true){
@Override
public View newView(Context context,Cursor cursor,ViewGroup parent){
MyLog.d(TAG,"newView cursor count %s position %s",cursor.getCount(),cursor.getPosition());
return LayoutInflater.from(context).inflate(R.layout.my_row,parent,false);
}
@Override
public void bindView(View view,Context context,Cursor cursor){
MyLog.d(TAG,"bindView at index %s",cursor.getPosition());
TextView name = (TextView) view.findViewById(R.id.name);
TextView address = (TextView) view.findViewById(R.id.address);
String naTxt = cursor.getString(COL_NAME);
String addTxt = cursor.getString(COL_ADDR)
+" "
+cursor.getString(COL_STR);
name.setText(naTxt);
address.setText(addTxt);
}
};
root.setAdapter(cursorAdapter);
发布于 2016-09-23 01:58:22
我发现了这个问题,这确实是最新的android中的一个新东西,因为我以前从未见过它。对于行布局,我使用了android:layout_height="match_parent"
。我应该用的是android:layout_height="wrap_content"
。难倒我了。
发布于 2016-09-23 01:14:31
请尝试删除
cursor.moveToFirst();
并使用
CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,0)
代替CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,true)
并且您应该使用android.widget.CursorAdapter
https://stackoverflow.com/questions/39650581
复制