ListViewOnItemClickListener
public class MainActivity extends Activity {
public Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
final String[] strGroup = {"字串1", "字串2", "字串3", "字串4", "字串5", "字串6"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
strGroup);
ListView v = new ListView(this);
v.setAdapter(adapter);
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
String text = strGroup[index];
String result = "索引值: " + index + "\n" + "內容: " + text;
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
}
});
setContentView(v);
}
}