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

时间:2017-02-13 10:50:08

标签: android list scroll adapter

滚动后,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);
    }
}
}

2 个答案:

答案 0 :(得分:0)

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

System.ServiceModel.Web

修改1:

@Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; } 应返回getViewTypeCount()包含的不同观看次数。如果ListView中的所有项目都属于同一类型,则应返回1.

最初,您的ListView为空,eventListe方法返回0,这导致getCount()

使用

更改此方法
Exception

答案 1 :(得分:0)

为此,您必须处理两种情况(if和else) 像这样

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;

}