ListView绑定ArrayAdapter
res/layout/activity_main.xml
<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="horizontal"> <!-- 添加一个ListView控件 --> <ListView android:id = "@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
MainActivity.java
package com.train.openso.myapplication_a; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends ActionBarActivity { //(1)定义一个数组来存放ListView中item的内容。 private final static String [] strs = new String[]{"first","second","third","fourth","fifth","sixth"}; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); //============================================================= //(2)通过实现ArrayAdapter的构造函数来创建一个ArrayAdapter的对象 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_single_choice,strs); //(3)为ListView绑定ArrayAdapter lv.setAdapter(arrayAdapter); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // 为ListView绑定一个点击监听器,点击后在标题栏显示点击的行数 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //点击后在标题上显示点击了第几行 setTitle("你点击了第"+position+"行"); } }); //================================================================ } }
ListView绑定SimpleAdapter
res/layout/activity_main.xml
<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="horizontal"> <!-- 添加一个ListView控件 --> <ListView android:id = "@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
item.xml --->做为每一行的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <ImageView android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ItemImage" /> <TextView android:id="@+id/ItemTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="20sp" /> <TextView android:id="@+id/ItemText" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle" /> </RelativeLayout>
MainActivity.java
package com.train.openso.myapplication_a; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends ActionBarActivity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); //=============在Java代码中为ListView绑定数据 //数据载入动态数组 ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<9;i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ITEMIMAGE",R.mipmap.ic_launcher); map.put("ITEMTITLE","第"+i+"行"); map.put("ITEMTEXT","这是第"+i+"行"); arrayList.add(map); } //通过实现ArrayAdapter的构造函数来创建一个ArrayAdapter的对象 //new SimpleAdapter(this,数据源,每行的布局xml,数据源个体键值,每行的布局的元素id); //可抽象为:以个人规定的xml格式装载数据源,并绑定至SimpleAdapter。 SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,arrayList, R.layout.item, new String[] {"ITEMIMAGE","ITEMTITLE","ITEMTEXT"}, new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}); lv.setAdapter(mSimpleAdapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { setTitle("点了第"+position+"行"); } }); /*============================================================= //(2)通过实现ArrayAdapter的构造函数来创建一个ArrayAdapter的对象 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_single_choice,strs); //(3)为ListView绑定ArrayAdapter lv.setAdapter(arrayAdapter); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // 为ListView绑定一个点击监听器,点击后在标题栏显示点击的行数 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //点击后在标题上显示点击了第几行 setTitle("你点击了第"+position+"行"); } }); ================================================================*/ } }
使用simpleAdapter的数据一般都是用HashMap构成的列表,列表的每一节对应ListView的每一行。通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上。这个布局文件一般根据自己的需要来自己定义。梳理一下使用SimpleAdapter的步骤。
(1)根据需要定义ListView每行所实现的布局。
(2)定义一个HashMap构成的列表,将数据以键值对的方式存放在里面。
(3)构造SimpleAdapter对象。
(4)将LsitView绑定到SimpleAdapter上。
----------------=------------
看了BaseAdapter,有点hold不住,以后再研究
参考:http://www.cnblogs.com/noTice520/archive/2011/12/05/2276379.html
时间: 2024-10-06 01:02:38