Android Advanced Development--SimpleAdapter Class

1. The display effect of ArrayAdapter is a bit monotonous. If you want to display more information in one line, such as text and pictures, etc., you need to use SimpleAdapter. The main function of SimpleAdapter class is to convert the data of the List collection into the data that ListView can support. Thus, various display effects are defined.

2. The hierarchical relationship of SimpleAdapter is as follows:
   java.lang.Object
      android.widget.BaseAdapter
         android.widget.SimpleAdapter

3. SimpleAdapter is a simple adapter that can specify a layout file for displaying rows, and map it to the specified layout file through keywords Layout file, one of the important constructors is:
   public SimpleAdapter(Context context,List<? extends Map<String,?>> data,int resource,String[] from,int[] to)
   where the parameter context represents the associated SimpleAdapter Context; data represents a list of Maps; resource represents a unique resource identifier that defines the view layout of list items; from represents a list of column names that will be added to the Map associated with each item; to represents the columns that should be displayed in the parameter from view.

4. Create a new style layout file stu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/stuId"
        android:layout_weight="0.3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

    <TextView
        android:id="@+id/stuName"
        android:layout_weight="0.3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

    <TextView
        android:id="@+id/stuPass"
        android:layout_weight="0.4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

</LinearLayout>

5、新建主布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text="学生信息表"
        android:gravity="center"/>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TableRow>
            <TextView
                android:text="学生编号"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"/>
            <TextView
                android:text="学生姓名"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"/>
            <TextView
                android:text="学生密码"
                android:layout_weight="0.4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"/>
        </TableRow>

        </TableLayout>
    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

6、新建AdapterActivity.java
package xiao.fuyan.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Created by xiao on 2017/1/1.
*/
public class AdapterActivity extends Activity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adapter_xml);

        listView = (ListView) findViewById(R.id.listView1);

        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("stuId", "001");
        map1.put("stuName", "whoami");
        map1.put("stuPass", "whoami");
        list.add(map1);
        map2.put("stuId", "002");
        map2.put("stuName", "faded");
        map2.put("stuPass", "faded");
        list.add(map2);
        map3.put("stuId", "003");
        map3.put("stuName", "cool");
        map3.put("stuPass", "cool");
        list.add(map3);

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.stu,
                new String[]{"stuId", "stuName", "stuPass"}, new int[]{R.id.stuId,
                R.id.stuName, R.id.stuPass});

        //为ListView添加适配器
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(AdapterActivity.this, parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447778&siteId=291194637