简体   繁体   中英

Listview not updating using volley library

hi everyone i had creating a chating app and getting the users chating data in one listview.this usersdata is called using json by volley library and all these are placed in a method and this method is placed in handler and this handler is refreshed for every 5 seconds.but the listview adapter is not updating.I had struck in this nearly from 20days please anybody help me out i am not able to understand what had i done wrong

suppose listview contains 5 items and when i add an 6item this item is not updating in 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;
    }
}

problem I see you are calling chatMethod() only once at your activity. So please put your chatMethod() function inside in your handler and make your handler repetitive for every 5 second.

chatmethod();

handler.postDelayed( new Runnable() {
        @Override
        public void run() {
            chatmethod();
            handler.postDelayed( this,5000 );
        }
    },5000 );

Also you need to move your notifyDataSetChanged() to onResponse at your Volley call result. Because it will refresh your list view after succesfull Voley result.

           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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM