滚动后未正确显示android列表适配器项

android list scroll adapter

94 观看

2回复

32 作者的声誉

ListView滚动后,我的项目混合了。该问题与无关,adapter但我无法弄清楚出了什么问题。在我的List商店里有几家Events。如果列表变大,它们就会混杂在一起。作为示例,当前事件将突出显示,但是在汇总之后,其他标题也将突出显示。

public class EintragAdapter extends BaseAdapter {

private ArrayList<Event> eventListe;
private final LayoutInflater inflater;
private Context context;

public EintragAdapter(Context c) {
    super();
    eventListe = new ArrayList<>();
    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    context = c;
}

@Override
public int getCount() {
    return eventListe.size();
}

@Override
public Event getItem(int position) {
    return eventListe.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean isEmpty() {
    return eventListe.isEmpty();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    final Event event = getItem(position);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_event, parent, false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    String endDate = event.getEnd();
    if (endDate.isEmpty()) {
        viewHolder.date.setText(event.getStart());
    } else {
        viewHolder.date.setText(String.format("%1$s %2$s %3$s", event.getStart(), "bis", endDate));
    }

    if (event.isToday()) {
        viewHolder.title.setTextColor(parent.getResources().getColor(R.color.colorAccent));
    }

    MainActivity mainActivity = (MainActivity) context;
    if (!mainActivity.userIsAdmin()) {
        viewHolder.delete.setVisibility(View.GONE);
    }

    // viewHolder.timestamp.setText(String.format("%1$s %2$s", "Zuletzt geändert:", event.getTimestamp()));
    if (!event.getDescription().isEmpty())
        viewHolder.description.setText(event.getDescription());
    viewHolder.title.setText(event.getTitle());
    viewHolder.delete.setTag(event);
    return convertView;
}

public void addJSONArray(JSONArray array) throws JSONException {
    if (!eventListe.isEmpty())
        eventListe.clear();

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        eventListe.add(new Event(
                object.getInt("id"),
                object.getString("title"),
                object.getString("description"),
                object.getString("start"),
                object.getString("end"),
                object.getString("reg_date")));
    }

    // sort by date - descending
    Collections.sort(eventListe, new Comparator<Event>() {
        @Override
        public int compare(Event event1, Event event2) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

            try {
                Date date1 = format.parse(event1.getStart());
                Date date2 = format.parse(event2.getStart());
                return date1.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return 0;
        }
    });

    notifyDataSetChanged();
}

private static class ViewHolder {
    final TextView title, date, description;
    final View delete;

    ViewHolder(View view) {
        title = (TextView) view.findViewById(R.id.event_title);
        date = (TextView) view.findViewById(R.id.event_date);
        description = (TextView) view.findViewById(R.id.event_description);
        delete = view.findViewById(R.id.deleteButton);
    }
}
}
作者: df1e 的来源 发布者: 2017 年 2 月 13 日

回应 2


0

1082 作者的声誉

在您的适配器类中重写这两个方法

@Override
public int getViewTypeCount() {
   return getCount();
}

@Override
public int getItemViewType(int position) {
   return position;
}

编辑1:

getViewTypeCount()应该返回您ListView包含的不同视图的数量。如果您的所有项目ListView都属于同一类型,则应返回1。

最初,您eventListe为空,并且getCount()方法返回0,这导致Exception

更改此方法

@Override
public int getViewTypeCount() {
   return getCount()>0?getCount():1;
}
作者: Gopal 发布者: 2017 年 2 月 13 日

0

99 作者的声誉

为此,您必须同时处理两种情况(如果和其他情况)

if (event.isToday()) {
    viewHolder.title.setTextColor(parent.getResources().getColor(R.color.colorAccent));
}
else{viewHolder.title.setTextColor(/*Default color here*/);}

这将解决您的突出问题。现在将适配器构造器修改为

public EintragAdapter(Context c, ArrayList<Event> data) {
super();
eventListe = data;  //data not be null
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
context = c;

}

作者: Ashish 发布者: 2017 年 2 月 13 日
32x32