1. Are you ready for the Galaxy S20? Here is everything we know so far!

ListView in TabWidget

Discussion in 'Introductions' started by iUngi, Oct 30, 2010.

  1. iUngi

    iUngi Lurker
    Thread Starter

    Hi,

    I have a problem with the listview in the tabwidget.
    My source is the following:

    main.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">
    <LinearLayout android:eek:rientation="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"
    android:padding="5dp">
    <ListView android:id="@+id/all_devices"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"></ListView>
    <ListView android:id="@+id/fav_devices"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_centerHorizontal="true">
    </ListView>
    </FrameLayout>
    </LinearLayout>
    </TabHost>

    main.java:
    package iUngi.iKey;

    import java.util.ArrayList;
    import java.util.Arrays;

    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TabHost;

    public class iBlueKey extends TabActivity {
    /** Called when the activity is first created. */


    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Resusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("all_devices").setIndicator("All visible devices",
    res.getDrawable(R.drawable.m_all))
    .setContent(intent);

    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("fav_devices").setIndicator("Favorite devices",
    res.getDrawable(R.drawable.m_fav))
    .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);

    }
    }


    I tray to add a listview to the first tab, but I get a runtime error.

    package iUngi.iKey;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import iUngi.iKey.R;

    public class AlbumsActivity extends Activity {
    static final String[] COUNTRIES = new String[] {
    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"};
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.fav_list);

    ListView list = (ListView)findViewById(R.id.all_devices);
    ArrayAdapter adapter =
    new ArrayAdapter(this, R.layout.list_layout, COUNTRIES);
    list.setAdapter(adapter);

    registerForContextMenu(list);
    }

    }

    list_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
    </TextView>

    If you have any suggestion or idea please share with me.
    Thanks.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Martimus

    Martimus One bite at a time...

  3. Steven58

    Moderator

    Welcome to Android Forums!
     
Loading...

Share This Page

Loading...