Question

I have a TabActivity with a single ListView inside TabHost's FrameLayout. This ListView is shared among my 3 tabs. The items of the list are downloaded from the internet using a Handler, but the list is not being updated when the data come. When I select another tab, the list updates itself, but I want to be able to see it being updated after download. I've already tried to invalidate the ListView, the FrameLayout and the TabHost, but nothing happens. The notifyDataSetChanged() and notifyDataSetInvalidated() methods don't help either. If I put the ListView out of the FrameLayout, it works as expected. Here's the code:

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

Solution

I solved the problem. I only needed to set listView.setVisibility(View.VISIBLE) after setting the adapter. Although the view is not set invisible or gone in XML file, it seems necessary to set visibility in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top