ListView的适配器使用

(1)ArrayAdapter的使用

<ListView
    android:id="@+id/lv_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
private ListView mlv;
String[] data = new String[15];
private void initView() {
    mlv= (ListView) findViewById(R.id.lv_test);
    ArrayAdapter <String> arAdapter = new ArrayAdapter<String>(this,android.R.layout
    .simple_list_item_1,data);

    mlv.setAdapter(arAdapter);

}
private void initData() {
    for(int i =0;i<data.length;i++){
        data[i]="项目"+i;
    }
}
(2)SimpleAdapter的使用

item_list.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:orientation="horizontal">

    <ImageView
        android:id="@+id/item_draw"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/item_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>
<ListView
    android:id="@+id/lv_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
private ListView mlv;
private List<Map<String,Objects>> data=new ArrayList<Map<String,Objects>>();
private int[] itemid=new int[]{R.id.item_text,R.id.item_draw};
private String[] itemKey = new String[]{"name","draw"};
private void initData() {
    Map<String,Objects> map=new HashMap<String,Objects>();;
    for(int i =0;i<15;i++){
        map.put("name","emmm"+i);
        map.put("draw",R.drawable.cat);
        data.add(map);
    }
}

private void initView() {
    mlv= (ListView) findViewById(R.id.lv_test);
    SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.item_list
    ,itemKey,itemid);
    mlv.setAdapter(simpleAdapter);

}
(3)自定义Adapter的使用

首先自己写一个adapter

package com.example.zxt.day1_25;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/1/25.
 */
public class adpter extends BaseAdapter {

    private Context context;
    private List<Map<String,Object>> dataList;

    public adpter(Context context, List<Map<String, Object>> dataList) {
        this.context = context;
        this.dataList = dataList;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_list,null);
        }

        ImageView mimg = (ImageView) convertView.findViewById(R.id.item_draw);
        TextView mtv= (TextView) convertView.findViewById(R.id.item_text);
        Button mbtn= (Button) convertView.findViewById(R.id.item_btn);

        Map<String,Object>map=dataList.get(position);
        mimg.setImageResource((Integer)map.get("draw"));
        mtv.setText((String) map.get("name"));

        mbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"删除了第"+position+"行",Toast.LENGTH_LONG).show();
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
}
item_list.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:orientation="horizontal">

    <ImageView
        android:id="@+id/item_draw"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/item_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/item_btn"
        android:text="删除"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
private void initView() {
    mlv= (ListView) findViewById(R.id.lv_test);
    adpter simpleAdapter = new adpter(this,data);
    mlv.setAdapter(simpleAdapter);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值