在你的主要课程中
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SimpleList extends Activity { //SimpleList is the name of this class
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv=(ListView)findViewById(R.id.listView1);
//final ArrayList<String> myNewList = new ArrayList<String>();
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.simple_array,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_LONG).show();
}
});
}
}
在 strings.xml 中创建一个字符串数组
<resources>
<string name="app_name">SimpleList</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">SimpleList</string>
<string-array name="simple_array">
<item>Bacon Double Cheese Burger</item>
<item>Bacon Cheeses Burger</item>
<item>Cheese Burger</item>
<item>Hamburger</item>
<item>Grilled Chicken Sandwich</item>
<item>Crispy Chicken Sandwich</item>
<item>Chicken Strips</item>
<item>Hot Dog</item>
<item>Chocolate Chip Cookie Dough Polar Swirl</item>
<item>Vanilla Shake</item>
<item>Chocolate Shake</item>
<item>Strawberry Shake</item>
</string-array>
</resources>
在 main.xml 中为您的 ListView 创建一个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFCC00"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>