1

4 つのタブがあり、そのうちの 1 つ (タブ 3) には 2 つのオプションを含むリストがあります。オプションの 1 つをクリックしたときに新しいアクティビティを開始する方法はありますか? 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 に答える