其他分享
首页 > 其他分享> > android -BaseAdapter不会向listView添加新项

android -BaseAdapter不会向listView添加新项

作者:互联网

我不知道为什么我的listView不添加新项目.

这是代码:

   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添加新行.我记录了这个baseAdapter中有30个新项目:

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

但它没有添加到listView.

你能帮我解决这个问题吗?

谢谢

解决方法:

您已使用contactList ArrayList显示ListView项,并且您将更新数据到doinBackground()中的contactList,它运行不在ui线程中的另一个线程,因此您无法从out ui therad更新ui数据,因此您必须将本地ArrayList用于doInBackground()并将新的或更新的数据传递给onPostExecute(),它将此数据更新为ui线程.

private class GetContacts extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
    @Override
    protected ArrayList<HashMap<String, String>> 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>> data = 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);
                    data.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

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

标签:android-adapter,android,listview,android-arrayadapter
来源: https://codeday.me/bug/20191003/1849839.html