安卓学习第12课——SimpleAdapter

SimpleAdapter一点也不简单,他的参数就带了5个,天哪,晕了。。

今天学的这个适配器, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

看这个大概明白,参数分别是第一个:表示访问整个android应用程序接口,基本上所有的组件都需要,一般都写this(改天研究一下),第二个应该是这个List对象名,第三个是list的id,第4,5指的分别是他的list里面的Map存的内容,String类型和其他类型(他的id)。

<RelativeLayout xmlns: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" >
<ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<!-- 定义一个ImageView,用于作为列表项的一部分。 -->
<ImageView android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:textColor="#f0f"
    android:paddingLeft="10dp"
    />
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14dp"
    android:paddingLeft="10dp"
    />
</LinearLayout>
</LinearLayout>
package com.example.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.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
//在xml中创建一个listview,用于显示simpleAdapter的列表项
    private String[] names=new String[]{"虎头","弄玉","李清照","李白"};
    private String[] descs=new String[]{"可爱的小孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"};
private int[] imagesIds=new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个List集合,集合元素师Map
        List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
        for(int i=0;i<names.length;i++){
            Map<String,Object> listItem=new HashMap<String,Object>();
            listItem.put("header", imagesIds[i]);
            listItem.put("personname", names[i]);
            listItem.put("desc", descs[i]);
            listItems.add(listItem);
        }
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.simple_item,
                new String[]{"personname","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
        ListView list=(ListView) findViewById(R.id.mylist);
        list.setAdapter(simpleAdapter);
    }

}

我认为这个代码实现的过程是:创建一个listview,实现simpleAdapter列表项,在创建的simpleAdapter里面写下列表项内容。

备注:这个学习了SimpleAdapter,list,HashMap.

安卓学习第12课——SimpleAdapter,布布扣,bubuko.com

时间: 2024-08-02 02:51:09

安卓学习第12课——SimpleAdapter的相关文章

安卓学习第13课——BaseAdapter

BaseAdapter创建这么一个对象,需要些四个方法. int getCount(); Object getItem(int position); long getItemId(int position);View getView(int position, View convertView, ViewGroup parent);(1)列表中的项数(2)返回值的列表内容(3)获得postion处的列表项的ID(4)该列表项里的组件 package com.example.baseadapter

安卓学习第9课——计时器chronometer

今天学习了钟表及计时器.. 我觉得AnalogClock和DigitalClock直接使用就可以.唯一需要知道的就是AnalogClock是可以修改表盘和分针时针的. 方法是android:dail及android:hand_minute和hand_hour. 下面介绍的是计时器的用法. 首先xml中只要放入一个chronometer和一个按钮即可.为的是是点击启动按钮,开始计时,20s停止. package com.example.chronometer; import android.app

安卓学习第11课——AutoCompleteTextView

...在百度上搜了这么一段.理解了ArrayAdapter的三个参数的用途 1. 这个小例子是要显示一个数组,我们就用ArrayAdapter,数组适配器,数据的数据类型<>是String类型的,数据的数据类型还可以是其他的包括对象类型的 2. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ArrayListDemo.this, android.R.layout.simple_list_item

安卓学习第10课——listview

1.普通listview <LinearLayout xmlns: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" androi

安卓学习第8课——开关ToggleButton、Switch

今天学的是对开关的监听,两种开关ToggleButton和switch <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

安卓学习第三课——常见布局

1.相对布局 简单的说,就是通过描述每个组件所在的位置,使用的layout_below等,就是控制组件与组件之间的位置关系. 2.绝对布局 就是通过描述他的x,y坐标来确定位置 3.线性布局 有两种是水平和竖直对其方式,一般情况下整体会使用线性布局,来排列众多的组件 3.帧布局 我感觉就是一层一层的,默认的情况下,多个组件是在同一个位置,所以你需要去修改位置.同时可以选择是否显示. 这可以用来描述视频播放器暂停键的控制方法. 代码如下. <?xml version="1.0" e

安卓学习第7课——checkbutton

1.布局 <TableLayout xmlns: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:orienta

安卓学习第6课——button

今天学会了按钮的背景图片是可以改变的... 背景可以不是图片,而是一个xml文件. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带文字的图片按钮" android:background="@drawable/button_selector" android:textSize=&

安卓学习第18课——StackView

<LinearLayout xmlns: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:orientation