日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android listview 中的checkbox,Android中ListView与CheckBox的使用,及问题解决

發布時間:2025/3/20 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android listview 中的checkbox,Android中ListView与CheckBox的使用,及问题解决 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近自己在編寫有關SIM卡管理的軟件做練習,其中使用到了ListView與CheckBox的的組合,遇到了和大家有同樣的問題:

1.選中一個checkbox對應位置的其他checkbox也會被選中

2.選中一個checkbox之后,滑動滾動條,之前選中的checkbox會莫名其妙的取消選中

于是網上搜之,具體的問題所在,網上答得也是很含糊,由于接觸android時間不長,

其中的原因我也不是很清楚,不過總的來說代碼還是搞定了

寫好的代碼供大家參考下:

import java.util.HashMap;

import java.util.List;

import com.rice.activity.R;

import com.rice.domain.ViewHolder;

import android.content.Context;

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 android.widget.CompoundButton.OnCheckedChangeListener;

public class MyAdapter extends BaseAdapter

{

private Context context;

private

List

Object>> data;

//用來記錄所有ListView記錄對應checkbox的狀態

public HashMap

Boolean> isSelected;

private int resource;

private int to[];

private String from[];

private LayoutInflater inflater = null;

private ViewHolder holder = null;

//構造函數

public MyAdapter(Context context,

List

Object>> data, int resource, String

from[], int to[])

{

this.context = context;

this.data = data;

this.resource = resource;

this.from = new

String[from.length];

this.to = new

int[to.length];

System.arraycopy(from, 0,

this.from, 0, from.length);

System.arraycopy(to, 0,

this.to, 0, to.length);

init();

}

public HashMap

Boolean> getIsSelected()

{

return isSelected;

}

//初始化設置所有checkbox都為未選擇狀態

private void init()

{

isSelected = new

HashMap();

for (int i = 0; i

< data.size(); i++)

{

isSelected.put(i,

false);

}

}

@Override

public int getCount()

{

return data.size();

}

@Override

public Object getItem(int arg0)

{

return data.get(arg0);

}

@Override

public long getItemId(int arg0)

{

return 0;

}

@Override

public View getView(final int position, View

view, ViewGroup arg2)

{

holder = null;

if(null == holder)

{

//總是新建一個ViewHolder對象,用來保存每一個listview條目的信息

holder = new

ViewHolder();

if(null ==

view)

{

inflater

= LayoutInflater.from(context);

view

= inflater.inflate(resource, null);

}

holder.name =

(TextView) view.findViewById(R.id.name);

holder.mobile

= (TextView) view.findViewById(R.id.mobile);

holder.checkBox

= (CheckBox) view.findViewById(R.id.item_checkBox);

}

HashMap

Object> map = data.get(position);

if(null != map)

{

String name =

(String) map.get("name");

String mobile

= (String) map.get("mobile");

holder.name.setText(name);

holder.mobile.setText(mobile);

}

//添加checkBox監聽

holder.checkBox.setOnCheckedChangeListener(new

OnCheckedChangeListener()

{

@Override

public void

onCheckedChanged(CompoundButton arg0, boolean isCheck)

{

if(isCheck)

{

isSelected.put(position,

true);

//System.out.println("add

checked=" + position);

}

else

if(!isCheck)

{

isSelected.put(position,

false);

//System.out.println("remove

checked=" + position);

}

}

});

//根據isSelected中記錄的信息,設置checkbox的狀態

holder.checkBox.setChecked(isSelected.get(position));

return view;

}

}

總結

以上是生活随笔為你收集整理的android listview 中的checkbox,Android中ListView与CheckBox的使用,及问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。