android之簡單listview適配器


listview的控件很好使用,但要在上面按照我們希望的格式放上數據,這就需要baseadapter的幫助了。這裏簡單列出一個

public class MyAdapter extends BaseAdapter{
	private List<Map<String, String>> list;
	private Context context;

	public MyAdapter(List<Map<String, String>> list,Context context){
		this.list=list;
		this.context = context;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.listview, null);
		}
		TextView title = (TextView) convertView.findViewById(R.id.title);
		title.setText(list.get(position).get("title"));
		return convertView;
	}
	
}
getCount()返回的是listview的長度,也就是含有多少條數據吧
getItemId也就是返回當前的條目,即第幾條
getView當然返回的是一個佈局了




convertView = LayoutInflater.from(context).inflate(R.layout.listview, null); 這句話的意思就是把我們找到的listview,xml文件佈局給我們listview裏面的每個item,即每一條,然後通過控制converView來實現我們要展現的形式。這裏的判空是用來實現converView的複用性,這個如果不明白可以查閱更多資料去加以理解。
剩下的事情就簡單了,也就是在我們的avtivity文件裏面配置一下就好,這裏我簡單寫兩句。

 listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(new MyAdapter(list, Test2Activity.this));

這兩句的意思分別是找出我們想要的控件,並把我們的佈局與數據放進去。這個list數據大家可以自己設置,我把我的數據展示如下

private void init() {
		list = new ArrayList<Map<String,String>>();
        map = new HashMap<String, String>();
        map.put("title", "我的資料");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "好友管理");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "偏好設置");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "註冊新賬戶");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "找回密碼");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "關於我們");
        list.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", "...");
        list.add(map);
		
	}
請記住,數據放在適配器之前呢是可以直接調用進去的,如果之後呢需要調用這樣一個方法來通知適配器更新數據,adapter.notifyDataSetChanged()這裏有不懂的就自己琢磨吧。

這裏簡單獻上我的listview的item的佈局文件

<?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:gravity="center_vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aa" />
    </LinearLayout>

</LinearLayout>

截圖展示我所實現的效果

     關於listview的更多內容,我會在以後的介紹中陸續加上,敬請期待。。。



這裏再補充一個更簡單的適配器,能適配很多內容,只要不是很複雜的都可以實現,代碼如下:

 //參數分別代表 上下文 存放數據的集合 佈局文件 集合裏面的字段 item佈局裏面對應的字段
        SimpleAdapter mss=new SimpleAdapter(ChengActivity.this, list,R.layout.listview, new String[]{"iocn","text"},new int[]{R.id.imageView,R.id.textView});
順便附帶我定義的集合
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

截圖如下



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章