安卓学习笔记之SimpleAdapter

来源:互联网 发布:英国情报机构 大数据 编辑:程序博客网 时间:2024/05/17 06:25

今天学习了一个适配器. 让我们看一下这个适配器的构造方法

SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to) 

这里我把simpleAdapter用于ListView

那么context 就代表ListView 这个类了.

List 代表的是需要显示的数据.

resource 代表显示需要的布局文件.

from 为data中map对应的键值

to 为需要显示的组件的id.


下面这段是转载的.


一个SimlpleAdapter是这个工作的。假设将SimpleAdapter用于ListView。那么ListView的每一个列表项就是resource参数值指定的布局。而data参数就是要加载到ListView中的数据。我们先看每一个列表项,假设列表项所对应的布局文件中包含了两个组件:TextView和EditText,id分别为textview和edittext。那么在加载列表项时,需要通过组件的id和data参数中List元素中的Map对象对应。因此,from参数Map对象的key,而to表示组件的id,例如,本例中的参数值为from = new String[]{"textview", "edittext"},to = new int[]{R.id.textview,R.id.edittext}。意思就是将Map对象中key为textview的value绑定到R.id.textview上,edittext也类似。


让我们来看一个例子

主布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >        <LinearLayout         android:id = "@+id/listLinearLayou"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical">                <ListView             android:id = "@id/android:list"            android:layout_width ="fill_parent"            android:layout_height = "wrap_content"            android:drawSelectorOnTop ="false" //这句话的意思是,点击一条记录,颜色会放映在文字的后面.为ture则为前面            android:scrollbars = "vertical" />                    </LinearLayout>   </LinearLayout>


user布局文件,用来为adapter服务的.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:padding="10dp"    android:paddingRight="10dp"    android:paddingTop="1dp"    android:paddingBottom="1dp" >        <TextView        android:id="@+id/user_name"        android:layout_width="180dp"        android:layout_height="30dp"        android:textSize="12sp"        android:singleLine="true"        android:text="@string/hello_world"        />        <TextView        android:id="@+id/user_ip"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:textSize="12sp"        android:text="@string/hello_world"        />    </LinearLayout>

下面是主代码.

package com.example.day0324;import java.util.ArrayList;import java.util.HashMap;import android.os.Bundle;import android.app.ListActivity;import android.view.Menu;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MainActivity extends ListActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();        HashMap<String, String> map1 = new HashMap<String, String>();        HashMap<String, String> map2 = new HashMap<String, String>();        HashMap<String, String> map3 = new HashMap<String, String>();        map1.put("user_name", "chenkai");        map1.put("user_ip", "192.168.0.1");        map2.put("user_name", "kuangfen");        map2.put("user_ip", "192.168.0.3");        map3.put("user_name", "yupengfei");        map3.put("user_ip", "192.168.0.5");        list.add(map1);        list.add(map2);        list.add(map3);        SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip});        setListAdapter(listAdapter);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);//点击的响应必须继承它.Toast.makeText(this, "位置" +position + "  id"+id, Toast.LENGTH_SHORT).show();}    }

SimpleAdapter listAdapter = new SimpleAdapter(this,list,R.layout,new String[] {"user_name","user_ip" },
    new int[] { R.id.user_name,id_user_ip});
this 本类
list ArrayList<HashMap<String,String>> 数据
R.latout.user 使用的布局文件
new String[] {"user_name","user_ip" } 对应着布局文件的列
new int[] { R.id.user_name,id_user_ip} 列组件
这是我做的简单笔记,有不对的地方可以指正我.

0 0
原创粉丝点击