无法在TabHost的FrameLayout中更新ListView

时间:2011-02-01 02:32:46

标签: android android-widget android-layout android-listview android-tabhost

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

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

1 个答案:

答案 0 :(得分:3)

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