zhaochunqi
6/26/2014 - 7:09 AM

Adapter--SimpleAdapter

Adapter--SimpleAdapter

package cn.zhaochunqi.testadapter.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

/**
 * Created by alex on 6/26/14.
 */
public class TestSimpleAdapter extends ActionBarActivity {

    private ListView lv_;
    private List<Map<String, String>> listdata = new ArrayList<Map<String, String>>();
    private ListView lv_simpleAdaptr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_simpleadapter);
        lv_simpleAdaptr = (ListView) findViewById(R.id.lv_testsimpleadapter);
        createList();
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listdata, R.layout.view_listview_simple, new String[]{"name", "number", "hello"}, new int[]{R.id.tv_name, R.id.tv_number, R.id.tv_hello});
        lv_simpleAdaptr.setAdapter(simpleAdapter);
    }

    /**
     * 创建需要的相应的list表
     */
    private void createList() {

        for (int i = 0; i < 100; i++) {
//            for(int j=0;j<5;j++) {
            Map<String, String> tempmap = new HashMap<String, String>();
            String s = "My name is " + i + "th";
            String num = i + "th";
            tempmap.put("name", s);
            tempmap.put("number", num);
            tempmap.put("hello", "hello");
            listdata.add(tempmap);
//            }
        }

    }


}
<?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="match_parent">
    <ListView
            android:id="@+id/lv_testsimpleadapter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>

</LinearLayout>
<?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="match_parent">

    <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

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

        <TextView
                android:id="@+id/tv_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>

    <TextView
            android:layout_weight="1"
            android:id="@+id/tv_hello"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="#ffff4423"/>

</LinearLayout>