Android 基本控件使用六(ListView+SimpleAdapter)

项目技术:利用 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>data = new ArrayList>();

// 适配器
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 item = null;


item = new HashMap();
item.put("name", "sophia");
item.put("number","11112222334");
data.add(item);
item = new HashMap();
item.put("name", "sophia0");
item.put("number","11112222335");
data.add(item);
item = new HashMap();
item.put("name", "sophia1");
item.put("number","11112222336");
data.add(item);
item = new HashMap();
item.put("name", "sophia2");
item.put("number","11112222337");
data.add(item);
item = new HashMap();
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 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();

}

});



}




}


你可能感兴趣的:(Android开发初级)