ArrayAdapter
1、首先在mainactivity.xml中布局一个ListView
<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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
2、新建一个item.xml,其中只有一个TextView
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
注意,最好在这个xml中不要使用布局,否则后面可能出错
3、新建一个item_1.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="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
4、新建一个item_2.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" >
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="aaaa" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="bbbbb" />
</LinearLayout>
5、在MainActivity.java写适配器
package com.learn.AdapterUsing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
String objects[] = {"老张","老方","老李","老毕","老刘","老韩"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//【1】找到我们关心的控件
ListView lv = (ListView) findViewById(R.id.lv);
//【2】创建一个arrayAdapter
//方法1:
//ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item, objects);
//方法2:
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_1, R.id.textView1, objects);
//方法3:
List<Map<String,String>> data = new ArrayList<Map<String,String>>();
Map<String,String> map1 = new HashMap<String, String>();
map1.put("name", "张飞");
map1.put("phone", "13888");
Map<String,String> map2 = new HashMap<String, String>();
map2.put("name", "赵云");
map2.put("phone", "110");
Map<String,String> map3 = new HashMap<String, String>();
map3.put("name", "貂蝉");
map3.put("phone", "138222");
data.add(map1);
data.add(map2);
data.add(map3);
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),data,R.layout.item_2,
new String[]{"name","phone"},new int[]{R.id.tv_name,R.id.tv_phone});
//【3】绑定适配器
lv.setAdapter(adapter);
}
}