大家好我创建了一个聊天应用程序并让用户在一个listview中查看数据。这个用户数据是使用json by volley库调用的,所有这些都放在一个方法中,这个方法放在处理程序中,这个处理程序被刷新每5秒钟一次。但是listview适配器没有更新。我几乎从20天开始接触了这个请任何人帮助我,我无法理解我做错了什么
假设listview包含5个项目,当我添加6item时,此项目未在listview中更新。
ChatList.java
public class Messages extends ActionBarActivity {
ListView chatview;
List<CBData> chatdata;
TextView sendbutton;
EditText msget;
String message;
ChatAdapter_Row chatAdapter;
ImageButton msg_menu, videobutton;
ImageView callbutton;
TextView name, viewprofile;
ImageView backbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_details);
chatview = (ListView) findViewById(R.id.chatview);
sendbutton = (TextView) findViewById(R.id.sendbutton);
msget = (EditText) findViewById(R.id.msget);
chatdata = new ArrayList<CBData>();
chatAdapter = new ChatAdapter_Row(Messages.this, chatdata);
chatview.setStackFromBottom(true);
chatview.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
chatview.setAdapter(chatAdapter);
sendbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
message = msget.getText().toString();
sendmethod();
}
});
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
@Override
public void run() {
chatAdapter.notifyDataSetChanged();
handler.postDelayed( this,5000 );
}
},5000 );
chatmethod();
}
private void chatmethod() {
String receiverid = getIntent().getStringExtra("ReceiverID");
String chaturl = Constant.URL + "chathome.php?sid=" + Session.getUserID(getApplicationContext()) + "&rid=" + receiverid;
Display.displaylog("ChatUrl", chaturl);
JsonObjectRequest chatobj = new JsonObjectRequest(Request.Method.GET, chaturl, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray chatarray = response.getJSONArray("chats");
for (int i = 0; i < chatarray.length(); i++) {
JSONObject chatjsonobj = chatarray.getJSONObject(i);
CBData cbData = new CBData();
cbData.setOwerid(chatjsonobj.getString("sender_id"));
cbData.setChatmsg(chatjsonobj.getString("message"));
cbData.setChatid(chatjsonobj.getString("chat_id"));
cbData.setChatsendername(chatjsonobj.getString("sender_first_name"));
cbData.setChattype(chatjsonobj.getString("chat_type"));
cbData.setChatsenderlastname(chatjsonobj.getString("sender_last_name"));
chatdata.add(cbData);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Display.displaylog("Chat", String.valueOf(error));
}
});
chatobj.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(chatobj);
}
}
ChatListAdapter.java
public class ChatAdapter_Row extends BaseAdapter {
Context chatcontext;
List<CBData> chatadaprow;
LayoutInflater inflater;
public ChatAdapter_Row(Context chatcontext, List<CBData> chatadaprow) {
this.chatcontext = chatcontext;
this.chatadaprow = chatadaprow;
}
@Override
public int getCount() {
return chatadaprow.size();
}
@Override
public Object getItem(int position) {
return chatadaprow.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) chatcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Chathold chathold;
if (convertView == null) {
convertView = inflater.inflate(R.layout.chat_row, parent, false);
chathold = new Chathold();
chathold.betweenname= (TextView) convertView.findViewById(R.id.betweenname);
chathold.msg= (TextView) convertView.findViewById(R.id.msg);
chathold.chatimage= (ImageView) convertView.findViewById(R.id.chatimage);
convertView.setTag(chathold);
}else {
chathold= (Chathold) convertView.getTag();
}
CBData chatdata=chatadaprow.get(position);
String cont=chatdata.getChatsendername()+" "+chatdata.getChatsenderlastname();
chathold.betweenname.setText(cont);
String chattype=chatdata.getChattype();
if (chattype.equals("text")){
chathold.msg.setText(chatdata.getChatmsg());
}else if (chattype.equals("image")){
chathold.msg.setVisibility(View.GONE);
chathold.chatimage.setVisibility(View.VISIBLE);
Glide.with(chatcontext).load(chatdata.getChatmsg()).into(chathold.chatimage);
}
return convertView;
}
static class Chathold {
TextView betweenname, msg;
ImageView chatimage;
}
}
答案 0 :(得分:0)
问题我发现您只在活动中致电chatMethod()
一次。因此,请将您的chatMethod()
函数放入处理程序中,并使您的处理程序每5秒钟重复一次。
chatmethod();
handler.postDelayed( new Runnable() {
@Override
public void run() {
chatmethod();
handler.postDelayed( this,5000 );
}
},5000 );
此外,您需要将notifyDataSetChanged()
移至onResponse
,即可获得您的排球通话结果。因为它会在成功的Voley结果后刷新列表视图。
try {
JSONArray chatarray = response.getJSONArray("chats");
for (int i = 0; i < chatarray.length(); i++) {
JSONObject chatjsonobj = chatarray.getJSONObject(i);
CBData cbData = new CBData();
cbData.setOwerid(chatjsonobj.getString("sender_id"));
cbData.setChatmsg(chatjsonobj.getString("message"));
cbData.setChatid(chatjsonobj.getString("chat_id"));
cbData.setChatsendername(chatjsonobj.getString("sender_first_name"));
cbData.setChattype(chatjsonobj.getString("chat_type"));
cbData.setChatsenderlastname(chatjsonobj.getString("sender_last_name"));
chatdata.add(cbData);
}
} catch (JSONException e) {
e.printStackTrace();
}
// Important part
chatAdapter.notifyDataSetChanged();