【发布时间】:2017-11-23 06:06:24
【问题描述】:
我正在尝试创建一个包含两个活动的聊天应用程序。第一个计划是一个简短列表,其中包含每行中最后发送的消息。第二个活动包含所有对话。我使用socket.io,我的问题是,当我点击后退按钮然后我回到我的应用程序时,notifyDataSetChanged() 停止工作。我的应用程序从与 android 应用程序合作的网站的对话框中接收消息。在控制台日志中,我看到收到了来自网站的消息,并且调用了 onTaskComplete() 方法,但列表视图没有刷新。我读到适配器在重新启动活动后丢失了对列表视图的引用,但是如何在我的代码中查看我在 onResume() 方法中创建了一个新适配器。我不明白我做错了什么? 而如果我将抛出超出条件“mSocket.on(“message”,onMessage);”然后返回应用程序后刷新列表视图,但消息的次数是返回次数的倍数。
下面是我的代码,请帮忙!
主活动:
package com.example.seadog.fb_dialog;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends Activity implements MyListener {
public ListView listView;
public MyBaseAdapter adapter;
public TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Get Socket.io Object
*/
SocketIO socketIo = new SocketIO();
Socket mSocket = socketIo.getSocket(); // get socket
Integer id = socketIo.getId(); // get Website ID
if(mSocket == null) {
socketIo.Connection();
mSocket = socketIo.getSocket();
mSocket.on("message", onMessage);
}
listView = (ListView) findViewById(R.id.listView);
/*
* OnItemClickListener
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Conversation.class);
intent.putExtra("item", position);
startActivity(intent);
TextView textView = (TextView) view.findViewById(R.id.descitem);
textView.setTypeface(null, Typeface.NORMAL);
}
});
textView = (TextView) findViewById(R.id.count);
}
private Emitter.Listener onMessage = new Emitter.Listener() {
/*
* Message Listener
*/
@Override
public void call(Object... args) {
Boolean isset = false;
try {
JSONObject object = (JSONObject) args[0];
String _id = object.getString("_id");
String message = object.getString("message");
JSONObject obj = new JSONObject();
obj.put("direction", "fb-lt");
obj.put("message", message);
obj.put("date", "2017-05-29T12:15:49.245Z");
for(int i = 0; i < arrayList.size(); i++){
ListData ld = (ListData) arrayList.get(i);
String id = ld.getId();
if(_id.equals(id)){
JSONArray Data = ld.getData();
Data.put(obj);
ld.setDescription(message);
arrayList.set(i, ld);
isset = true;
Log.d("LOG", message);
}
}
if(!isset) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj);
ListData ld = new ListData();
ld.set_id(_id);
ld.setID(1);
ld.setTitle("Klient:");
ld.setDescription(message);
ld.setData(jsonArray);
arrayList.add(ld);
}
onTaskComplete();
} catch (JSONException e) {
e.printStackTrace();
}
}
};
public void onResume() {
super.onResume();
ArrayList clone = (ArrayList) arrayList.clone();
arrayList.clear();
arrayList.addAll(clone);
adapter = new MyBaseAdapter(this, arrayList);
listView.setAdapter(adapter);
}
@Override
public void onTaskComplete() {
runOnUiThread(new Runnable() {
@Override
public void run () {
Log.d("LOG:","refresh");
adapter.notifyDataSetChanged();
}
});
}
}
接口 MyListener:
package com.example.seadog.fb_dialog;
import java.util.ArrayList;
public interface MyListener {
ArrayList arrayList = new ArrayList();
void onTaskComplete();
}
【问题讨论】:
-
您想在使用时刷新列表视图再次返回您的应用吗?
-
是的,因为当应用程序在后台运行时我会收到一条新消息时,列表视图无法刷新。 ArrayList 已更新,但 listview 尚未更新。所以在来到前台时,我想刷新一个列表视图。
标签: java android listview arraylist notifydatasetchanged