listview就是列表啦,一下代碼是listview的簡單運用。
首先是xml文件
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"></ListView>
其次是一個java文件
package com.android;
import android.app.Activity;
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 FortestActivity extends Activity {
private ListView list;
String[] name = new String[] { "Java", "C++", "C", "C#", "VB", "XML",
".NET", "J#" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.listView1);
// 適配器
ArrayAdapter<String> array = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, name);
// 向列表添加數據
list.setAdapter(array);
// 一定要記得設置選擇模式,要不然點擊條目就沒有反應了撒。
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// 設置監聽,這裏有好幾種的監聽方式,根據自身需要選擇。
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String show = (String) list.getItemAtPosition(arg2);
Toast.makeText(FortestActivity.this, show + "被選擇",
Toast.LENGTH_SHORT).show();
}
});
}
}
運行效果截圖