繁体   English   中英

Listview不使用排球库更新

[英]Listview not updating using volley library

大家好,我创建了一个聊天应用程序并让用户在一个listview中聊天数据。该用户数据由volley库使用json调用,所有这些都放置在一个方法中,并且此方法放置在处理程序中,并且每5个刷新一次此处理程序seconds。但是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;
    }
}

问题,我看到您在活动中仅调用一次chatMethod() 因此,请将您的chatMethod()函数放入处理程序中,并使处理程序每​​5秒钟重复一次。

chatmethod();

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

另外,您还需要在Volley调用结果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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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