Ответ 1
На самом деле это довольно просто:
Это ваша деятельность с помощью ListView, она реализует OnItemClickListener:
public class MainActivity extends Activity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//* *EDIT* *
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
// Then you start a new Activity via Intent
Intent intent = new Intent();
intent.setClass(this, ListItemDetail.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
Изменить
Вышеупомянутый код будет помещен в ваш MainActivity.java. Я изменил имя класса на MainActivity
, а contentView на setContentView(R.layout.activity_main)
- имена - это имена недавно созданного проекта Android в Eclipse.
См. также две новые строки в разделе // * Редактировать * - они установят Listener для кликов по элементам в списке.
Ваш файл activity_main.xml должен выглядеть следующим образом:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/sections" >
</ListView>
</RelativeLayout>
array.xml (не string.xml) в вашей папке `res/values /` выглядит так:
<resources>
<string-array name="sections">
<item >Pro Constructive</item>
<item >Con Constructive</item>
<item >1st Speaker Cross</item>
<item >Pro Rebbutal</item>
<item >Con Rebuttal</item>
<item >2nd Speaker Cross</item>
<item >Pro Summary</item>
<item >Con Summary</item>
<item >Grand Cross</item>
<item >Pro Final Focus</item>
<item >Con Final Focus</item>
</string-array>
</resources>
N.B.: Если вы скопируете и вставьте этот код, он должен работать. Но вы получите сообщение об ошибке, нажав на элемент, потому что вы еще не создали ListItemDetail.class
.
Вот пример того, как это могло выглядеть:
Ваш ListItemDetail.java:
public class ListItemDetail extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listitem);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
// Here we turn your string.xml in an array
String[] myKeys = getResources().getStringArray(R.array.sections);
TextView myTextView = (TextView) findViewById(R.id.my_textview);
myTextView.setText(myKeys[position]);
}
}
И его activity_listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"/>
</LinearLayout>
Если вы скопируете этот код, он будет работать.