Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setCurrentTab not working

For some reason setCurrentTab is not switching tabs after the first time. This is the code that I use.

private OnClickListener buttonListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));

    }
};

It is connected to buttons each of which have a tag that equals the number of of tab to be shown. When the button is clicked the first time and this method is called then the tab shows up. I can also see that it executes the code that creates the tab contents.

However, once a tab has been shown once and I have moved to another one, clicking back to it doesn't work. The method is definitely called and the tag is also correct. I put in commands to print to the log to confirm that. Also, it works first time round so must be okay.

Any idea?

Full code:

public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTabs();
    }

    @Override
    protected void initTabs() {

        addTab("0",this);
        addTab("1",this);
        addTab("2",this);
        addTab("3",this);

    }

    /*
     * 
     * @param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
     * @return An array containing all of the objects in the list.
     */
    protected Serializable[] getDataArray(Bundle extras) {
        int size = extras.size();
        Serializable[] data = new Serializable[size];
        for (int i = 0; i < size; i++) {
            data[i] = extras.getSerializable(Integer.toString(i));
        }

        return data;
    }

    @Override
    public View createTabContent(String tag) {


        // Get the data returned from the servelet and display it in the ListView
        Log.d("TestTabs","createTabContent");
         ListView lv = (ListView) findViewById(R.id.list_view);
         List<String> list1Strings = new ArrayList<String>();
         switch (Integer.parseInt(tag)){
            case 0:
                Log.d("TestTabs","Case 0");
                lv.setAdapter(new MyAdapter(this,lv,null));
                break;

            case 1:

                Log.d("TestTabs","Case 1");
                list1Strings.add("Item 21");
                list1Strings.add("Item 22");
                list1Strings.add("Item 23");
                list1Strings.add("Item 24");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;

            case 2:

                Log.d("TestTabs","Case 2");
                list1Strings.add("Item 31");
                list1Strings.add("Item 32");
                list1Strings.add("Item 33");
                list1Strings.add("Item 34");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      

            case 3:

                Log.d("TestTabs","Case 3");
                list1Strings.add("Item 41");
                list1Strings.add("Item 42");
                list1Strings.add("Item 43");
                list1Strings.add("Item 44");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      
        }

        return lv;
    }


    protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
        TabSpec tabSpec = tabHost.newTabSpec(tabName);
        tabSpec.setIndicator(tabName);                // Don't set tab layout since we are going to make it invisible
        tabSpec.setContent(tabFactory);
        tabHost.addTab(tabSpec);

        addButton(tabName);


    }

    protected void addButton(String tabName){
        Button button = (Button) buttonHolder.getChildAt(nextChild);
        button.setText(tabName);
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(buttonListener);
        nextChild--;
    }


    private OnClickListener buttonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            v.setBackgroundResource(R.drawable.tab_selected);
            tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
            Log.d("TabbedScreen","Set tab to " + v.getTag());
            View view;
            for (int i=0; i< childCount; i++)
                if ((view = buttonHolder.getChildAt(i)) != v){
                    view.setBackgroundResource(R.drawable.button_tab);
                    view.invalidate();
                }

        }
    };
}

XML:

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


  <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
       android:layout_below="@id/content_screen_user_details">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
            <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:visibility="gone"/>            
            <LinearLayout android:id="@+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button3" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="3"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button2" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                     android:tag="2"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button1" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="1"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button0" 
                    android:background="@drawable/tab_selected" android:visibility="invisible"
                    android:tag="0"/>
            </LinearLayout> 
        </FrameLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <com.applicat.meuchedet.views.RoundedListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="2dip" 
        android:paddingRight="2dip" 
        android:dividerHeight="1dip"
        android:footerDividersEnabled="true"
        android:listSelector="@drawable/listview_selected_item_background"
        android:fadingEdge="none"
        android:cacheColorHint = "#00000000"
        roundedListView:radius="20"
        roundedListView:border="2"
        />
        </FrameLayout>
    </LinearLayout>
</TabHost>

Edit:

I was wondering if the problem is in the row:

ListView lv = (ListView) findViewById(R.id.list_view);

Am I getting back the same object each time? If so, how do I get a new one each time based on the one defined in the FrameLayout.

like image 628
theblitz Avatar asked Dec 01 '25 02:12

theblitz


2 Answers

How can you be sure that the value assigned to button in your xml is the same you get with only nextChild-- .

not so good solution, you can test it.

void addButton(String tabName){ 
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.getTag() !=  String.valueOf(nextChild); ERROR;  

but It's better to set it to the same as you always get with buttonHolder.getChildAt()

    Button button = (Button) buttonHolder.getChildAt(nextChild);
    button.setTag(String.valueOf(nextChild));

modified:

protected void addButton(String tabName){
    Button button = (Button) buttonHolder.getChildAt(nextChild);
    // ----------- modified -----------
    button.setTag(String.valueOf(nextChild));
    button.setText(tabName);
    button.setVisibility(View.VISIBLE);
    button.setOnClickListener(buttonListener);
    nextChild--;
}


private OnClickListener buttonListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        v.setBackgroundResource(R.drawable.tab_selected);
        // ----------- modified -----------
        Button btc = (Button)v;
        int idx;
        try {
          idx = Integer.parseInt((String) btc.getTag());
            } catch(NumberFormatException exx) {
            System.out.println("Could not parse " + exx);
            } 
        if ( idx < childCount) {
           Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
           Button butv;
           for (int i=0; i< childCount; i++) 
            if ( i != idx){
            butv = (Button)  buttonHolder.getChildAt(i);    
            butv.setBackgroundResource(R.drawable.button_tab);
            butv.invalidate();
            }
           tabHost.setCurrentTab(idx);
           tabHost.focusCurrentTab(idx);
        }
        // ----------- modified ----------- 

    }
};

}

like image 198
moskito-x Avatar answered Dec 03 '25 20:12

moskito-x


In order to use getTag, you must need to set it first. So I believe you forgot to add the following line in your addButton method:

button.setTag(tabName);
like image 29
waqaslam Avatar answered Dec 03 '25 19:12

waqaslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!