列表常用Adapter,自定义Adapter

  1. ArrayAdapter实现单行文本ListView:
    (1)定义一个数组存放ListView中item的内容
    (2)通过实现ArrayAdapter的构造方法创建一个ArrayAdapter对象
    (3)通过ListView的setAdapter()方法绑定ArrayAdapter
    在这里插入图片描述
    在这里插入图片描述
    <ListView
        android:id="@+id/lv_array"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#d9d9d9"
        android:dividerHeight="1dp"
        >
    </ListView>

在这里插入图片描述

后台代码:
使用系统自带布局文件的不同效果:
A、android.R.layout.simple_list_item_1
B、android.R.layout.simple_list_item_checked
C、android.R.layout.simple_list_item_multiple_choice
D、android.R.layout.simple_list_item_single_choice

    private  String[] sArray = null;
    //List<Object>  //不定长
    //List<double>
    //List<String>
    private List<String> listTitle=null;
  private void init() {
        //1.创建数据源
        sArray = new String[10];
        for(int i=0;i<10;i++){
            sArray[i]="hello"+(i+1);
        };
        //2.创建适配器
        //第一个参数this,第二个参数布局文件:android.R.layout.simple_list_item_1为系统自带文件
        第三个参数数据源:sArray为数据源
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                sArray
                );

        //3.将适配器绑定到ListView
        ListView lst_Array = ((ListView) findViewById(R.id.lv_array));
        //进行绑定
        lst_Array.setAdapter(arrayAdapter);

//设置多选模式

        //2.创建适配器
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_multiple_choice,
                listTitle
                );
        //3.将适配器绑定到ListView事件
        ListView lst_Arraty = (ListView) findViewById(R.id.lv_array);
        //设置选择模式,多选CHOICE_MODE
        lst_Arraty.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);;
        lst_Arraty.setAdapter(arrayAdapter);

//设置自定义列表
activity_main.xml

    <ListView
        android:id="@+id/lv_simple"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="1dp"
        android:divider="#d9d9d9"
        >
    </ListView>

在这里插入图片描述

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    >
    //gravity:线性布局里的控件对齐方式,在TextVIew是文本里的内容
    <ImageView
        android:id="@+id/iv_item"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@drawable/wx"
        android:layout_margin="8dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Echat"
        android:textSize="20sp"
        android:layout_margin="8dp"
        />
</LinearLayout>

在这里插入图片描述
在这里插入图片描述
后台代码

public class MainActivity extends AppCompatActivity {
    private List<HashMap<String,Object>> lst = null;
    private int[] iArray = {R.drawable.dz,
            R.drawable.jd,
            R.drawable.qq,
            R.drawable.tm,
            R.drawable.uc,
            R.drawable.wx,
            R.drawable.xl};
   private String[] sArray={"斗地主","京东","球球","大猫","UC浏览器","微信","新浪"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init() {
        //创建数据源
        lst = new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<7;i++){
            HashMap<String,Object> map = new HashMap<String, Object>();
            map.put("icon",iArray[i]);
            map.put("title",sArray[i]);
            lst.add(map);
        }
        //创建Adapter,第一个上下文this,第二个数据源,第三个每一行的布局文件
        //
        SimpleAdapter adapter = new SimpleAdapter(this,
                lst,R.layout.list_item,new String[]{"icon","title"},
                new int[]{R.id.iv_item,R.id.tv_item}
                );
        //绑定到ListView方法
        final ListView iv_simple = (ListView) findViewById(R.id.lv_simple);
        iv_simple.setAdapter(adapter);
        //添加事件
        iv_simple.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String strObj = iv_simple.getItemAtPosition(position).toString();
//点击获取当前一行,给map是一个HashMap数组

                HashMap<String,Object> map = (HashMap<String,Object>) iv_simple.getItemAtPosition(position);
                Toast.makeText(MainActivity.this,
                        map.get("title").toString(),  //显示当前map中的title
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值