Adapter(5)SimpleAdapter

SimplAdapter使用自定义的layout定义item 的view, 要传入对应的每个item上的字段名和各个子控件的id,

但是如果item上有button等控件,则默认的不能响应事件,要自定义adapter.

一基本的simple-adapter

1.SimpleAdapterFrgmt.java

public class SimpleAdapterFrgmt extends Fragment implements OnClickListener {
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> datas;
    private ListView listView;
    private Button btn;//注意,当item上有button时,这个button无法响应.默认的SimpleAdapter将不再适应.要自定义BaseAdapter

    private void init() {
        datas = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for (int i = 0; i < 20; i++) {
            map = new HashMap<String, Object>();
            map.put("icon", R.drawable.ic_launcher);
            map.put("title", "title" + i);
            map.put("content", "content" + i);
            map.put("button", "button" + i);
            map.put("check", false);//注意,当item上有button时,这个button无法响应.默认的SimpleAdapter将不再适应.要自定义BaseAdapter
            datas.add(map);
        }
        //每个item上的各字段的名字
        String[] from = new String[] { "icon", "title", "content", "button",
                "check" };
        
        //布局文件上的控件id
        int[] to = new int[] { R.id.simple_adpt_icon, R.id.simple_adpt_title,
                R.id.simple_adpt_contnet, R.id.simple_adpt_btn ,R.id.simple_adpt_check};
        
        simpleAdapter = new SimpleAdapter(getActivity(), datas,
                R.layout.list_item_simple_adapter, from, to);
        listView.setAdapter(simpleAdapter);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        View v = inflater.inflate(R.layout.frgmt_simple_adapter, container,
                false);
        listView = (ListView) v.findViewById(R.id.simple_adapter_list_view);
        
        //注意,当item上有button时,这个button无法响应.默认的SimpleAdapter将不再适应.要自定义BaseAdapter
        
        init();
        return v;
    }

    @Override
    public void onClick(View v) {
        
    }
}

2.list_item_simple_adapter.xml

<?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"
    android:orientation="horizontal" >

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/simple_adpt_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/title" />

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

    </LinearLayout>

    <Button
        android:id="@+id/simple_adpt_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/simple_adpt_btn" />

    <CheckBox
        android:id="@+id/simple_adpt_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/check" />

</LinearLayout>

 

3.效果

 

二,自定义simple-adapter

1.CustomSimpleAdapter.java

public class CustomSimpleAdapter extends SimpleAdapter {
    // 要使用到的数据源
    private List<Map<String, Object>> listCache = new ArrayList<Map<String, Object>>();
    // 填充数据的资源文件
    private int resource;
    //上下文
    private Context context;

    public CustomSimpleAdapter(Context context, List<Map<String, Object>> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.context = context;
        this.listCache = data;
        this.resource = resource;
    }
    // item的总行数
    @Override
    public int getCount() {
        return listCache == null ? 0 : listCache.size();
    }

    // item对象
    @Override
    public Object getItem(int position) {
        return listCache.get(position);
    }

    // item的id
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView ) {
            convertView = LayoutInflater.from(context).inflate(resource, null);
        }
        ImageView imageView = (ImageView) convertView
                .findViewById(R.id.simple_adpt_icon);
        TextView title = (TextView) convertView
                .findViewById(R.id.simple_adpt_title);
        TextView content = (TextView) convertView
                .findViewById(R.id.simple_adpt_contnet);
        
        Map<String, Object> data = (Map<String, Object>) getItem(position);
        title.setText(data.get("title").toString());
        content.setText(data.get("content").toString());
        return convertView;
    }
}

 

这个getView方法写的不对,性能有问题,这样每次都要findViewById,正确写法应该使用ViewHolder和tag,见:

  http://www.cnblogs.com/sjjg/articles/5446654.html

 

2.list_item_simple_adapter.xml

<?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"
    android:orientation="horizontal" >

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/simple_adpt_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/title" />

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

    </LinearLayout>

    <Button
        android:id="@+id/simple_adpt_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/simple_adpt_btn" />

    <CheckBox
        android:id="@+id/simple_adpt_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/check" />

</LinearLayout>

 

posted @ 2015-05-26 11:12  f9q  阅读(143)  评论(0)    收藏  举报