各种Adapter的用法

同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter,SimpleAdapter,SimpleCursorAdapter,以及重写BaseAdapter等方法。

  ArrayAdapter比较简单,但它只能用于显示文字。而SimpleAdapter则有很强的扩展性,可以自定义出各种效 果,SimpleCursorAdapter则可以从数据库中读取数据显示在列表上,通过从写BaseAdapter可以在列表上加处理的事件等。

  ArrayAdapter:

 1 package com.shang.test;
 2 
 3  import java.util.ArrayList;
 4 
 5  import android.app.Activity;
 6  import android.os.Bundle;
 7  import android.widget.ArrayAdapter;
 8  import android.widget.ListView;
 9 
10  /**
11  * 
12  * @author shangzhenxiang
13  *
14  */
15  public class TestArrayAdapterActivity extends Activity{
16 
17     private ListView mListView;
18     private ArrayList<String> mArrayList = new ArrayList<String>();
19     
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.testarrayadapter);
24         mListView = (ListView) findViewById(R.id.myArrayList);
25         mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
26     }
27     
28     private ArrayList<String> getData() {
29         mArrayList.add("测试数据1");
30         mArrayList.add("测试数据2");
31         mArrayList.add("测试数据3");
32         mArrayList.add("测试数据4");
33         mArrayList.add("测试数据5");
34         mArrayList.add("测试数据6");
35         return mArrayList;
36     }
37 }
View Cod

布局里面有个ListView就可以了:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">
    <TextView  
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>
    <ListView
        android:id="@+id/myArrayList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>    
 </LinearLayout>
View Code

上面的代码中用到了ArrayAdapter的构造方法:

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

Api中是这么描述的:

其中Context为当前的环境变量,可以显示一行文字的一个布局文件,和一个List的集合,也就是数据源。

布局文件可以自己写,也可以用系统的,我这里是用的系统的。自己写的布局中包含一个TextView就可以了,当然系统中也有包含一个TextView的布局文件:就是 android.R.layout.simple_expandable_list_item_1,调用这个比较方便。

这里给出运行后的效果图:

  


SimpleAdapter:

package com.shang.test;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * 
 * @author shangzhenxiang
 *
 */
public class TestSimpleAdapter extends Activity {

    private ListView mListView;
    private SimpleAdapter mAdapter;
    private List<HashMap<String, Object>> mHashMaps;
    private HashMap<String, Object> map;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testsimpleadapter);
        mListView = (ListView) findViewById(R.id.mySimpleList);
        mAdapter = new SimpleAdapter(this, getData(), R.layout.simpleitem, new String[]{"image", "title", "info"}, new int[]{R.id.img, R.id.title, R.id.info});
        mListView.setAdapter(mAdapter);
    }
    
    private List<HashMap<String, Object>> getData() {
        mHashMaps = new ArrayList<HashMap<String,Object>>();
        map = new HashMap<String, Object>();
        map.put("image", R.drawable.gallery_photo_1);
        map.put("title", "G1");
        map.put("info", "google 1");
        mHashMaps.add(map);
        
        map = new HashMap<String, Object>();
        map.put("image", R.drawable.gallery_photo_2);
        map.put("title", "G2");
        map.put("info", "google 2");
        mHashMaps.add(map);
        
        map = new HashMap<String, Object>();
        map.put("image", R.drawable.gallery_photo_3);
        map.put("title", "G3");
        map.put("info", "google 3");
        
        mHashMaps.add(map);
        return mHashMaps;
    }
}
View Code
<?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:orientation="horizontal">
    <ImageView 
        android:layout_width="wrap_content" 
        android:id="@+id/img" 
        android:layout_margin="5px"
        android:layout_height="wrap_content">
        </ImageView>
    <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView 
            android:id="@+id/title" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="22px"></TextView>
        <TextView 
            android:id="@+id/info" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="13px"></TextView>
    </LinearLayout>
</LinearLayout>
View Code

simpleAdapter的:


第一个参数和第三个参数跟ArrayAdapter中的是一样的,第二个参数就是由HashMap组成的List,也就是数据源,而第5个参数也就是map中的key,最后一个参数就是map中key对应的值要显示在布局中的位置的id。

看下效果:

原文地址:https://www.cnblogs.com/samjustin/p/4649658.html