光标未与自定义适配器正确绑定文本
为我的 ListFragment
扩展 SimpleCursorAdapter
的自定义适配器无法正确显示数据库中的项目。它不显示 TextView 中的文本,我还需要对光标执行什么操作才能显示正确的文本?
我以为我必须覆盖 bindView
但这没有效果
设置适配器:
public void populateList(){
String[] fields = new String[] {BowlersDB.NAME};
Cursor c = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,
new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");
mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,c,fields,new int[] {R.id.nameCheckTV});
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
适配器:
public class CheckAdapter extends SimpleCursorAdapter{
Context context;
Cursor c;
public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.c = c;
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view,Context context,Cursor cursor){
String st = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));
TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
tv.setText(st);
}
@Override
public View getView(final int position,View convertView,ViewGroup parent){
if(convertView == null){
LayoutInflater inflator = getLayoutInflater();
convertView = inflator.inflate(R.layout.check_listview,null);
}
CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
cb.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox check = (CheckBox)v.findViewById(R.id.checkBox1);
int pos = position;
if(check.isChecked()){
mCheckedItems.add(position);
}else if(!check.isChecked()){
for(int i=0;i< mCheckedItems.size();i++){
if(mCheckedItems.get(i) == position){
mCheckedItems.remove(i);
}
}
}
}
});
return convertView;
}
}
My Custom Adapter that extends SimpleCursorAdapter
for my ListFragment
does not display the items in my database correctly. it does not display the text in the TextView, what else do I have to do to the cursor for it to show the correct text?
I thought I had to override the bindView
but that had no effect
Setting the adapter:
public void populateList(){
String[] fields = new String[] {BowlersDB.NAME};
Cursor c = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,
new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");
mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,c,fields,new int[] {R.id.nameCheckTV});
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
Adapter:
public class CheckAdapter extends SimpleCursorAdapter{
Context context;
Cursor c;
public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.c = c;
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view,Context context,Cursor cursor){
String st = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));
TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
tv.setText(st);
}
@Override
public View getView(final int position,View convertView,ViewGroup parent){
if(convertView == null){
LayoutInflater inflator = getLayoutInflater();
convertView = inflator.inflate(R.layout.check_listview,null);
}
CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
cb.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox check = (CheckBox)v.findViewById(R.id.checkBox1);
int pos = position;
if(check.isChecked()){
mCheckedItems.add(position);
}else if(!check.isChecked()){
for(int i=0;i< mCheckedItems.size();i++){
if(mCheckedItems.get(i) == position){
mCheckedItems.remove(i);
}
}
}
}
});
return convertView;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白了。查看
CursorAdapter.getView()
。请注意getView()
中如何调用bindView()
和newView()
。看起来,通过覆盖getView()
,您可以确保您的CursorAdapter
永远不会调用bindView()
。要么将所有内容仅放入getView()
中,要么仅放入newView()
和bindView()
中,但不能同时放入两者中。编辑我:
四件事:
我已经阅读了
CursorAdapter
,看起来你想重写bindView()
getView()
的。看起来getView()
对于要显示的每一行都被调用两次,这在某种程度上阻止了光标的更新,因此始终指向第一行。我认为这就是为什么您的ListView
仅显示第一行。此外,ListView 的默认CursorAdapter
实现将为您回收视图,因此重写getView()
确实没有必要(查看此 回答了解更多信息)。创建
mAdapter
时,您应该将 nullCursor
传递到构造函数中。Cursor
将在onLoadFinished
中传递(当Loader
完成查询时),并将通过swapCursor(光标)
。您的
onCreateLoader()
方法将创建(或启动)执行初始查询的Loader
。确保它看起来像这样,<前><代码>@OverrideonCreateLoader(int id, Bundle args) {
公共加载器
String[] 投影 = { BowlersDB.ID, BowlersDB.NAME };
CursorLoader 游标加载器 = new CursorLoader(getActivity(),
BowlersDB.CONTENT_URI,投影,空,空,空);
返回游标加载器;
}
确保使用正确的
SimpleCursorAdapter
构造函数...您当前使用的构造函数已被弃用! 因此您无需传递任何特殊内容)。编辑 II:
澄清一下,
bindView()
方法是您使用Cursor
当前位置的数据填充行的小部件的地方。您将被预先定位在正确的行上的光标
。您需要从Cursor
中提取数据并将其倒入您所需的小部件中。换句话说,您需要查询行的 id(而不是使用getView()
中的“position”参数)。尝试在bindView()
中设置onClickListener
,如下所示:I think I got it. Check out the implementation for
CursorAdapter.getView()
. Notice howbindView()
andnewView()
are called withingetView()
. It looks like by overridinggetView()
you are ensuring that yourCursorAdapter
never callsbindView()
. Either put everything ONLY ingetView()
or ONLY innewView()
andbindView()
, but not both.EDIT I:
Four things:
I've read up on
CursorAdapter
s and it looks like you want to overridebindView()
instead ofgetView()
. It looks likegetView()
is called twice for each row that's to be displayed and this is somehow preventing the cursor from being updated and thus always points to the first row. I think this is why yourListView
only displays the first row. Further, the defaultCursorAdapter
implementation for a ListView will recycle views for you, so overridinggetView()
really isn't necessary (check out this answer for more info).When creating your
mAdapter
, you should pass a nullCursor
into the constructor. TheCursor
will be delivered inonLoadFinished
(when theLoader
has finished the query) and will be bound to the adapter withswapCursor(cursor)
.Your
onCreateLoader()
method will create (or start) theLoader
that will perform the initial query. Make sure it looks something like this,Make sure you make use of the correct
SimpleCursorAdapter
constructor... the one you are currently using is deprecated! (pass0
as a flag... theCursorLoader
will register a content observer for you so you don't need to pass in anything special).EDIT II:
To clarify, the
bindView()
method is where you populate your row's widgets with the data from theCursor
's current position. You are handed theCursor
pre-positioned at the proper row. You need to pull data out of theCursor
and pour it into your desired widgets. In other words, you'll need to query for the row's id (instead of using the "position" argument ingetView()
). Try setting theonClickListener
inbindView()
with something like: