I have a list in my activity, when I get to the end of the list, it calls async and gets new data using json. these are the async and baseAdaper codes:
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 ;
I registered here, when I reached the end of the View list, it calls addAll, and it returns 30 new items that they buy, they are not added to the listview, I donβt know why.
source share