Java 正在尝试使用JSON信息更新片段中的列表
所以我试图用JSON通信的服务器提供的信息更新一个列表 这是进行交流的班级:Java 正在尝试使用JSON信息更新片段中的列表,java,android,json,android-fragments,android-listview,Java,Android,Json,Android Fragments,Android Listview,所以我试图用JSON通信的服务器提供的信息更新一个列表 这是进行交流的班级: package com.example.simon_000.buddy; public class TCPConnection { private RunOnThread thread; private Receive receive; private MainActivity ma; private Socket socket; private DataInputStream input; private
package com.example.simon_000.buddy;
public class TCPConnection {
private RunOnThread thread;
private Receive receive;
private MainActivity ma;
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
private InetAddress address;
private int connectionPort;
private String ip;
private Exception exception;
public static String id;
public static ArrayList<members> memberList = new ArrayList<members>();
public static ArrayList<String> groupsList = new ArrayList<String>();
public static ArrayList<String> namesList = new ArrayList<String>();
public TCPConnection(String ip, int connectionPort, MainActivity ma) {
this.ip = ip;
this.connectionPort = connectionPort;
thread = new RunOnThread();
this.ma = ma;
}
public void connect() {
thread.start();
thread.execute(new Connect());
}
public void disconnect() {
thread.execute(new Disconnect());
}
public void send(String expression) {
thread.execute(new Send(expression));
}
private class Receive extends Thread {
public void run() {
String result;
try {
while (receive != null) {
result = (String) input.readUTF();
newMessage(result);
}
} catch (Exception e) { // IOException, ClassNotFoundException
receive = null;
}
}
}
public void newMessage(final String answer) {
ma.runOnUiThread(new Runnable() {
public void run() {
String message = answer;
String type;
JSONObject jObj = null;
try {
Log.d("TEST", message);
jObj = new JSONObject(message);
type = jObj.getString("type");
if (type.equals("groups")) {
recevieGroups(jObj);
}
else if (type.equals("register")) {
recevieID(jObj);
}
else if (type.equals("members")) {
receiveMembers(jObj);
}
else if (type.equals("location")) {
receiveLocations(jObj);
} else if (type.equals("locations")) {
receiveLocations(jObj);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void receiveLocations(JSONObject jObj) throws JSONException {
// { ”type”:”locations”, ”group”:”NAME”, ”locations”:[ {”member”:”NAME”, ”longitude”:”LONGITUDE”, ”latitude”:”LATITUDE” }, … ] }
JSONArray jArray = jObj.getJSONArray("locations");
for (int i = 0; i < jArray.length(); i++) {
members m = new members();
JSONObject jRealObject = jArray.getJSONObject(i);
m.setName(jRealObject.getString("member"));
m.setLongitude(Double.parseDouble(jRealObject.getString("longitude")));
m.setLatitude(Double.parseDouble(jRealObject.getString("latitude")));
memberList.add(m);
Log.d("TEST", " memberNAMES : " + m.getName()+" lng: "+ m.getLongitude()+" lat: "+m.getLatitude());
}
ma.updateMapMarkers(memberList);
}
public void receiveMembers(JSONObject jObj) throws JSONException {
// { “type”:”members”, “group”:”NAME”, “members”:[ {“member”:”NAME”},…] }
JSONArray jArray = jObj.getJSONArray("members");
for (int i = 0; i < jArray.length(); i++) {
String n;
JSONObject jRealObject = jArray.getJSONObject(i);
n = jRealObject.getString("member");
Log.d("TEST", " MembernamesBEFORE_ADD : " + n );
namesList.add(n);
Log.d("TEST", " memberNAMES : " + n);
}
}
public void recevieID(JSONObject jObj) throws JSONException {
id = (jObj.getString("id"));
Log.d("TEST", " ID : " + id);
}
public void recevieGroups(JSONObject jObj) throws JSONException {
JSONArray jArray = jObj.getJSONArray("groups");
for (int i = 0; i < jArray.length(); i++) {
String g;
JSONObject jRealObject = jArray.getJSONObject(i);
g = (jRealObject.getString("group"));
groupsList.add(g);
Log.d("TEST", " groupNames : " + g);
}
}
public Exception getException() {
Exception result = exception;
exception = null;
return result;
}
private class Connect implements Runnable {
public void run() {
try {
Log.d("TCPConnection", "Connect-run");
address = InetAddress.getByName(ip);
Log.d("TCPConnection-Connect", "Skapar socket");
socket = new Socket(address, connectionPort);
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
output.flush();
Log.d("TCPConnection-Connect", "Strömmar klara");
newMessage("CONNECTED");
receive = new Receive();
receive.start();
} catch (Exception e) { // SocketException, UnknownHostException
Log.d("TCPConnection-Connect", e.toString());
exception = e;
newMessage("EXCEPTION");
}
}
}
public class Disconnect implements Runnable {
public void run() {
try {
if (socket != null)
socket.close();
if (input != null)
input.close();
if (output != null)
output.close();
thread.stop();
newMessage("CLOSED");
} catch (IOException e) {
exception = e;
newMessage("EXCEPTION");
}
}
}
public class Send implements Runnable {
private String exp;
public Send(String exp) {
this.exp = exp;
}
public void run() {
try {
output.writeUTF(exp);
output.flush();
} catch (IOException e) {
exception = e;
newMessage("EXCEPTION");
}
}
}
}
必须从UI线程调用适配器.notifyDataSetChanged()。目前它是从后台线程调用的 因此,不是:
adapter.notifyDataSetChanged();
你应该写:
yourActivity.runOnUiThread(new Runnable(){
public void run() {
adapter.notifyDataSetChanged();
}
});
从后台线程调用该方法时。
这允许ListView使用连接到已更改的适配器的数据进行正确更新
10-27 17:49:00.168 24903-24903/com.example.simon_000.buddy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.simon_000.buddy, PID: 24903
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131034141, class android.widget.ListView) with Adapter(class com.example.simon_000.buddy.customs.GroupAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1555)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3624)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3436)
at android.view.View.dispatchTouchEvent(View.java:7713)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
at android.view.View.dispatchPointerEvent(View.java:7893)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3950)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3829)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3521)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5535)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5515)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5486)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5615)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.j
adapter.notifyDataSetChanged();
yourActivity.runOnUiThread(new Runnable(){
public void run() {
adapter.notifyDataSetChanged();
}
});