SimpleAdapter 实例

调用的Layout文件样式

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image_0"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:layout_weight="2"
    android:adjustViewBounds="true"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="姓名: "
                android:textSize="17dp"/>

            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#f00"
                android:textSize="10pt" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="年龄: "
                android:textSize="17dp"/>

            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#f00"
                android:textSize="10pt">
            </TextView>
        </TableRow>
    </TableLayout>
</LinearLayout>

Layout文件.xml

<?xml version="1.0" encoding="utf-8"?>

    <ListView
        android:id="@+id/listview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>

Main.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class MainActivity extends Activity {

SimpleAdapter simpleAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adapter);
    ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
    String[] names = new String[]{"张三", "李四", "网速", "赵六"};
    int[] ages=new int[]{10,20,30,40};
    int[] imageIDs=new int[]{R.drawable.lijiang,R.drawable.qiao,R.drawable.shuangta,R.drawable.shui};
    listView = findViewById(R.id.listview_id);

    for(int i=0;i<4;i++){
        Map<String,Object> map=new HashMap<>();
        map.put("image",imageIDs[i]);
        map.put("name",names[i]);
        map.put("age",ages[i]);
        arrayList.add(map);
    }
    String[] from=new String[]{"image","name","age"};
    int[] to=new int[]{R.id.image_0,R.id.tv1,R.id.tv2};
    simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.textview_layout,from,to);
    listView.setAdapter(simpleAdapter);
}

}

效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值