从其他线程调用时,ListView不显示

时间:2013-11-09 06:52:21

标签: android multithreading listview

我试图从不同的帖子中显示ListView,但不确定我在这里缺少什么。这是handleMessage()方法,我也试图创建一个计时器和runOnUiThread()方案,但也无法显示任何内容。我确信列表视图本身没有问题,因为如果我再次调用整个活动,它就可以工作(但是,不要使用该设计)。非常感谢任何提示...

public void ShowList(String[] list)
{
    for (String val : list)
        Log.d(TAG, "ShowList: list = " +val);

    // Set up the list view
    GuideAdapter = new ArrayAdapter<String>(this, R.layout.main, android.R.id.list, list);

    setListAdapter(GuideAdapter);
    Log.d(TAG, "ShowList: Created setListAdapter...");

    // Get an object for the list
    ListView listView = getListView();
    listView.setTextFilterEnabled(true);
    listView.setCacheColorHint(0);

    listView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
        }
    });
}

Handler mHandler = new Handler(new Handler.Callback()
{
    @Override
    public boolean handleMessage(Message msg)
    {
        if (msg.what == STATE_GUIDE) {              
        ShowList(ChannelList);
        }

    return true;
    }
});

public class MyThread extends Thread
{
    public MyThread()   {
    }

    public void run()   {
        ...
        mHandler.sendEmptyMessageDelayed(STATE_GUIDE, 0);
    }
}

1 个答案:

答案 0 :(得分:0)

更改以下代码

// Set up the list view
GuideAdapter = new ArrayAdapter<String>(this, R.layout.main, android.R.id.list, list);

以下

// Set up the list view
GuideAdapter = new ArrayAdapter<String>(this, android.R.id.list, list);

不要调用ShowList(ChannelList);在你的处理程序上。在onCreate方法上调用它。由于在单个活动中无法多次调用ArrayAdapter。