具有不同行布局的游标适配器
我正在尝试创建一个自定义光标适配器,它将根据光标中的某些数据使用两种不同的布局。我一直在阅读“重写 getViewTypeCount() 和 getItemViewType()”来实现这一点,但我似乎无法弄清楚如何实现这一点。
这是我的 bindView 和新 View 方法的代码:
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tView = (TextView) view.findViewById(R.id.TextView1);
tView.setText("The text");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.item1, parent, false);
}
。
编辑: 现在我可以正常工作了,但我想根据游标中的某些数据选择要使用的行布局,但我无法正常工作..有人有一些想法吗?
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtAddress
.setText("blabla");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View v = null;
int type = cursor.getPosition() % 2;
if(type == 0) {
v = mInflater.inflate(R.layout.item, parent, false);
} else {
v = mInflater.inflate(R.layout.item2, parent, false);
}
holder.txtAddress = (TextView) v.findViewById(R.id.tvName);
v.setTag(holder);
return v;
}
I'm trying to create a custom cursoradapter that will use two different layouts depending on some data in the cursor. I keep reading about 'overriding getViewTypeCount() and getItemViewType()' to make this happen but I can't seem to figure out how to implement this..
This is my code for the bindView and new View methods:
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tView = (TextView) view.findViewById(R.id.TextView1);
tView.setText("The text");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.item1, parent, false);
}
.
EDIT:
Now I got this working, but I want to choose what rowlayout to use depending on some data in the cursor and I can't get that working.. anybody got some ideas?
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtAddress
.setText("blabla");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View v = null;
int type = cursor.getPosition() % 2;
if(type == 0) {
v = mInflater.inflate(R.layout.item, parent, false);
} else {
v = mInflater.inflate(R.layout.item2, parent, false);
}
holder.txtAddress = (TextView) v.findViewById(R.id.tvName);
v.setTag(holder);
return v;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以我终于成功了。对于感兴趣的人,工作代码如下:
So I finally got it work. For the ones interested the working code is below:
看一下这个示例,您可以轻松地修改它来解决您的问题。这非常简单。
Take a look to this example, you can easily adapt it for solving your problem. It's pretty straightforward.
关于在 getItemViewType 方法中访问光标的另一个可能的解决方案如下:
Another possible solution regarding the access to the cursor in the getItemViewType method is the following: