码迷,mamicode.com
首页 > 其他好文 > 详细

adapterView及子类

时间:2014-06-10 16:42:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   class   blog   code   

1、使用arrayadapter创建ListView

bubuko.com,布布扣
public class ArrayAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         String[] str = {"1","2","3","4","5","6"};
//android.R.layout.simple_expandable_list_1 android提供布局文件作为列表组件 ArrayAdapter
<String> adaper = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, str); } }
bubuko.com,布布扣

2、使用simpleAdapter创建ListView

<?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:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:contentDescription="@string/app_name" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="20sp" />

</LinearLayout>

  

bubuko.com,布布扣
public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 对象中那些key生成对应value的列表项
        SimpleAdapter adapter = new SimpleAdapter(this, getData(),
                R.layout.simple, new String[] { "title", "img" }, new int[] {
                        R.id.title, R.id.img });
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        // map.put(参数名字,参数值)
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "摩托罗拉");
        map.put("img", R.drawable.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "诺基亚");
        map.put("img", R.drawable.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "三星");
        map.put("img", R.drawable.ic_launcher);
        list.add(map);
        return list;

    }

}
bubuko.com,布布扣

 

adapterView及子类,布布扣,bubuko.com

adapterView及子类

标签:des   android   style   class   blog   code   

原文地址:http://www.cnblogs.com/kevinhuspace/p/3779723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!