クリック処理に対応させます。
ほとんど決まり文句で済み、多言語にも対応できます。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.test.listapp.MainActivity"> <ListView android:id="@+id/listView" android:entries="@array/name" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
MainActivity.java(完成)
package com.test.listapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((ListView)findViewById(R.id.listView)).setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { String[] strings = getResources().getStringArray(R.array.name); Toast.makeText(MainActivity.this, strings[position], Toast.LENGTH_SHORT).show(); } } ); } }