2013-10-25 73 views
-3

你好我工作的應用程序,它是類似於facebook的那個。在那目前我被困在一個點,就像我們在facebook上的帖子顯示在我們的牆上,所有的帖子是像20 20時尚我想在我的應用程序中應用同樣的事情。對於那件事,我使用listview哪裏得到價值形式服務器和創建視圖根據我也得到所有的價值,但問題是,當我添加第一個20值然後它工作正常,但是當我添加另一個20值將刪除以前的數據在listview中。如何維護新增元素的數組列表?

任何想法如何,我可以做這件事情在我的應用程序,並在此先感謝....從服務器

我的函數來獲取價值

private void getPostnew(String start) { 

     String URL = start; 

     System.out.println("start value new :" + start); 

     final String usernamefor = ""; 

     aq = new AQuery(getParent()); 
     listitem = new ArrayList<BeanTop>(); 
     aq.ajax(URL, JSONObject.class, 10 * 1000, 
       new AjaxCallback<JSONObject>() { 

        @Override 
        public void callback(String url, JSONObject json1, 
          AjaxStatus status) { 

         System.out.println("json " + json1); 

         if (json1 == null) { 

         } else { 

          try { 

           JSONArray jarray = json1 
             .getJSONArray("subject"); 

           for (int j = 0; j < jarray.length(); j++) { 
            try { 
             JSONObject j1 = jarray.getJSONObject(j); 

             try { 

              listcount = j1 
                .getString("likecount"); 

             } catch (Exception e) { 
              listcount = "0"; 

             } 

             AddObject(j1.getString("text"), 
               j1.getString("leftpic"), 
               j1.getString("rightpic"), 
               j1.getString("rightvotecount"), 
               j1.getString("leftvotecount"), 
               j1.getString("textleft"), 
               j1.getString("textright"), 
               j1.getString("date_created"), 
               j1.getString("voteid"), 
               j1.getString("user"), 
               j1.getString("dom"), 
               j1.getString("Isvoted"), 
               j1.getString("Islike"), 
               j1.getString("profilepic"), 
               listcount, 
               j1.getString("commentcount")); 

            } catch (Exception e) { 

             e.printStackTrace(); 
            } 

           } 
          } catch (Exception e) { 

           e.printStackTrace(); 
          } 

         } 

         FriendlistAdapter ad = new FriendlistAdapter(Top.this, 
           listitem); 
         subjectlist.setAdapter(ad); 
         ad.notifyDataSetChanged(); 

        } 

       }); 

    } 

方法保存在bean類

數據
private void AddObject(String string1, String string2, String string3, 
      String string5, String string6, String string7, String string8, 
      String string9, String string10, String string11, 
      String usernamefor, String isvoted, String isliked, 
      String profilepic, String likecount, String commentcount) { 

     BeanTop ib = new BeanTop(); 

     Date date = null; 
     try { 
      System.out.println("date " + string9); 
      date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string9); 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     ib.setText(string1); 
     ib.setLeftpic(string2); 
     ib.setRightpic(string3); 
     ib.setRightVote(string5); 
     ib.setLeftVote(string6); 
     ib.setLefttext(string7); 
     ib.setRighttext(string8); 
     ib.setDate(string9); 
     ib.setDate1(date); 
     ib.setVoteid(string10); 
     ib.setUsername(string11); 
     ib.setDom(usernamefor); 
     ib.setIsvoted(isvoted); 
     ib.setIsliked(isliked); 
     ib.setProfilepic(profilepic); 
     ib.setLikecount(likecount); 
     ib.setCommentcount(commentcount); 
     List<BeanTop> bookingList = new ArrayList<BeanTop>(); 
     bookingList.addAll(listitem); 
     Collections.sort(bookingList, new Comparator<BeanTop>() { 
      public int compare(BeanTop m1, BeanTop m2) { 
       return m1.getDate().compareTo(m2.getDate()); 
      } 
     }); 
     Collections.reverse(bookingList); 

     try { 
     listitem.clear(); 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
     listitem.addAll(bookingList); 
     try { 
      bookingList.clear(); 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     listitem.add(ib); 

    } 
+1

發佈您的代碼! –

+0

你可以現在檢查.. – Simmant

+0

一些像... addAll:/ –

回答

0

不能說沒有看到你的代碼,但是我可以告訴你這樣做的一般方法...

首先,你應該保持一個列表,在說一流水平像...

List<MyPost> posts = ArrayList<MyPost>(); 

然後你就可以創建你通過傳遞它的帖子名單適配器。

並調用「.addAll(XXX)」給你‘帖子’名單每次你從服務器類的物品,

posts.addAll(newItems); 

,並在之後的是,調用yourAdapter.notifyDatasetChanged();讓列表可以重繪/更新其意見...

而且在你的代碼...使用,

if (listitem == null) { 
    listitem = new ArrayList<BeanTop>(); 
} 

getPostnew(),而不是隻listitem = new ArrayList<BeanTop>();

希望這有助於...

+0

感謝回答我的問題我得到我的代碼的解決方案,我刪除listitem.clear();形成我的代碼,但當我這樣做,我掛我的整個應用程序,並給出並跟隨錯誤.... java.lang.OutOfMemoryError – Simmant

0

要重新初始化listitem = new ArrayList<BeanTop>(); eveytime的getPostnew methood被調用。這就是爲什麼你的舊帖子丟失了。嘗試改變該行:

if (listitem == null) { 
     listitem = new ArrayList<BeanTop>(); 
    } 
0

你應該使用全局變量來存儲你從服務器獲取數據,在你的方法每次執行它時 getPostnew(String start),該listitem將被重新創建;所以最後數據會迷路。

相關問題