初学安卓,项目中有用到ListView+CheckBox,下面来分享一下代码:
1.创建checkboxlist_item.xml存放listItem,代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="40dip" android:gravity="center" android:orientation="horizontal" android:weightSum="10"> <TextView android:id="@+id/item_tv" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginLeft="2dip" android:layout_weight="2" android:textColor="#000000"></TextView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="6" android:gravity="center"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false"></CheckBox> </LinearLayout> </LinearLayout> </LinearLayout>
2.创建数组对象Record
package com.callrecording; import android.graphics.drawable.Drawable; import java.io.Serializable; /** * Created by lenovo on 2018/10/11. */ public class Record implements Serializable { private String recordId; private String recordName; private boolean isChcked; //保存复选框的状态 public void setRecordName(String recordName) { this.recordName = recordName; } public String getRecordName() { return recordName; } public void setIsChcked(boolean isChcked) { this.isChcked = isChcked; } public boolean getIsChcked() { return isChcked; } public String getRecordId() { return recordId; } }
之前试过不创建这个对象,没有这个对象会导致点击复选框的时候获取不到数据,而且会出现复选框勾选错乱的情况
3.创建适配器CheckBoxAdapter :
package com.callrecording; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by lenovo on 2018/10/11. */ public class CheckBoxAdapter extends BaseAdapter { private static String TAG = "CheckBoxAdapter"; private Context context; private List<Record> list; private LayoutInflater layoutInflater; private TextView tv; private CheckBox cb; public CheckBoxAdapter(Context context,List<Record> list) { this.context = context; this.list = list;//list中checkbox状态为false layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null != list?list.get(position):null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewCache viewCache = new ViewCache(); Record record=(Record)getItem(position); // 判断是不是第一次进来 if(convertView == null){ convertView = layoutInflater.inflate(R.layout.checkboxlist_item, null); tv = (TextView) convertView.findViewById(R.id.item_tv); cb = (CheckBox) convertView.findViewById(R.id.cb); viewCache.tv = tv; viewCache.cb = cb; convertView.setTag(viewCache); final ViewCache finalViewHolder = viewCache; viewCache.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { Record info = (Record) finalViewHolder.cb.getTag(); info.setIsChcked(compoundButton.isChecked()); } }); convertView.setTag(viewCache); viewCache.cb.setTag(record); }else { viewCache = (ViewCache) convertView.getTag(); viewCache.cb.setTag(record); } viewCache.tv.setText(record.getRecordName()); viewCache.cb.setChecked(record.getIsChcked()); return convertView; } public class ViewCache{ TextView tv; CheckBox cb; } }
4.加载并显示数据:
public void showList(List l) { try { list = new ArrayList<Record>(); if(l!=null) { for (int i = 0; i < l.size(); i++) { Record record = new Record(); record.setRecordName(l.get(i).toString()); record.setIsChcked(false); list.add(record); }//初始化数据 } listview = (ListView)findViewById(R.id.recordlistview); cbAdapter = new CheckBoxAdapter(this,list); listview.setAdapter(cbAdapter); Log.i(TAG,"列表信息:" +list.toString()); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { CheckBoxAdapter.ViewCache viewCache = (CheckBoxAdapter.ViewCache) view.getTag(); viewCache.cb.toggle(); list.get(position).setIsChcked(viewCache.cb.isChecked()); String nameStr=list.get(position).getRecordName(); name=nameStr.substring(nameStr.indexOf(" ")+2,nameStr.length()); Log.i(TAG,"文件名:" +name); cbAdapter.notifyDataSetChanged(); } }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"错误信息:" +e.toString()); } Toast.makeText(Call.this, "加载完成!" , Toast.LENGTH_SHORT).show(); }
我是初学菜鸟,不对的地方请各位大神不吝赐教,不喜勿喷!