安卓学习笔记之SimpleAdapter

本文详细介绍了如何使用SimpleAdapter在Android应用中实现ListView的数据展示。包括SimpleAdapter的构造方法参数解释、如何将数据与布局文件进行绑定,以及一个实际的例子演示。通过设置合适的参数,开发者能够高效地将数据加载并展示在ListView中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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;
    }


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.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} 列组件
这是我做的简单笔记,有不对的地方可以指正我.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值