有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java setListAdapter不在ListActivity中显示我的Stringarray

列出你的项目。xml:http://pastebin.com/bn56L3NN

在onCreate()和创建Comm对象之后发生的事情是,我得到一条“连接已建立”消息,该消息在另一个线程中被拾取,我在receiveMessage中得到该消息,然后我发送“list”,并再次被回调到receiveMessage

我查过日志了。v和我确实收到了我想要列出的消息,我的问题是当我到达这些行时,我不能在ListActivity中显示它,也许我应该用其他内容替换它们:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, userRooms));
ListView lv = getListView();
lv.setTextFilterEnabled(true);

完整代码:

package elf.app;

import 安卓.app.ListActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemClickListener;
import 安卓.widget.ArrayAdapter;
import 安卓.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) 个答案

  1. # 1 楼答案

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