Android:自定义光标适配器问题
我想知道是否有人能够为我指明正确的方向,或者帮我看看我的列表视图自定义光标适配器类出了什么问题。
基本上,我让它“工作”,因为它会用数据库中的名字填充列表视图,而不是移动到下一行。但是现在,它会抛出一个错误并且不会进入列表,所以任何感谢帮助。
基本上,我在实现时遇到的麻烦是从数据库读取名称和图像路径,并使用我的自定义适配器将其应用到 row.xml。这是我的代码:
public class CustomAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private Context context;
private int layout;
//private Cursor c;
String animal_name;
String img_path;
public CustomAdapter(Context context,int layout, Cursor c, String[] from, int[] to){
super(context, layout, c, from, to);
//this.c = c;
this.context = context;
this.layout = layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewWrapper wrapper = null;
Cursor c = getCursor();
animal_name = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_ANIMALNAME)));
img_path = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_IMGPATH)));
if(row == null){
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(R.layout.row, null);
// row is passed in as "base" variable in ViewWrapper class.
wrapper = new ViewWrapper(row);
row.setTag(row);
}
else{
wrapper=(ViewWrapper)row.getTag();
}
wrapper.getLabel().setText(animal_name);
wrapper.getIcon().setImageResource(R.id.icon);
return (row);
}
}
class ViewWrapper{
View base;
TextView label = null;
ImageView icon = null;
ViewWrapper(View base){
this.base = base;
}
TextView getLabel(){
if(label== null){
label=(TextView)base.findViewById(R.id.author);
}
return (label);
}
ImageView getIcon(){
if(icon == null){
icon=(ImageView)base.findViewById(R.id.icon);
}
return (icon);
}
}
并按如下方式设置适配器
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.row, myCursor, new String[]{"animal_name"}, new int[]{R.id.animal});
this.setListAdapter(mAdapter);
非常感谢任何帮助。
I am wondering if someone may be able to point me in the right direction or lend a pair of eyes and see where I am going wrong with my custom cursor adapter class for a listview.
Basically, I had it "working", in the sense that it would populate the listview with the firstname from the database, and not move to the next row.Now however, it will throw up an error and not enter into list, so any help is appreciated.
Basically what I am having trouble in achieving is reading a name and image path from database and apply it to a row.xml with my custom adapter.Here is my code:
public class CustomAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private Context context;
private int layout;
//private Cursor c;
String animal_name;
String img_path;
public CustomAdapter(Context context,int layout, Cursor c, String[] from, int[] to){
super(context, layout, c, from, to);
//this.c = c;
this.context = context;
this.layout = layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewWrapper wrapper = null;
Cursor c = getCursor();
animal_name = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_ANIMALNAME)));
img_path = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_IMGPATH)));
if(row == null){
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(R.layout.row, null);
// row is passed in as "base" variable in ViewWrapper class.
wrapper = new ViewWrapper(row);
row.setTag(row);
}
else{
wrapper=(ViewWrapper)row.getTag();
}
wrapper.getLabel().setText(animal_name);
wrapper.getIcon().setImageResource(R.id.icon);
return (row);
}
}
class ViewWrapper{
View base;
TextView label = null;
ImageView icon = null;
ViewWrapper(View base){
this.base = base;
}
TextView getLabel(){
if(label== null){
label=(TextView)base.findViewById(R.id.author);
}
return (label);
}
ImageView getIcon(){
if(icon == null){
icon=(ImageView)base.findViewById(R.id.icon);
}
return (icon);
}
}
and have been setting the adapter as follows
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.row, myCursor, new String[]{"animal_name"}, new int[]{R.id.animal});
this.setListAdapter(mAdapter);
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是你正在做 row.setTag(row) 。您应该将Tag 设置为ViewWrapper。
在游标适配器中,您应该覆盖bindview和newView而不是getView。
从 10,000 英尺开始,其工作方式如下
GetView如果view为null则调用newView,new view后调用bindView。
You problem is that you were doing row.setTag(row). You should setTag to ViewWrapper instead.
In cursor adapter, you should override bindview and newView instead of getView.
From 10,000 feet, the way it works is as follow
GetView calls newView if the view is null, and call bindView after new view.