选项卡更改后,使用新数据刷新Listview不起作用(Android)

时间:2012-10-24 14:09:41

标签: android listview refresh reload baseadapter

我在刷新ListView时遇到问题。我有一个TabActivity调用另一个TabActivity(SubTabActivity)调用ListActivity。 我的ListView从Web服务器获取XML文件中的数据。 在我的自定义标题中,我有一个按钮来重新加载/刷新ListView。 当我更改subTab时,我调用onTabChanged-Method以使用其他数据重绘List。

我现在的问题是我只能在进行第一次Tab Change之前刷新ListView。在我更改了Tap后,我只能刷新第二个列表。当我回到第一个列表时,我无法刷新它们。我只能从我的XML文件中获取新数据,但我无法显示它。 我试过invalidate(),invalidateViews()和notifyDataSetChanged(),但没有什么能解决这个bug。

我的AsyncTask要刷新

private static class ReloadNews extends AsyncTask<Void, Void, Void>
{   
    @Override
    protected void onPreExecute()
    {
        MenuActivity.startProgress();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        News.getNews(News.url);

        myref.runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
                // TODO Auto-generated method stub  
                if(SubTabMenuActivity.subMenuTabHost.getCurrentTabTag().equals("FestivalNews"))
                {
                    adapter = new NewsAdapter(myref, News.getFestivalNews());
                    newsList.setAdapter(adapter);
                }
                else if(SubTabMenuActivity.subMenuTabHost.getCurrentTabTag().equals("RennNews"))
                {
                    adapter = new NewsAdapter(myref, News.getRegattaNews());
                    newsList.setAdapter(adapter);
                }
            }   
        });     
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        MenuActivity.stopProgress();
    }
}

知道TabChange后它为什么不起作用?

修改

10-25 05:58:28.264: D/dalvikvm(272): GC_EXPLICIT freed 8K, 51% free 2725K/5511K,   external 410K/517K, paused 241ms
10-25 05:58:33.104: D/dalvikvm(310): GC_EXPLICIT freed 236K, 45% free 3366K/6087K, external 410K/517K, paused 53ms
10-25 05:58:35.281: D/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
10-25 05:58:38.163: D/dalvikvm(345): GC_EXPLICIT freed 106K, 44% free 3521K/6215K, external 410K/517K, paused 79ms
10-25 05:58:43.153: D/dalvikvm(360): GC_EXPLICIT freed 325K, 51% free 2720K/5511K, external 410K/517K, paused 54ms
10-25 05:58:57.233: I/System.out(433): ADaPter
10-25 05:58:57.243: I/System.out(433): ADaPter
10-25 05:58:57.263: I/System.out(433): ADaPter
10-25 05:58:57.273: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.273: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.293: I/System.out(433): ADaPter
10-25 05:58:57.303: I/System.out(433): ADaPter
10-25 05:58:57.313: I/System.out(433): ADaPter
10-25 05:58:57.323: I/System.out(433): ADaPter
10-25 05:58:57.333: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.333: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.433: I/System.out(433): ADaPter
10-25 05:58:57.453: I/System.out(433): ADaPter
10-25 05:58:57.483: I/System.out(433): ADaPter
10-25 05:58:57.493: D/dalvikvm(433): GC_CONCURRENT freed 1274K, 62% free 3322K/8647K, external 2400K/2689K, paused 4ms+5ms
10-25 05:58:57.523: I/System.out(433): ADaPter
10-25 05:58:57.533: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.533: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.553: I/System.out(433): ADaPter
10-25 05:58:57.563: I/System.out(433): ADaPter
10-25 05:58:57.573: I/System.out(433): ADaPter
10-25 05:58:57.593: I/System.out(433): ADaPter
10-25 05:58:57.593: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.603: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.623: I/System.out(433): ADaPter
10-25 05:58:57.633: I/System.out(433): ADaPter
10-25 05:58:57.643: I/System.out(433): ADaPter
10-25 05:58:57.653: I/System.out(433): ADaPter
10-25 05:58:57.663: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.663: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.693: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.693: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.693: I/System.out(433): ADaPter
10-25 05:58:57.713: I/System.out(433): ADaPter
10-25 05:58:57.723: I/System.out(433): ADaPter
10-25 05:58:57.733: I/System.out(433): ADaPter
10-25 05:58:57.743: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.743: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.793: I/System.out(433): ADaPter
10-25 05:58:57.803: I/System.out(433): ADaPter
10-25 05:58:57.813: I/System.out(433): ADaPter
10-25 05:58:57.813: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.813: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.933: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.933: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:57.943: I/System.out(433): ADaPter
10-25 05:58:58.003: D/dalvikvm(433): GC_CONCURRENT freed 890K, 61% free 3439K/8647K, external 2240K/2689K, paused 8ms+4ms
10-25 05:58:58.003: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.003: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.013: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.013: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.023: I/System.out(433): ADaPter
10-25 05:58:58.073: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.073: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.083: I/System.out(433): ADaPter
10-25 05:58:58.083: I/System.out(433): ADaPter
10-25 05:58:58.103: I/System.out(433): ADaPter
10-25 05:58:58.113: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.113: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.123: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.123: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.143: I/System.out(433): ADaPter
10-25 05:58:58.433: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.453: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.453: D/skia(433): --- SkImageDecoder::Factory returned null
10-25 05:58:58.453: D/skia(433): --- SkImageDecoder::Factory returned null

填充列表时以及在tabchange之前调用refresh时,这是我的LogCat正常。

以下logcat部分位于tabchange之后并再次调用refresh。

10-25 05:59:01.493: D/dalvikvm(433): GC_FOR_MALLOC freed 293K, 58% free 3686K/8647K, external 2240K/2689K, paused 30ms
10-25 05:59:01.493: I/dalvikvm-heap(433): Grow heap (frag case) to 8.655MB for 524304-byte allocation
10-25 05:59:01.523: D/dalvikvm(433): GC_FOR_MALLOC freed 256K, 58% free 3942K/9223K, external 2240K/2689K, paused 31ms
10-25 05:59:01.923: D/dalvikvm(433): GC_FOR_MALLOC freed 532K, 56% free 4094K/9223K, external 2240K/2689K, paused 27ms
10-25 05:59:02.313: D/dalvikvm(433): GC_CONCURRENT freed 862K, 54% free 4319K/9223K, external 2240K/2689K, paused 5ms+6ms
10-25 05:59:02.943: D/dalvikvm(433): GC_CONCURRENT freed 989K, 51% free 4609K/9223K, external 2240K/2689K, paused 7ms+3ms
10-25 05:59:03.473: D/dalvikvm(433): GC_CONCURRENT freed 1478K, 52% free 4447K/9223K, external 2240K/2689K, paused 7ms+7ms
10-25 05:59:03.783: W/InputManagerService(60): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4073bca0

我注意到适配器没有将数据加载到List中。如果我调用adapter.count();我可以看到他已经获得了新的数据,但他不再将它绘制到列表中了。

添加另一个logTag后,我注意到我的适配器的构造函数被调用但不是getView方法。我的ListView是否可能从适配器中失去焦点?

2 个答案:

答案 0 :(得分:0)

之后

 newsList.setAdapter(adapter);

添加:

adapter.notifyDataSetChanged();
祝你好运

答案 1 :(得分:0)

好的,我找到了自己的解决方案。我已将不同的列表拆分为不同的Activtiys,并且每个mainTab都创建了一个自己的subTab Activity。 我并不为这个解决方案感到自豪,但它确实有效。