標籤:
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
Android train——ListView綁定ArrayAdapter、SimpleAdapter、SimpleCursorAdapter、BaseAdapter