【发布时间】:2015-12-09 13:39:09
【问题描述】:
我目前尝试将接收自异步任务的数据添加到自定义适配器。在第一次调用任务时,有一个空的“通知”列表。在第二次和第三次通话中,收到的通知必须添加到现有的通知列表中。
我的问题是新通知不会添加到现有列表中。新通知将取代旧列表。
private class LoadNotificationsTask extends AsyncTask<String, Integer, ArrayList<Notification>> {
NotificationFragment fragment;
String parameters;
private LoadNotificationsTask(NotificationFragment fragment, String parameters) {
this.fragment = fragment;
this.parameters = parameters;
}
@Override
protected ArrayList<Notification> doInBackground(String... params) {
try {
return Client.getNotifications(this.parameters);
} catch (Exception e) {
return new ArrayList<Notification>();
}
}
@Override
protected void onPostExecute(ArrayList<Notification> notifications) {
NotificationListAdapter notificationListAdapter = new NotificationListAdapter(notifications);
setListAdapter(notificationListAdapter);
}
}
public class NotificationListAdapter extends BaseAdapter {
private ArrayList<Notification> notifications;
public NotificationListAdapter(ArrayList<Notification> notifications) {
super();
this.notifications = notifications;
}
@Override
public int getCount() {
return this.notifications.size();
}
@Override
public Object getItem(int position) {
return this.notifications.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.notificationlist_item, parent, false);
}
final Notification notification = (Notification) this.getItem(position);
TextView headline = (TextView) convertView.findViewById(R.id.notilist_hostname);
headline.setText(notification.getNAME1() + "\\" +notification.getNAME2());
return convertView;
}
【问题讨论】:
-
你面临的问题是什么?
-
有什么问题。解释
-
第一次调用任务有效,第二次或第三次调用任务,旧通知将被新通知替换而不是添加它
-
尽量不要在
onPostExecute中创建您的适配器,而是通过现有或不存在的过滤器向适配器添加通知
标签: android android-asynctask android-arrayadapter