Android学习第八天----SimpleAdapter

本文介绍了如何使用SimpleAdapter显示复杂的数据,并提供了自定义Adapter的具体步骤。通过创建XML布局文件和Java类,可以有效地管理ListView中的数据。

simpleadapter的实现也是比较重要的,通过simpleadapter的源码去重写属于自己的adapter。

 

原始的使用方法,在xml中添加一个listview组件,然后创建一个xml文件,里面放置任意的组件,以下用比较简单的textview来介绍。

该方法将数据封装到一个类对象中会跟好处理,以下是xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android1:id="@+id/listView1"
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

下面是自己创建的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 

封装类,java代码:

package com.will.entity;

public class Students {
    public Integer stuId;
    public Integer stuImageId;
    public String stuName;
    public String stuPhone;
    
    
    public Students(Integer stuId, Integer stuImageId, String stuName,
            String stuPhone) {
        this.stuId = stuId;
        this.stuImageId = stuImageId;
        this.stuName = stuName;
        this.stuPhone = stuPhone;
    }
    
}    

自己重写的adapter类:

  

package com.will.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {
    private Context mContext;
    private List<? extends Map<String,?>> mData;
    private int mResourceTextView;
    private String [] mFrom;
    private int [] mTo;

    public MyBaseAdapter(Context mContext,
            List<? extends Map<String, ?>> mData, int mResourceTextView,
            String[] mFrom, int[] mTo) {
        super();
        this.mContext = mContext;
        this.mData = mData;
        this.mResourceTextView = mResourceTextView;
        this.mFrom = mFrom;
        this.mTo = mTo;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
    // 将之前传入进来的数值取出来并放置到hashMap中 HashMap
<String, String> hashMap=(HashMap<String, String>)mData.get(position); //创建xml解析器 LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    //查找到该布局文件里面的linearlayout组件 LinearLayout linearLayout
=(LinearLayout)layoutInflater.inflate(mResourceTextView, null); //通过布局文件里面的组件,查找出来 ImageView imageView=(ImageView)linearLayout.findViewById(mTo[0]); TextView textView1=(TextView)linearLayout.findViewById(mTo[1]); TextView textView2=(TextView)linearLayout.findViewById(mTo[2]); //对组件进行赋值 int imageRes=Integer.parseInt(hashMap.get(mFrom[0])); String textViewValue1=hashMap.get(mFrom[1]); String textViewValue2=hashMap.get(mFrom[2]);
    //对自己创建出来的xml文件进行赋值 imageView.setImageResource(imageRes); textView1.setText(textViewValue1); textView2.setText(textViewValue2);
    //返回该布局文件
return linearLayout; } }

mainActivity代码:

  

package com.will.test;

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.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.will.entity.Persons;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Students stu1=new Students(1, R.drawable.ic_launcher, "Tom", "1231");
        
        Map<String, String> map1=new HashMap<String, String>();
        map1.put("stuImage", String.valueOf(stu1.stuImageId));
        map1.put("stuName", stu1.stuName);
        map1.put("stuPhone", stu1.stuPhone);
        
        
        Students stu2=new Students(2, R.drawable.ic_launcher, "Tom2", "123213123");
        
        Map<String, String> map2=new HashMap<String, String>();
        map2.put("stuImage", String.valueOf(stu2.stuImageId));
        map2.put("stuName", stu2.stuName);
        map2.put("stuPhone", stu2.stuPhone);
        
        
        List <Map<String,String>> data= new ArrayList<Map<String,String>>();
        data.add(map1);
        data.add(map2);
        
        ListAdapter mListAdapter = new MyBaseAdapter(this, data, R.layout.activity_main_lv, new String[]{"stuImage","stuName","stuPhone"}, new int[]{R.id.imageView1,R.id.textView1,R.id.textView2});
        
        ListView  mListView=(ListView)findViewById(R.id.listView1);
        
        mListView.setAdapter(mListAdapter);
    }
}


创建两个对象,然后将对象的值放置到map中,然后加入到list中,然后在通过自己重写的adapter将以上的值传递给adapter就可以显示出来。
 

转载于:https://www.cnblogs.com/will-peng/archive/2013/03/13/2958261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值