如何从列表视图中删除重复值?

how to remove Duplicate Values from listview?

我在 android 应用程序中工作,我必须在其中显示来自 json 的数据到列表视图。现在我从 json 获取数据,但我在列表视图中获取重复值。我该如何解决这个问题?

JSON 值

{"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();
}

这就是我得到的

Clear your arraylist 之前的数据,然后在 it.Check 中添加新数据以获取列表大小,如果它大于零,请清除您的数组列表,然后在列表中添加 HashMap。

像这样

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

我没有得到正确的解决方案,但我已经通过更改 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();
        }