如何解决setListAdapter不在ListActivity中显示我的String数组
|| list_item.xml:http://pastebin.com/bn56L3NN 在onCreate()之后和创建Comm对象之后发生的事情是,我收到一个“建立连接”消息,该消息在另一个线程中被拾取,并且在ReceiveMessage中得到该消息,然后发送“ list”并得到再次调用回receiveMessage。 我已经检查过Log.v,但确实得到了我想列出的消息,但问题是当我到达这些行时我无法在ListActivity中显示它,也许我应该将它们替换为某些内容其他?:setlistadapter(new ArrayAdapter<String>(this,R.layout.list_item,userRooms));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
完整代码:
package elf.app;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import elf.app.comm.CommClient;
import elf.app.comm.CommListener;
import elf.app.entity.ELFList;
import elf.app.entity.Entry;
public class RoomListActivity extends ListActivity implements CommListener {
private ELFList eList;
private String[] userRooms;
private CommClient comm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
eList = new ELFList();
comm = new CommClient( getIntent().getExtras().getString(\"ip\"),getIntent().getExtras().getInt(\"port\") );
comm.setListener(this);
new Thread(comm).start();
}
public void receiveMessage(String IP,String message,int id) {
if(message.equals(\"Connection established\")) {
comm.send(\"list\");
}
if(message.charat(0)==\'#\') {
String[] strArr = toStringarr(message);
eList.add(strArr);
listItems();
}
}
public String[] toStringarr(String str) {
String substr = str.substring(1);
return substr.split(\"@\");
}
public void listItems() {
userRooms = eList.returnNames();
setlistadapter(new ArrayAdapter<String>(this,userRooms));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setonItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
Entry e = eList.getEntry(position);
String roominfo = e.toString();
Intent intent = new Intent(RoomListActivity.this,RoomInfoActivity.class);
intent.putExtra(\"entry\",roominfo);
intent.putExtra(\"ip\",getIntent().getExtras().getString(\"ip\"));
intent.putExtra(\"port\",getIntent().getExtras().getInt(\"port\"));
comm.disconnect();
RoomListActivity.this.startActivity(intent);
}
});
}
}
解决方法
您忘记在
onCreate()
中为Activity
设置内容视图,其中包含Activity
的布局。因此,没有要显示的列表可以显示任何内容。在布局XML文件中定义一个布局,然后使用setContentView()
将其设置为内容视图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。