Android ListView

作者: 酥酥肉 | 来源:发表于2018-09-17 10:33 被阅读0次

ArrayAdapter

<?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="vertical">
    <ListView
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>
 private void initArrayList() {
 private String[] datas = {"张三","李四","王五","麻子","小强"};
//        初始化适配器
        arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,datas);
        listView.setAdapter(arrayAdapter);

    }

public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
        this(context, resource, 0, Arrays.asList(objects));
    }
  • ArrayAdapter第一个参数上下文,第二个参数layout, 第三个参数显示的数据

SimpleAdapter

simple_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:layout_margin="5dp"/>
   <LinearLayout
       android:id="@+id/ll2"
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:orientation="vertical"
       android:layout_marginTop="5dp"
       android:layout_marginLeft="10dp">
       <TextView
           android:id="@+id/text1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="哈哈"
           android:textSize="30sp"
           android:layout_marginTop="10dp"/>
       <TextView
           android:id="@+id/text2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="哈哈哈哈哈"
           android:textSize="24dp"
           android:layout_marginTop="10dp"/>
   </LinearLayout>
</LinearLayout>


private void initSimpleList() {
        //    这三个经常是同时出现的
        List<Map<String,Object>> lists;
        //    定义数据
        String[] theme = {"张三","李四","王五"};
        String[] content ={"我是张三,你好","我是李四,你好","我是王五,你好"};
        int[] imageViews = {R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher};
        lists = new ArrayList<>() ;
        for (int i = 0;i < theme.length;i++) {
            Map<String,Object> map = new HashMap<String, Object>() ;
            map.put("image",imageViews[i]);
            map.put("theme",theme[i]);
            map.put("content",content[i]);
            lists.add(map);
        }

        simpleAdapter = new SimpleAdapter(this,lists,R.layout.simple_listview,new String[]{"image","theme","content"},
                new int[]{R.id.image1,R.id.text1,R.id.text2});
        listView.setAdapter(simpleAdapter);
    }

    public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            @LayoutRes int resource, String[] from, @IdRes int[] to) {
}
  • SimpleAdapter 第一个参数 上下文
  • 第二个参数 含有Map对象的 List
  • 第三个参数 layout
  • 第四个参数 new String[]{}数组 数组的里面的每一项要与第二个参数中的存入map集合的的key值一样,一 一对应
  • new int[]{}数组,数组里面的第三个参数中的item里面的控件id。

相关文章

网友评论

    本文标题:Android ListView

    本文链接:https://www.haomeiwen.com/subject/ilzngftx.html