如何从列表视图中删除重复值?
问题描述:
我在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();
}
答
以前的数据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();
}
尝试在显示列表之前清除personList。 – KunalK
'tt'和'ss'代表什么,它们在哪里定义? 'id'和'ids'一样。尽量让你的变量名称更清晰。无论如何,目前还不清楚他们为什么看起来像田地 –