Android自定义Adapter从asynctask添加数据不会被添加
Android custom Adapter add data from asynctask will not be added
我目前正在尝试将从异步任务接收到的数据添加到自定义适配器。在第一次调用任务时,有一个空列表 "notifications"。在第二次和第三次调用时,收到的通知必须添加到现有通知列表中。
我的问题是新通知不会添加到现有列表中。新通知将替换旧列表。
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;
}
首先在 AsyncTask 之外启动 ListView 适配器,即。在 onCreate()
里面
// Declare class variable
ArrayList<Notification> mNotifications;
NotificationListAdapter notificationListAdapter;
// inside onCreate()
mNotifications = new ArrayList<Notification>();
notificationListAdapter = new NotificationListAdapter(mNotifications);
setListAdapter(notificationListAdapter);
然后使用以下代码编辑您的 AsyncTask postExecute()
@Override
protected void onPostExecute(ArrayList<Notification> notifications) {
mNotifications.addAll(notifications);
notificationListAdapter.notifyDataSetChanged();
}
也许更改 getItemId 函数 return 值。
@Override
public long getItemId(int position) {
return 0;//change return position;
}
我目前正在尝试将从异步任务接收到的数据添加到自定义适配器。在第一次调用任务时,有一个空列表 "notifications"。在第二次和第三次调用时,收到的通知必须添加到现有通知列表中。
我的问题是新通知不会添加到现有列表中。新通知将替换旧列表。
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;
}
首先在 AsyncTask 之外启动 ListView 适配器,即。在 onCreate()
里面 // Declare class variable
ArrayList<Notification> mNotifications;
NotificationListAdapter notificationListAdapter;
// inside onCreate()
mNotifications = new ArrayList<Notification>();
notificationListAdapter = new NotificationListAdapter(mNotifications);
setListAdapter(notificationListAdapter);
然后使用以下代码编辑您的 AsyncTask postExecute()
@Override
protected void onPostExecute(ArrayList<Notification> notifications) {
mNotifications.addAll(notifications);
notificationListAdapter.notifyDataSetChanged();
}
也许更改 getItemId 函数 return 值。
@Override
public long getItemId(int position) {
return 0;//change return position;
}