38
36

More than 5 years have passed since last update.

[Android] ListView のリストの要素をタップしたら、それぞれ任意の画面に遷移し、更に戻る方法

Last updated at Posted at 2014-06-13

前回までの様子

ListViewに要素を追加させていました。 => こんな感じ。

で今回はここから要素を1つタップしたら、それぞれの別の画面に遷移させたいと思います。

方針

  1. タップされた時に、その要素の position を取得
  2. 別のActivity(SecondActivity.java)に渡し画面遷移させる
  3. SecondActivityを終了させ、前のアクティビティ戻る

画面

飛び元

Screen Shot 2014-06-13 at 15.43.44.png

飛び先

Screen Shot 2014-06-13 at 15.43.48.png

リストのSecondActivity要素をタップした時は上の通り。
その他要素に応じて別々のアクティビティの画面に飛ばせるようにします。

コード

MainActivity.java (元のアクティビティ)

package com.example.lvtest;




public class MainActivity extends Activity implements OnItemClickListener {

    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        //add some items
        adapter.add("SecondActivityの画面");
        adapter.add("ThirdActivityの画面");
        ListView listView = (ListView) findViewById(R.id.listView1);
        //ListView set ArrayAdapter
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((OnItemClickListener) this);
    }

          // リストの何番目かで切り分けて、各要素に対してインテントを設定(飛ばす先のアクティビティを指定)
          switch (pos) {
          case 0:
            startActivity(new Intent(this, SecondActivity.class));
            break;
          case 1:
            startActivity(new Intent(this, ThirdActivity.class));
            break;
          }

    }


}

layout/activity_main.xml (元のxml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

SecondActivity.java (飛び先のアクティビティ)

package com.example.lvtest;


public class AboutMeActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //今回は使ってないけど、飛んできたインテントをゲットしている
        Bundle extras = getIntent().getExtras();
        //今回は使ってないけど、レイアウトのidからviewの情報をゲットしている
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.RelativeLayout0);
    }


}

activity_second.xml (飛び先のxml)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity" >

    <TextView
        android:id="@+id/TextView0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".SecondActivity" />

</RelativeLayout>

AndroidManifest.xml

よく忘れるのがこのファイルの修正

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.lvtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

上記Second〜と同じ様に編集していけば、Third〜に関しても同様にできました。

戻るに関して

戻るボタンをクリックをトリガーにして、finishさせると戻れます。

public void onClick( View v_ )
{
 finish();
}

その他

戻るはまた今度。
コードが汚いのと、もっと工夫できるはずなので、修正したい。。。

38
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
36