我想是时候问我的stackoverbros.一个问题了。我使用socket.io-client.java https://github.com/nkzawa/socket.io-client.java与使用socket.io的节点服务器通信。到目前为止,连接工作正常。
这里是onCreate和变量定义。
class Text
{
String username;
String message;
}
Socket socket;
ListView listView;
ArrayList<Text> texts = new ArrayList<Text>();
String username = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
initList();
activateEnter();
try {
startSocket();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
创建套接字的函数。
private void startSocket() throws URISyntaxException
{
socket = IO.socket("http://localhost");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener()
{
@Override
public void call(Object... args)
{
socket.emit("adduser", "");
}
}).on("chat_msg", new Emitter.Listener()
{
@Override
public void call(Object... args)
{
JSONObject obj = (JSONObject)args[0];
try {
String uname = obj.getString("username");
String msg = obj.getString("msg");
updateList(uname, msg);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).on("username", new Emitter.Listener()
{
@Override
public void call(Object... args)
{
JSONObject obj = (JSONObject)args[0];
try {
username = obj.getString("username");
} catch (JSONException e) {
e.printStackTrace();
}
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener()
{
@Override
public void call(Object... args)
{
}
});
socket.connect();
}
现在,我有了一个聊天功能,聊天消息进入一个ListView与一个自定义适配器,我创建的套接字函数之外,像这样。
class FancyAdapter extends ArrayAdapter<Text>
{
FancyAdapter()
{
super(MainActivity.this, android.R.layout.simple_list_item_1, texts);
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.text, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(texts.get(position));
return(convertView);
}
}
class ViewHolder
{
public TextView text_username = null;
public TextView text_message = null;
ViewHolder(View row)
{
text_username = (TextView) row.findViewById(R.id.text_username);
text_message = (TextView) row.findViewById(R.id.text_message);
}
void populateFrom(Text w)
{
text_username.setText(w.username);
text_message.setText(w.message);
}
}
FancyAdapter adapter = null;
private void initList()
{
adapter = new FancyAdapter();
listView.setAdapter(adapter);
}
private void updateList(String uname, String message)
{
Text text = new Text();
text.username = uname;
text.message = message;
texts.add(text);
adapter.notifyDataSetChanged();
}
现在,当我调用updateList(string, string)从套接字调用,特别是"chat_message",我得到"ViewRootImpl$ calledfromwrongthreadeexception只有原来的线程,创建一个视图层次结构可以触摸它的视图。"
哦,好吧,我试着在套接字函数的EVENT_CONNECT事件中移动列表/适配器相关的函数和类,但似乎在那里定义的东西不会为"chat_message"等其他事件定义。
所以我不知道该怎么做。
你只能通过UI线程与视图交互。
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
updateList(uname, msg);
}
});