0

我有TabHost的FrameLayout中单个ListView的TabActivity。这个ListView在我的3个选项卡中共享。列表中的项目是使用Handler从互联网上下载的,但是当数据到来时列表不会被更新。当我选择另一个选项卡时,列表会自动更新,但我希望能够在下载后看到它正在更新。我已经试图使ListView,FrameLayout和TabHost无效,但没有任何反应。 notifyDataSetChanged()和notifyDataSetInvalidated()方法也没有帮助。如果我把ListView放在FrameLayout之外,它可以按预期工作。下面的代码:无法更新TabHost的FrameLayout中的ListView

<?xml version="1.0" encoding="UTF-8"?> 

<TabHost android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_alignParentTop="true"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/comments_list_lv" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
     </FrameLayout> 
</LinearLayout> 

public class CommentsActivity extends TabActivity implements Handler.Callback, OnTabChangeListener { 

private static final int take = 20; 
private Handler handler; 
private CommentsListModel model = null; 
private ListView listView; 
private CommentsListAdapter adapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.comments_activity); 

    handler = new Handler(this); 

    setContentView(R.layout.comments_activity); 
    listView = (ListView) findViewById(R.id.comments_list_lv); 
    listView.setTextFilterEnabled(true); 
    getTabHost().setOnTabChangedListener(this); 

    buildTabs(); 
    setListView(); 
    requestData(); 
} 


@Override 
public boolean handleMessage(Message msg) { 
    AbstractResponse response = (AbstractResponse) msg.obj; 
    ResponseStatus status = response.getStatus(); 

    if (status.getResponseCode() == ResponseStatus.OK) { 
     model = (CommentsListModel) response.getResponseObject(); 
     adapter = new CommentsListAdapter(this, model.getComments()); 
     listView.setAdapter(adapter); 
    } 
    return true; 
} 


@Override 
public synchronized void onTabChanged(String tabName) { 
    if (model != null) { 
     Resources res = getResources(); 
     CommentsFilter filter = null; 
     if (tabName.equals(res.getString(R.string.comments_all))) { 
      filter = new CommentsFilter(model.getComments(), CommentsFilter.ALL); 
     } else if (tabName.equals(res.getString(R.string.comments_liked))) { 
      filter = new CommentsFilter(model.getComments(), CommentsFilter.LIKED); 
     } else if (tabName.equals(res.getString(R.string.comments_disliked))) { 
      filter = new CommentsFilter(model.getComments(), CommentsFilter.DISLIKED); 
     } 
     adapter = new CommentsListAdapter(this, filter.filter()); 
     listView.setAdapter(adapter); 
    } 
} 


private void buildTabs() { 
    Resources res = getResources(); 

    TabHost tabHost = getTabHost(); 
    String tabName = res.getString(R.string.comments_all); 

    TabHost.TabSpec spec = tabHost.newTabSpec(tabName); 
    spec.setIndicator(tabName, null); 
    spec.setContent(R.id.comments_list_lv); 
    tabHost.addTab(spec); 

    tabName = res.getString(R.string.comments_liked); 

    spec = tabHost.newTabSpec(tabName); 
    spec.setIndicator(tabName, null); 
    spec.setContent(R.id.comments_list_lv); 
    tabHost.addTab(spec); 

    tabName = res.getString(R.string.comments_disliked); 

    spec = tabHost.newTabSpec(tabName); 
    spec.setIndicator(tabName, null); 
    spec.setContent(R.id.comments_list_lv); 
    tabHost.addTab(spec); 
    } 


private void requestData() { 
    AbstractRequest request = new CommentsRequest(barcode, 0, take); 
    request.addListener(handler); 

    Message msg = new Message(); 
    msg.what = ApplicationActions.ProtocolActions.REQUEST; 
    msg.obj = request; 
    msg.setTarget(AsyncRequestController.getInstance().getInboxHandler()); 
    msg.sendToTarget(); 
} 
                             } 

回答

3

我解决了这个问题。我只需要在设置适配器后设置listView.setVisibility(View.VISIBLE)。虽然该视图没有设置为不可见或不在XML文件中,但在这种情况下,似乎有必要设置可见性。

+0

我不知道如何,但这对我也有效:-) – 2015-05-20 06:02:27