1

我有 4 个选项卡,其中 1 个选项卡(选项卡 3)有一个包含 2 个选项的列表。当我单击其中一个选项时,是否可以开始新活动?我尝试将 Activity 更改为 ListActivity 但我的应用程序在加载后立即崩溃。我是否需要为此实施其他方法才能使其正常工作?

代码:

public class MainActivity extends Activity implements OnClickListener{

TabHost th;
ListView libraryView;
String libraryList[] = {"Item1", "Item2"};

@Override
public void onCreate(Bundle savedInstanceState) {
    //Always do this at the start.
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, R.id.library_list_content, libraryList);
    libraryView = (ListView) findViewById(R.id.listView1);
    libraryView.setAdapter(adapter);

    //Creating the TabHost and gathering its ID for usage.
    th = (TabHost) findViewById(R.id.tabhost);        
    th.setup();

    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("tab1");
    th.addTab(specs);

    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("tab2");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("tab3");
    th.addTab(specs);

    specs = th.newTabSpec("tag4");
    specs.setContent(R.id.tab4);
    specs.setIndicator("tab4");
    th.addTab(specs);

}

@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.listView1:


    }

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
                </ListView>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </LinearLayout>

        </FrameLayout>

        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginBottom="-4dp" >
        </TabWidget>
    </LinearLayout>
</TabHost>

</RelativeLayout>
4

1 回答 1

0

像这样为列表视图添加点击监听器

ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {                
    // depending on item positions create intents for activity. 
       }
});
于 2012-08-25T14:24:01.417 回答