Devraj Devraj - 1 month ago 5
Android Question

repeatation of data in Listview after calling api everytime

I am calling api to fetch data from server and populating the data into Listview using adapter but data gets double itself everytime api getting called. ie:- on first time api call, list shows data like.

A
B
C


on second time api call, list gets data like..

A
B
C
A
B
C


here is my Fragment:

String GetChatURL = RECEIVING_URL+"receiptID="+userId+"&senderID="+recepientID;

StringRequest stringRequest = new StringRequest(Request.Method.GET, GetChatURL,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {

Log.d("respose valuee",response);

try {

JSONArray jsonArray = new JSONArray(response);

JSONObject jsonObject = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
for(int i = 0;i<=jsonArray.length();i++) {

final ChatReceivingBean chatReceivingBean = new ChatReceivingBean();

jsonObject = jsonArray.getJSONObject(i);

chatReceivingBean.setMessageText(jsonObject.getString("MessageText"));
chatReceivingBean.setSenderID (jsonObject.getString ("senderID"));

chatArrayList.add (chatReceivingBean);
String id = chatReceivingBean.getSenderID ();

adapter = new ChatAdapter (getActivity(),chatArrayList,id);
messagesContainer.setAdapter(adapter);

scroll();


}

Log.e("stringgggg ",chatArrayList.toString ());

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


}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.toString(),Toast.LENGTH_LONG ).show();
}
})

{


@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String,String>();
map.put(KEY_CHAT_RECEIVE_USERID,userId);
map.put(KEY_CHAT_RECEIVE_SENDERID,recepientID);


return map;
}
};

try {
RequestQueue requestQueue = Volley.newRequestQueue (getActivity ());
requestQueue.add (stringRequest);
}catch (Exception e){

}


Here is Adapter class:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

sharedPreferences = context.getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);

recepientID = sharedPreferences.getString("key_id",null);
userId = sharedPreferences.getString("key_name",null);

Log.d ("recepientID",recepientID);
Log.d ("userId",userId);

String iid = chatReceivingBeen.get (position).getSenderID ();

View v=convertView;
if(v==null)
{

if(chatReceivingBeen.get (position).getSenderID().equalsIgnoreCase(recepientID)){

//v = View.inflate(cxt, R.layout.right, null);
v= View.inflate (context, R.layout.list_item_chat_other,null);

}else {
// v = View.inflate(cxt, R.layout.left, null);
v= View.inflate (context, R.layout.list_item_chat,null);
}
}

txtMessage = (TextView)v.findViewById(R .id.txtMessage);

if(chatReceivingBeen!=null) {

// holder.txtMessage.setText (chatReceivingBeen.get (position).getMessageText ());
txtMessage.setText (chatReceivingBeen.get (position).getMessageText ());
}


return v;
}

Answer

clear chatArrayList before adding to it:

  StringRequest stringRequest = new StringRequest(Request.Method.GET, GetChatURL,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("respose valuee",response);

                    try {

                        //clear
                        chatArrayList.clear()
                        JSONArray jsonArray = new JSONArray(response);

                        JSONObject jsonObject = new JSONObject();
                        JSONObject jsonObject1 = new JSONObject();
                        ....