2016-06-28 59 views
0

我在android應用程序中工作,其中我必須顯示數據從json到listview。現在我從json獲取數據,但我在listview中獲取重複值。我如何解決這個問題?如何從列表視圖中刪除重複值?

JSON VALUE

{"result":[{"messages":"i am user","sender_id":"5","rec_id":"admin"},{"messages":"i am admin","sender_id":"admin","rec_id":"5"},{"messages":"i am user again","sender_id":"5","rec_id":"admin"},{"messages":"i am admin again.","sender_id":"admin","rec_id":"5"}]} 

代碼

protected void showList(){ 
    try { 
     JSONObject jsonObj = new JSONObject(myJSON); 
     peoples = jsonObj.getJSONArray(TAG_RESULTS); 

     for(int i=0;i<peoples.length();i++){ 
      JSONObject c = peoples.getJSONObject(i); 

      id = c.getString("sender_id"); 
      ids = c.getString("rec_id"); 
      if(id.equals(session_id)&&ids.equals("admin")) { 
       ss = c.getString("messages"); 
       } 
      if(id.equals("admin")&&ids.equals(session_id)) { 
       tt = c.getString("messages"); 
      } 

      HashMap<String,String> persons = new HashMap<String,String>(); 

      persons.put(TAG_ID,tt); 
      persons.put(TAG_MESSAGE,ss); 
      personList.add(persons); 

     } 

     adapter = new SimpleAdapter(
       Messages.this, personList, R.layout.activity_message,new String[]{TAG_ID,TAG_MESSAGE},new int[]{R.id.id,R.id.messag}); 

     list.setAdapter(adapter); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

public void getData(){ 
    class GetDataJSON extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      InputStream inputStream = null; 
      String result = null; 
      try { 

       String postReceiverUrl = "http://piresearch.in/gpsapp/emp_message.php"; 
       //"http://progresscard.progresscard.in/progress_card/messages/get_messages.php"; 
       // HttpClient 
       HttpClient httpClient = new DefaultHttpClient(); 

       // post header 
       HttpPost httpPost = new HttpPost(postReceiverUrl); 

       // add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("user_id", session_id)); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpClient.execute(httpPost); 
       HttpEntity resEntity = response.getEntity(); 

       inputStream = resEntity.getContent(); 
       // json is UTF-8 by default 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (Exception e) { 
       Log.i("tagconvertstr", "[" + result + "]"); 
       System.out.println(e); 
      } 
      finally { 
       try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
      } 
      return result; 
     } 

     @Override 
     protected void onPostExecute(String result){ 
      myJSON = result; 
      showList(); 
     } 
    } 
    GetDataJSON g = new GetDataJSON(); 
    g.execute(); 
} 

這就是我得到enter image description here

+0

嘗試在顯示列表之前清除personList。 – KunalK

+0

'tt'和'ss'代表什麼,它們在哪裏定義? 'id'和'ids'一樣。儘量讓你的變量名稱更清晰。無論如何,目前還不清楚他們爲什麼看起來像田地 –

回答

1

以前的數據Clear your arraylist在it.Check添加新的數據列表的大小,如果前它大於零清除你的數組列表,然後在列表中添加HashMap。

像這樣的事情

if(personList.size()>0) 
    personList.clear(); 
//now add your HashMap into list 
0

我還沒有得到妥善的解決辦法,但我已經做在HashMap的改變解決了這個問題。

HashMap<String,String> persons = new HashMap<String,String>();//globally Declare 

    protected void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      peoples = jsonObj.getJSONArray(TAG_RESULTS); 

      for(int i=0;i<peoples.length();i++){ 
       JSONObject c = peoples.getJSONObject(i); 

       id = c.getString("sender_id"); 
       ids = c.getString("rec_id"); 
       if(id.equals(session_id)&&ids.equals("admin")) { 
        ss = c.getString("messages"); 
        persons.put(TAG_ID,tt); 
        } 
       if(id.equals("admin")&&ids.equals(session_id)) { 
        tt = c.getString("messages"); 
        persons.put(TAG_MESSAGE,ss); 
       } 





       personList.add(persons); 

      } 

      adapter = new SimpleAdapter(
        Messages.this, personList, R.layout.activity_message,new String[]{TAG_ID,TAG_MESSAGE},new int[]{R.id.id,R.id.messag}); 

      list.setAdapter(adapter); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     }