setListAdapter不会在ListActivity中显示我的String-array

时间:2011-05-23 12:45:19

标签: java android eclipse render listactivity

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, R.layout.list_item, 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);
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

您忘记为Activity中的onCreate()设置内容视图,其中包含Activity的布局。因此没有可显示的列表可以显示任何内容。在布局XML文件中定义布局,并使用setContentView()将其设置为内容视图。