我正在调用api从服务器获取数据并使用适配器将数据填充到Listview中,但每次调用api时数据都会自动加倍。即: - 在第一次api呼叫时,列表显示数据,如。
A
B
C
在第二次api调用时,list获取类似..
的数据A
B
C
A
B
C
这是我的片段:
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){
}
这是Adapter类:
@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;
}
答案 0 :(得分:1)
清除chatArrayList
:
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();
....