在ListView中,以內不同的Adapter不同,所以也會有不同的效果,其中比較常用的是SampleAdapter,SimpleCursorAdapter,ArrayAdapter,BaseAdapter等,
萬事開頭難,還是從最簡單的SimpleAdapter說起,以後再一點點學習
simpleAdapter的擴展性最好,可以定義各種各樣的佈局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(複選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對顯示ListView做了許多優化,方面顯示而已。
先看看一個實例,是由SimpleAdapter與ListView綁定後的一個小例子。
ListViewone.java文件
Java代碼
-
package org.hualang.simpleadapter;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
-
import android.app.ListActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
import android.widget.Toast;
-
-
public class ListViewone extends ListActivity {
-
/** Called when the activity is first created. */
-
private Toast toast;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
-
HashMap<String,String> map1=new HashMap<String,String>();
-
HashMap<String,String> map2=new HashMap<String,String>();
-
HashMap<String,String> map3=new HashMap<String,String>();
-
map1.put("name", "凝墨");
-
map1.put("phone", "13699452790");
-
map2.put("name", "小棕");
-
map2.put("phone", "15827980910");
-
map3.put("name", "花郎");
-
map3.put("phone", "18678091166");
-
-
list.add(map1);
-
list.add(map2);
-
list.add(map3);
-
SimpleAdapter listAdapter=new SimpleAdapter(this,
-
list,
-
R.layout.info,
-
new String[] {"name","phone"},
-
new int[] {R.id.name,R.id.phone});
-
setListAdapter(listAdapter);
-
}
-
protected void onListItemClick(ListView l,View v,int position,long id)
-
{
-
super.onListItemClick(l,v,position,id);
-
if(l.getItemIdAtPosition(position)==0)
-
{
-
toast.makeText(getApplicationContext(), "我是凝墨", Toast.LENGTH_SHORT).show();
-
}else if(l.getItemIdAtPosition(position)==1)
-
{
-
toast.makeText(getApplicationContext(), "我是小棕", Toast.LENGTH_SHORT).show();
-
}else if(l.getItemIdAtPosition(position)==2)
-
{
-
toast.makeText(getApplicationContext(), "我是花郎", Toast.LENGTH_SHORT).show();
-
}
-
-
}
-
- }
main.xml文件
Java代碼
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/linearlayout"
-
android:orientation="vertical"
-
>
-
<ListView
-
android:id="@id/android:list"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:drawSelectorOnTop="false"
-
android:scrollbars="vertical"
-
/>
-
</LinearLayout>
-
</LinearLayout>
info.xml
Java代碼
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:paddingLeft="10dip"
-
android:paddingRight="10dip"
-
android:paddingTop="1dip"
-
android:paddingBottom="1dip"
-
>
-
<TextView
-
android:id="@+id/name"
-
android:layout_width="180dip"
-
android:layout_height="30dip"
-
android:textSize="10pt"
-
android:singleLine="true"
-
/>
-
<TextView
-
android:id="@+id/phone"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:gravity="right"
-
android:textSize="10pt"
-
/>
-
</LinearLayout>
使用simpleAdapter的數據用一般都是HashMap構成的List,list的每一節對應ListView的每一行。HashMap的每個鍵值數據映射到佈局文件中對應id的組件上。因爲系統沒有對應的佈局文件可用,我們可以自己定義一個佈局info.xml。下面做適配,new一個SimpleAdapter參數一次是:this,佈局文件(info.xml)。佈局文件的組件name,phone。佈局文件的各組件分別映射到HashMap的各元素上,完成適配。
運行結果如下:
當點擊了第一行
實例2:顯示一個帶圖片的ListView,使用適配器SampleAdapter
ListViewone.java
Java代碼
-
package org.hualang.simpleadapter;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import android.app.ListActivity;
-
import android.os.Bundle;
-
import android.widget.SimpleAdapter;
-
public class ListViewone extends ListActivity {
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.info,
-
new String[]{"name","phone","img"},
-
new int[]{R.id.name,R.id.phone,R.id.img});
-
setListAdapter(adapter);
-
}
-
-
private List<Map<String, Object>> getData() {
-
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
-
-
Map<String, Object> map = new HashMap<String, Object>();
-
map.put("name", "凝墨");
-
map.put("phone", "13699782346");
-
map.put("img", R.drawable.pic1);
-
list.add(map);
-
-
map = new HashMap<String, Object>();
-
map.put("name", "小棕");
-
map.put("phone", "15899034671");
-
map.put("img", R.drawable.pic2);
-
list.add(map);
-
-
map = new HashMap<String, Object>();
-
map.put("name", "花郎");
-
map.put("phone", "18677656526");
-
map.put("img", R.drawable.pic3);
-
list.add(map);
-
-
return list;
-
}
-
}
info.xml
Java代碼
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="horizontal" android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
<ImageView android:id="@+id/img"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_margin="5px"/>
-
<LinearLayout android:orientation="vertical"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content">
-
-
<TextView android:id="@+id/name"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textColor="#FFFFFFFF"
-
android:textSize="22px" />
-
<TextView android:id="@+id/phone"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textColor="#FFFFFFFF"
-
android:textSize="13px" />
-
</LinearLayout>
-
</LinearLayout>
這裏,就不做事件處理了,運行結果如下: