我在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();
}
答案 0 :(得分:1)
Clear your arraylist
之前的数据。检查列表的大小,如果大于零,请清除您的arraylist,然后在列表中添加HashMap。
像这样的东西
if(personList.size()>0)
personList.clear();
//now add your HashMap into list
答案 1 :(得分: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();
}