标签:
MainActivity:
package com.wyl.listview;
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.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
ListView listview;
ListAdapter adapter;
ListAdapter la;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
listview = (ListView) findViewById(R.id.listview01);
//// adapter.a
// //数据源,
// //1.新建适配器
// //
// String[] a = {"the first","the second","第三个","张雅岚"};
//// List<String> b = new ArrayList<String>();
//// b.add("张雅岚");
//// b.add("wyl");
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a);
// listview.setAdapter(adapter);
//
// String[] c = {"one","two","three","four"};
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , c);
// System.out.println("=============");
// listview.setAdapter(adapter);
// la = new SimpleAdapter(this, getData(), resource, from, to)
//
// public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
//
// Added in API level 1
// Constructor
//
// Parameters
// context The context where the View associated with this SimpleAdapter is running
// data A List of Maps. Each entry in the List corresponds to one row in the list.
// The Maps contain the data for each row, and should include all the entries specified in "from"
// resource Resource identifier of a view layout that defines the views for this list item. The layout
// file should include at least those named views defined in "to"
// from A list of column names that will be added to the Map associated with each item.
// to The views that should display column in the "from" parameter. These should all be TextViews.
// The first N views in this list are given the values of the first N columns in the from parameter.
la = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
listview.setAdapter(la);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map map = new HashMap<String,Object>();
for(int i=0;i<10;i++){
map.put("pic", R.drawable.ic_launcher);
map.put("text", "wyl"+i);
System.out.println("i:"+i);
list.add(map);
}
return list;
}
}
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="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="demo"
/>
</LinearLayout>
标签:
原文地址:http://www.cnblogs.com/Sunnor/p/4711476.html