Android 基本控制元件使用六(ListView+SimpleAdapter)
專案技術:利用 ListView + SimpleAdapter 將 name 和 number 顯示出來,
通過給 item 設定監聽(setOnItemClickListener)土司“ name 和 number ”
專案描述:通過 ListView+SimpleAdapter 將 name+ number 顯示出來,並且 還需要在點選每一項時 土司“name 和 number ”
在每項顯示多種資料的時候 我們可以使用 HashMap 來存放資料
實現如下圖:
activity_main.xml
<RelativeLayoutxmlns:android
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity">
<ListView
android:id="@+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
contact_item.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_contact_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginBottom="5dp"
android:textColor="#000000"
android:text="yyl"/>
<TextView
android:id="@+id/tv_contact_item_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_contact_item_name"
android:textSize="14sp"
android:textColor="#555555"
android:text="100000000"/>
</RelativeLayout>
實體類,用於描述資料的屬性
Contact.java
package cn.sophia.android_listview_simpleadapter;
public class Contact {
private String name;
private String number;
public Contact(String name, String number) {
super();
this.name = name;
this.number = number;
}
public String getName() {
returnname;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
returnnumber;
}
public void setNumber(String number) {
this.number = number;
}
}
MainActivity.java
package cn.sophia.android_listview_simpleadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// 宣告控制元件
private ListView listView;
// 資料來源
List<Map<String, Object>>data = new ArrayList<Map<String,Object>>();
// 介面卡
private SimpleAdapter adater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控制元件
listView = (ListView) findViewById(R.id.lv_contacts);
// 建立資料來源
Map<String, Object> item = null;
item = new HashMap<String, Object>();
item.put("name", "sophia");
item.put("number","11112222334");
data.add(item);
item = new HashMap<String, Object>();
item.put("name", "sophia0");
item.put("number","11112222335");
data.add(item);
item = new HashMap<String, Object>();
item.put("name", "sophia1");
item.put("number","11112222336");
data.add(item);
item = new HashMap<String, Object>();
item.put("name", "sophia2");
item.put("number","11112222337");
data.add(item);
item = new HashMap<String, Object>();
item.put("name", "sophia3");
item.put("number","111122223338");
data.add(item);
// 建立介面卡
// 根據Map的哪個Key把資料取出來
String[] from = {"name","number"};
// 取出來的資料顯示到哪個控制元件上
int[] to = {R.id.tv_contact_item_name,R.id.tv_contact_item_number};
adater = new SimpleAdapter(this, data, R.layout.contact_item, from , to);
// 設定介面卡
listView.setAdapter(adater);
// 為控制元件設定監聽
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String, Object> item =data.get(position);
String name = item.get("name").toString();
String number = item.get("number").toString();
Toast.makeText(MainActivity.this,"name:"+name+"&"+"number:"+number, Toast.LENGTH_SHORT).show();
}
});
}
}