Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ChatApp--适配器未更新_Java_Android_Listview_Adapter_Baseadapter - Fatal编程技术网

Java ChatApp--适配器未更新

Java ChatApp--适配器未更新,java,android,listview,adapter,baseadapter,Java,Android,Listview,Adapter,Baseadapter,我正在使用聊天应用程序,我需要每一秒钟刷新一次列表。我正在使用该处理程序重复调用该方法,但它没有更新ListView。我正在调用在该方法中更改的notify数据集 Messages.java public class Messages extends Activity { ListView chatview; ChatAdapter chatAdapter; List<CBData> chatdata; public int APP_REFRESH_T

我正在使用聊天应用程序,我需要每一秒钟刷新一次列表。我正在使用该处理程序重复调用该方法,但它没有更新
ListView
。我正在调用在该方法中更改的notify数据集

Messages.java

public class Messages extends Activity {
    ListView chatview;
    ChatAdapter chatAdapter;
    List<CBData> chatdata;
    public int APP_REFRESH_TIME_IN_SEC = 1;
    Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_details);
        chatview = (ListView) findViewById(R.id.chatview);
        chatdata = new ArrayList<CBData>();
        chatAdapter = new ChatAdapter(getApplicationContext(), chatdata);
        chatview.setAdapter(chatAdapter);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                chatmethod();
                handler.postDelayed(this, APP_REFRESH_TIME_IN_SEC * 1000);
            }
        }, APP_REFRESH_TIME_IN_SEC * 1000);

    }

    private void chatmethod() {
        chatdata.clear();
        String receiverid = getIntent().getStringExtra("ReceiverID");
        String chaturl = Constant.URL + "chathome.php?sid=" + Session.getUserID(getApplicationContext()) + "&rid=" + receiverid;
        Display.displaylog("Chat", chaturl);
        JsonObjectRequest chatreq = new JsonObjectRequest(Request.Method.GET, chaturl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray chatsarray = response.getJSONArray("chats");
                    for (int i = 0; i < chatsarray.length(); i++) {
                        JSONObject chatobj = chatsarray.getJSONObject(i);
                        CBData cbchat = new CBData();
                        cbchat.setOwerid(chatobj.getString("sender_id"));
                        cbchat.setChatmessage(chatobj.getString("message"));
                        chatdata.add(cbchat);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                chatAdapter.notifyDataSetChanged();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Display.displaylog("ChatError", String.valueOf(error));
            }
        });
        chatreq.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(chatreq);
    }
}
公共类消息扩展活动{
列表视图;
聊天适配器聊天适配器;
列出数据;
公共int应用程序刷新时间秒=1;
按钮2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_详细信息);
chatview=(ListView)findViewById(R.id.chatview);
chatdata=newarraylist();
chatAdapter=新的chatAdapter(getApplicationContext(),chatdata);
setAdapter(chatAdapter);
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
方法();
handler.postDelayed(此为应用程序刷新时间,单位为秒*1000);
}
},应用程序刷新时间(秒*1000);
}
私有方法(){
chatdata.clear();
字符串receiverid=getIntent().getStringExtra(“receiverid”);
字符串chaturl=Constant.URL+“chathome.php?sid=“+Session.getUserID(getApplicationContext())+”&rid=“+receiverid;
Display.displaylog(“聊天”,chaturl);
JsonObjectRequest chatreq=newJSONObjectRequest(Request.Method.GET,chaturl,(String)null,new Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray chatsarray=response.getJSONArray(“chats”);
对于(int i=0;i
ChatAdapter.java

    public class ChatAdapter extends BaseAdapter {
    List<CBData> chatlist;
    Context chatcontext;
    LayoutInflater chatinflater;

    public ChatAdapter(Context chatcontext, List<CBData> chatlist) {
        this.chatcontext = chatcontext;
        this.chatlist = chatlist;
    }

    @Override
    public int getCount() {
        return chatlist.size();
    }

    @Override
    public Object getItem(int position) {
        return chatlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        chatinflater = (LayoutInflater) chatcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Chatholder chatholder;
        final CBData chatdatalist = chatlist.get(position);
        String owerid = Session.getUserID(chatcontext);
        if (convertView == null) {
            chatholder=new Chatholder();
            final boolean isMe = chatdatalist.getOwerid().equals(owerid);
            if (isMe) {
                convertView = chatinflater.inflate(R.layout.chatright, parent, false);
                chatholder.chatbubble = (TextView) convertView.findViewById(R.id.chatbubble);
                convertView.setTag(chatholder);
            } else {
                convertView = chatinflater.inflate(R.layout.chatleft, parent, false);
                chatholder.chatbubble = (TextView) convertView.findViewById(R.id.chatbubble);
                convertView.setTag(chatholder);
            }
        }else {
            chatholder= (Chatholder) convertView.getTag();
        }
        chatholder.chatbubble.setText(chatdatalist.getChatmessage());
//        TextView chatbubble = (TextView) convertView.findViewById(R.id.chatbubble);

        return convertView;
    }

    static class Chatholder {
        TextView chatbubble;
    }
}
公共类ChatAdapter扩展了BaseAdapter{
列表聊天列表;
语境;
充气机;
公共聊天适配器(上下文聊天上下文,列表聊天列表){
this.chatcontext=chatcontext;
this.chatlist=聊天列表;
}
@凌驾
public int getCount(){
返回chatlist.size();
}
@凌驾
公共对象getItem(int位置){
返回聊天列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
chatinflater=(LayoutFlater)chatcontext.getSystemService(Context.LAYOUT\u INFLATER\u服务);
聊天室管理员聊天室管理员;
最终CBData chatdatalist=chatlist.get(位置);
字符串owerid=Session.getUserID(chatcontext);
if(convertView==null){
chatholder=新的chatholder();
最后一个布尔值isMe=chatdatalist.getOwerid().equals(owerid);
如果(isMe){
convertView=chatinflater.充气(R.layout.chatright,父项,false);
chatholder.chatbuble=(TextView)convertView.findViewById(R.id.chatbuble);
convertView.setTag(聊天室持有者);
}否则{
convertView=chatinflater.inflate(R.layout.chatleft,父项,false);
chatholder.chatbuble=(TextView)convertView.findViewById(R.id.chatbuble);
convertView.setTag(聊天室持有者);
}
}否则{
chatholder=(chatholder)convertView.getTag();
}
chatholder.chatbuble.setText(chatdatalist.getChatmessage());
//TextView chatbuble=(TextView)convertView.findViewById(R.id.chatbuble);
返回视图;
}
静态类聊天室{
文本视图聊天泡泡;
}
}
更新的Message.java

public class Messages extends Activity {
    ListView chatview;
    ChatAdapter chatAdapter;
    List<CBData> chatdata;
    public int APP_REFRESH_TIME_IN_SEC = 1;
    Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_details);
        chatview = (ListView) findViewById(R.id.chatview);
        chatdata = new ArrayList<CBData>();
        chatAdapter = new ChatAdapter(getApplicationContext(), chatdata);
        chatview.setAdapter(chatAdapter);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                chatmethod();
                handler.postDelayed(this, APP_REFRESH_TIME_IN_SEC * 1000);
            }
        }, APP_REFRESH_TIME_IN_SEC * 1000);

    }

    private void chatmethod() {
        chatdata = new ArrayList<CBData>();
        String receiverid = getIntent().getStringExtra("ReceiverID");
        String chaturl = Constant.URL + "chathome.php?sid=" + Session.getUserID(getApplicationContext()) + "&rid=" + receiverid;
        Display.displaylog("Chat", chaturl);
        JsonObjectRequest chatreq = new JsonObjectRequest(Request.Method.GET, chaturl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray chatsarray = response.getJSONArray("chats");
                    for (int i = 0; i < chatsarray.length(); i++) {
                        JSONObject chatobj = chatsarray.getJSONObject(i);
                        CBData cbchat = new CBData();
                        cbchat.setOwerid(chatobj.getString("sender_id"));
                        cbchat.setChatmessage(chatobj.getString("message"));
                        chatdata.add(cbchat);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                chatAdapter = new ChatAdapter(getApplicationContext(), chatdata); 
                chatview.setAdapter(chatAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Display.displaylog("ChatError", String.valueOf(error));
            }
        });
        chatreq.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(chatreq);
    }
}
公共类消息扩展活动{
列表视图;
聊天适配器聊天适配器;
列出数据;
公共int应用程序刷新时间秒=1;
按钮2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_详细信息);
chatview=(ListView)findViewById(R.id.chatview);
chatdata=newarraylist();
chatAdapter=新的chatAdapter(getApplicationContext(),chatdata);
setAdapter(chatAdapter);
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
方法();
handler.postDelayed(此为应用程序刷新时间,单位为秒*1000);
}
},应用程序刷新时间(秒*1000);
}
私有方法(){
chatdata=newarraylist();
字符串receiverid=getIntent().getStringExtra(“receiverid”);
字符串chaturl=Constant.URL+“chathome.php?sid=“+Session.getUserID(getApplicationContext())+”&rid=“+receiverid;
Display.displaylog(“聊天”,chaturl);
JsonObjectRequest chatreq=n
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/colorPrimary"
        android:dividerHeight="2dp"
        android:id="@+id/chatview" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/chatbubble"
        android:gravity="left"
        android:textColor="#000"
        android:padding="10dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/chat_bg"
        />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/chatbubble"
        android:layout_gravity="right"
        android:textColor="#000"
        android:padding="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/chat_bg"
        />
</LinearLayout>
 ((BaseAdapter) chatview.getAdapter()).notifyDataSetChanged(); 
public void reload(List<CBData> chatlist) {
        this.chatlist=chatlist;
        notifyDataSetChanged();
    }
chatAdapter.reload(chatdata);