安卓學習筆記之SimpleAdapter

今天學習了一個適配器. 讓我們看一下這個適配器的構造方法

SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to) 

這裏我把simpleAdapter用於ListView

那麼context 就代表ListView 這個類了.

List 代表的是需要顯示的數據.

resource 代表顯示需要的佈局文件.

from 爲data中map對應的鍵值

to 爲需要顯示的組件的id.


下面這段是轉載的.


一個SimlpleAdapter是這個工作的。假設將SimpleAdapter用於ListView。那麼ListView的每一個列表項就是resource參數值指定的佈局。而data參數就是要加載到ListView中的數據。我們先看每一個列表項,假設列表項所對應的佈局文件中包含了兩個組件:TextView和EditText,id分別爲textview和edittext。那麼在加載列表項時,需要通過組件的id和data參數中List元素中的Map對象對應。因此,from參數Map對象的key,而to表示組件的id,例如,本例中的參數值爲from = new String[]{"textview", "edittext"},to = new int[]{R.id.textview,R.id.edittext}。意思就是將Map對象中key爲textview的value綁定到R.id.textview上,edittext也類似。


讓我們來看一個例子

主佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >
    
    <LinearLayout 
        android:id = "@+id/listLinearLayou"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        
        <ListView 
            android:id = "@id/android:list"
            android:layout_width ="fill_parent"
            android:layout_height = "wrap_content"
            android:drawSelectorOnTop ="false" //這句話的意思是,點擊一條記錄,顏色會放映在文字的後面.爲ture則爲前面
            android:scrollbars = "vertical" />
        
        
    </LinearLayout>
   

</LinearLayout>


user佈局文件,用來爲adapter服務的.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    android:paddingRight="10dp"
    android:paddingTop="1dp"
    android:paddingBottom="1dp" >
    
    <TextView
        android:id="@+id/user_name"
        android:layout_width="180dp"
        android:layout_height="30dp"
        android:textSize="12sp"
        android:singleLine="true"
        android:text="@string/hello_world"
        />
    
    <TextView
        android:id="@+id/user_ip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="12sp"
        android:text="@string/hello_world"
        />
    

</LinearLayout>

下面是主代碼.

package com.example.day0324;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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("user_name", "chenkai");
        map1.put("user_ip", "192.168.0.1");
        map2.put("user_name", "kuangfen");
        map2.put("user_ip", "192.168.0.3");
        map3.put("user_name", "yupengfei");
        map3.put("user_ip", "192.168.0.5");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip});
        setListAdapter(listAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		//點擊的響應必須繼承它.
		Toast.makeText(this, "位置" +position + "  id"+id, Toast.LENGTH_SHORT).show();
	}
    
}

SimpleAdapter listAdapter = new SimpleAdapter(this,list,R.layout,new String[] {"user_name","user_ip" },
    new int[] { R.id.user_name,id_user_ip});
this 本類
list ArrayList<HashMap<String,String>> 數據
R.latout.user 使用的佈局文件
new String[] {"user_name","user_ip" } 對應着佈局文件的列
new int[] { R.id.user_name,id_user_ip} 列組件
這是我做的簡單筆記,有不對的地方可以指正我.

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