【问题标题】:android-notifyDataSetChanged doesn't work on BaseAdapterandroid-notifyDataSetChanged 在 BaseAdapter 上不起作用
【发布时间】:2014-11-08 04:57:04
【问题描述】:

我的活动有一个 listview ,当我到达 listview 的末尾时,它会调用 async 并使用 json 获取新数据。 这是 async 和 baseAdaper 代码:

ListAdapter ladap;

private class GetContacts AsyncTask<Void, Void,ArrayList<HashMap<String, String>>> {    
@Override
protected Void doInBackground(Void... arg0) {
    Spots_tab1_json sh = new Spots_tab1_json();
    String jsonStr = sh.makeServiceCall(url + page, Spots_tab1_json.GET);

    ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);
                String id = new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8");
                String dates = new String(c.getString("dates").getBytes("ISO-8859-1"), "UTF-8");
                String price = new String(c.getString("gheymat").getBytes("ISO-8859-1"), "UTF-8");
                HashMap<String, String> contact = new HashMap<String, String>();
                contact.put("id", id);
                contact.put("dates", dates);
                contact.put("price", price);
                dataC.add(contact);
            }
            }
        } catch (JSONException e) {
            goterr = true;
        } catch (UnsupportedEncodingException e) {
            goterr = true;
        }
    } else {
        goterr = true;
    }
    return dataC;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    super.onPostExecute(result);
    if (!isCancelled() && goterr == false) {
        if(ladap==null){
            ladap=new ListAdapter(MainActivity.this,result);
            lv.setAdapter(ladap);
        }else{
            ladap.addAll(result);
            ladap.notifyDataSetChanged();
        }

}

}

public class ListAdapter extends BaseAdapter {
Activity activity;
public ArrayList<HashMap<String, String>> list;

public ListAdapter(Activity activity,ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = (Activity) activity;
    this.list = list;
}

public void addAll(ArrayList<HashMap<String, String>> result) {
    Log.v("this",result.size()+" resultsize");
    this.list = result;
    notifyDataSetChanged();
}

public int getCount() {
    return contactList.size();
}

public Object getItem(int position) {
    return contactList.get(position);
}

public long getItemId(int arg0) {
    return 0;
}

private class ViewHolder {
    TextView title,price;
    ImageView img ; 
    //RelativeLayout rl; 
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.price = (TextView) convertView.findViewById(R.id.price);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

        item = contactList.get(position);
        holder.price.setText(item.get("price"));
    return convertView;
}
}

我在这里登录,当我到达 listView 的末尾时,它调用 addAll 并返回新的 30 项购买它没有添加到 listview ,我不知道为什么。

【问题讨论】:

    标签: android android-listview android-adapter


    【解决方案1】:

    首先,当您调用 addAll() 时,您已经通知了您的列表视图,因此不再需要此代码,请从您的代码中删除:

    ladap.notifyDataSetChanged();
    

    将新数据添加到列表视图数据持有者,而不是将新数据分配给列表视图数据持有者:

    public void addAll(ArrayList<HashMap<String, String>> result) {
        Log.v("this",result.size()+" resultsize");
        this.list = result;
        notifyDataSetChanged();
    }
    

    替换为:

    public void addAll(ArrayList<HashMap<String, String>> result) {
        Log.v("this",result.size()+" resultsize");
        if(list==null){
          list = new ArrayList<HashMap<String, String>>();
        }
        list.addAll(result)
        notifyDataSetChanged();
    }
    

    【讨论】:

    • 是的,这就是我的意思..LOL :)
    【解决方案2】:

    尝试改变这一点:

    public void addAll(ArrayList<HashMap<String, String>> result) {
        Log.v("this",result.size()+" resultsize");
        this.list = result;
        notifyDataSetChanged();
    }
    

    收件人:

    public void addAll(ArrayList<HashMap<String, String>> result) {
        Log.v("this",result.size()+" resultsize");
        for(int i = 0;i < result.size(); i++)
           list.add(result.get(i));
        //notifyDataSetChanged(); //no need to call again here
    }
    

    重点是,我认为this.list = result; 会在添加新项目之前删除旧项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多