我不知道为什么我的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 -BaseAdapter不会向listView添加新项全部内容,希望文章能够帮你解决android -BaseAdapter不会向listView添加新项所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)