Tab Host List Adapter and Lists

依然范特西╮ 提交于 2020-01-15 14:17:28

问题


Hi I have a problem with my tabhost and I don't really understand why. My tab host contains 4 listviews and shows a list in each tab. Sometimes when I change the tab (click on new tab) the content doesn't change or it freezes. I have tried it on several devices and the problem is still there

Here is my code:

public class TabHostActivity extends TabActivity implements OnTabChangeListener {

    private TabHost mTabHost;
    private Resources mResources;
    private SharedPreferences prefs;
    private ListView lv1;
    private ListView lv2;
    private ListView lv3;
    private ListView lv4;
    private String servicio = "teatre"; // por ahora luego varia segun los
                                        // eventos
    private String servicio2 = "expos";
    private static final String Tab_title1 = "Agenda";
    private static final String Tab_title3 = "Teatre";
    private static final String Tab_title4 = "Exposicions";
    private static final String Tab_title2 = "Cinema";
    private EventsList eventsL;
    private ArrayList<Event> events;
    private EventAdapterAprop mAdapter;
    private EventAdapterAprop mAdapter2;
    private EventAdapterAprop mAdapter3;
    private EventAdapterAprop mAdapter4;
    private EventsList eventsL3;
    private EventsList eventsL4;
    private ArrayList<Event> events3;
    private ArrayList<Event> events4;
    private EventsList eventsL2;
    private ArrayList<Event> events2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost);

        mTabHost = getTabHost();
        lv1 = (ListView) findViewById(R.id.list1);

        lv2 = (ListView) findViewById(R.id.list2);
        lv3 = (ListView) findViewById(R.id.list3);
        lv4 = (ListView) findViewById(R.id.list4);

        Bundle b = getIntent().getExtras();

        //agenda
        try{
        eventsL = b.getParcelable("events");
        events = new ArrayList<Event>();

        for (int i = 0; i < eventsL.size(); i++) {
            events.add(eventsL.get(i));
        }


            mAdapter = new EventAdapterAprop(this, events);
        } catch (Exception ex) {
            ex.printStackTrace();
            mAdapter = new EventAdapterAprop(this, crearVacio());
        }
        // teatro

        try{
        eventsL3 = b.getParcelable("events3");
        events3 = new ArrayList<Event>();

        for (int i = 0; i < eventsL3.size(); i++) {
            events3.add(eventsL3.get(i));
        }


            mAdapter3 = new EventAdapterAprop(this, events3);
        } catch(Exception ex) {
            mAdapter3 = new EventAdapterAprop(this, crearVacio());
            ex.printStackTrace();
        }


        // expos
        try{
        eventsL4 = b.getParcelable("events4");
        events4 = new ArrayList<Event>();

        for (int i = 0; i < eventsL4.size(); i++) {
            events4.add(eventsL4.get(i));
        }

            mAdapter4 = new EventAdapterAprop(this, events4);
        } catch(Exception ex){
            mAdapter4 = new EventAdapterAprop(this, crearVacio());
            ex.printStackTrace();
        }

        // cine
        try{
        eventsL2 = b.getParcelable("events2");
        events2 = new ArrayList<Event>();

        for (int i = 0; i < eventsL2.size(); i++) {
            events2.add(eventsL2.get(i));
        }


            mAdapter2 = new EventAdapterAprop(this, events2);
        } catch(Exception ex) {
            mAdapter2 = new EventAdapterAprop(this, crearVacio());
            ex.printStackTrace();
        }

        //mAdapter2=new EventAdapterAprop(this, crearVacio());

        lv1.setAdapter(mAdapter);
        lv1.setTextFilterEnabled(true);
        lv1.computeScroll();

        lv2.setAdapter(mAdapter2);
        lv2.setTextFilterEnabled(true);
        lv2.computeScroll();

        lv3.setAdapter(mAdapter3);
        lv3.setTextFilterEnabled(true);
        lv3.computeScroll();

        lv4.setAdapter(mAdapter4);
        lv4.setTextFilterEnabled(true);
        lv4.computeScroll();

        mTabHost.addTab(mTabHost.newTabSpec(Tab_title1)
                .setIndicator(Tab_title1).setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lv1;
                    }
                }));
        mTabHost.addTab(mTabHost.newTabSpec(Tab_title2)
                .setIndicator(Tab_title2).setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lv2;
                    }
                }));

        mTabHost.addTab(mTabHost.newTabSpec(Tab_title3)
                .setIndicator(Tab_title3).setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lv3;
                    }
                }));
        mTabHost.addTab(mTabHost.newTabSpec(Tab_title4)
                .setIndicator(Tab_title4).setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lv4;
                    }
                }));
    }

    public ArrayList<Event> crearVacio() {
        ArrayList<Event> arrayList = new ArrayList<Event>();
        Event e = new Event();
        e.setTitle("Cap esdeveniment correspon a la seva recerca");
        e.setDates("");
        e.setTimes("");
        e.setVenue("");
        arrayList.add(e);
        return arrayList;
    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub

    }

    }

and the xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android1="http://schemas.android.com/apk/res/android">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:background="@drawable/fonsbutxacadroid2">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android1:background="@color/amarillo1"/>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <ListView android:id="@+id/list1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1" 
                >
            </ListView>
            <ListView android:id="@+id/list2" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                >
            </ListView>
            <ListView android:id="@+id/list3" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                >
            </ListView>
            <ListView android:id="@+id/list4" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                >
            </ListView>
        </FrameLayout>
    </LinearLayout>
</TabHost>

What is wrong?

Thanks in advance.


回答1:


Making each tab one separate activity solved the problem

layout example:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
xmlns:android1="http://schemas.android.com/apk/res/android" 
android1:id="@+id/@android:id/tabhost" android1:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="5dp">
    <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"
        >           
    </FrameLayout>
</LinearLayout>

The tabhost class:

public class AllActivitiesTabs extends TabActivity {

private static final String Tab_title = "Nueva";
private TabHost mTabHost;
private TextView txtTabInfo;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


mTabHost = getTabHost();

TabSpec spec;
//to add one tab activity       
txtTabInfo = new TextView(getApplicationContext());
txtTabInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,         
LayoutParams.WRAP_CONTENT));
txtTabInfo.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.tab_incidencia));
Intent intent = new Intent();
intent.setClass(this, ListActivityClass.class);
spec = mTabHost.newTabSpec(Tab_title).setIndicator(txtTabInfo)
            .setContent(intent);
mTabHost.addTab(spec);

//do the same for other activities
//your activities can be lists or something else:


txtTabInfo2 = new TextView(getApplicationContext());
txtTabInfo2.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.tab_solucions));
txtTabInfo2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,     
LayoutParams.WRAP_CONTENT));
intent = new Intent();
intent.setClass(this, FirstGroup.class);

spec = mTabHost.newTabSpec(Tab_title2).setIndicator(txtTabInfo2)
            .setContent(intent);
mTabHost.addTab(spec);
}
}


来源:https://stackoverflow.com/questions/6058708/tab-host-list-adapter-and-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!