项目技术:利用 ListView + SimpleAdapter 将 name 和 number 显示出来,
通过给 item 设置监听(setOnItemClickListener)土司“ name 和 number ”
项目描述:通过 ListView+SimpleAdapter 将 name+ number 显示出来,并且 还需要在点击每一项时 土司“name 和 number ”
在每项显示多种数据的时候 我们可以使用 HashMap 来存放数据
实现如下图:
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/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
// 为控件设置监听
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView> parent, View view, int position,
long id) {
Map
String name = item.get("name").toString();
String number = item.get("number").toString();
Toast.makeText(MainActivity.this,"name:"+name+"&"+"number:"+number, Toast.LENGTH_SHORT).show();
}
});
}
}