【发布时间】:2015-11-24 03:42:12
【问题描述】:
我基本上是在尝试通过同一个 ListView 适配器显示多个视图。但是,适配器最终会生成多个重复项,有时还会使用 NullPointer 崩溃。我的猜测是我实现的适配器都错了。这是我的完整代码:
该项目可以是照片或文字。
适配器:
public class FeedAdapter extends BaseAdapter {
static private Activity activity;
private static LayoutInflater inflater = null;
ArrayList<ActivityTable> actList = new ArrayList<ActivityTable>();
Holder holder;
public FeedAdapter(Activity a, ArrayList<ActivityTable> actList) {
activity = a;
this.actList = actList;
}
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
final ActivityTable act = actList.get(position);
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
if (act.getType().equals("text")) {
convertView = inflater.inflate(R.layout.feed_single_text, null);
holder = new Holder();
//More code that Set the caption to the holder
convertView.setTag(holder);
}
if (act.getType().equals("photo")) {
convertView = inflater.inflate(R.layout.feed_single_picture, parent, false);
holder = new Holder();
holder.media = (ImageView) convertView.findViewById(R.id.postphoto);
//More code that Set the photo to the holder
convertView.setTag(holder);
}
} else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
public static class Holder {
ImageView media;
TextView caption;
}
}
我是否以错误的方式在同一个适配器中膨胀多个视图?谁能指出错误?
【问题讨论】:
-
你能贴出你为每一行设置数据的代码吗?我的意思是“返回转换视图”上方和
else{...}下方的代码 -
当有更好的选择时,你为什么还要使用
ListView?您在维护旧代码吗? -
@AvinashR 有什么更好的选择?
-
@PhanVănLinh 即使没有设置数据,我也会得到重复的行
-
请发布错误堆栈。不知道这些人如何发布答案的错误?
标签: java android listview android-listview