【问题标题】:Android custom Adapter add data from asynctask will not be added [closed]Android自定义适配器从asynctask添加数据将不会被添加[关闭]
【发布时间】: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


【解决方案1】:

首先在 AsyncTask 之外启动 ListView Adapter 即。在 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();
}

【讨论】:

  • 啊,太简单了....我尝试将新通知添加到适配器,而不是从列表中删除。效果很好!谢谢!
【解决方案2】:

也许改变 getItemId 函数的返回值。

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

【讨论】:

    猜你喜欢
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    相关资源
    最近更新 更多