简体   繁体   中英

setListAdapter doesn't display my String-array in the ListActivity

list_item.xml: http://pastebin.com/bn56L3NN

What happens after onCreate() and after creating the Comm-object is that I get a "Connection established" message which gets picked up in another thread and I get the message in receiveMessage, I then send "list" and get called back to receiveMessage again.

I've checked with Log.v and I do get back the message that I want to list, my problem is that I can't display it in the ListActivity when I get to these lines, maybe I should replace them with something else?:

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

Full code:

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);
            }
        });
    }

}

You forgot to set a content view for your Activity in onCreate() which contains the layout for the Activity . So there is no list to display which could display anything. Define a layout in a layout XML file and set it as content view using setContentView() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM