具有不同行布局的游标适配器 [英] cursoradapter with different row layouts
问题描述
我正在尝试创建一个自定义光标适配器,它将根据光标中的某些数据使用两种不同的布局.我一直在阅读关于覆盖 getViewTypeCount() 和 getItemViewType()"来实现这一点,但我似乎无法弄清楚如何实现这一点..
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..
这是我的 bindView 和新 View 方法的代码:
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);
}
.
现在我开始工作了,但我想根据游标中的一些数据选择要使用的行布局,但我无法让它工作......有人有什么想法吗?
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;
}
推荐答案
所以我终于搞定了.对于感兴趣的人,工作代码如下:
So I finally got it work. For the ones interested the working code is below:
private int getItemViewType(Cursor cursor) {
String type = cursor.getString(cursor.getColumnIndex("type"));
if (type.equals("1")) {
return 0;
} else {
return 1;
}
}
@Override
public int getItemViewType(int position) {
Cursor cursor = (Cursor) getItem(position);
return getItemViewType(cursor);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.textView
.setText(cursor.getString(cursor.getColumnIndex("body")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View v = null;
if (cursor.getString(cursor.getColumnIndex("type")).equals("1")) {
v = mInflater.inflate(R.layout.message1, parent, false);
holder.textView = (TextView) v
.findViewById(R.id.textView1);
} else {
v = mInflater.inflate(R.layout.message2, parent, false);
holder.textView = (TextView) v
.findViewById(R.id.textView2);
}
v.setTag(holder);
return v;
}
public static class ViewHolder {
public TextView textView;
}
这篇关于具有不同行布局的游标适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!