如何在 android 中使用自定义适配器获取多个复选框值
how to get the the multiple checkbox values using custom adapter in android
您好,在下面的代码中,我用 checkboxes.Now 显示文本视图 当我选择单个值时,它 return 文本视图的名称也与第二个文本视图名称相同。
现在我想 return 基于复选框选择的值一次多个文本视图名称。
Eg:suppose两个用户我选择user1,user2这两个我选择了我想return这两个名字在一个变量中。
java
public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String check;
ListView users;
int position;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friend = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friend = friends;
}
public int getCount() {
return friend.length;
}
public FriendInfo getItem(int position) {
return friend[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friend[position].userName);
holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
checkBoxState = new boolean[friend.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
check=friend[position].userName;
}
Toast.makeText(getApplicationContext(),friend[position].userName+"checked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
在您的适配器中声明并初始化它...
ArrayList<String> checkedFriends = new ArrayList<String>();
用这个替换您的点击侦听器....
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
checkedFriends.add(friend[position].userName);
check=friend[position].userName;
} else {
checkedFriends.remove(friend[position].userName);
}
Toast.makeText(getApplicationContext(),checkedFriends.toString()+" checked", Toast.LENGTH_LONG).show();
}
});
您好,在下面的代码中,我用 checkboxes.Now 显示文本视图 当我选择单个值时,它 return 文本视图的名称也与第二个文本视图名称相同。
现在我想 return 基于复选框选择的值一次多个文本视图名称。 Eg:suppose两个用户我选择user1,user2这两个我选择了我想return这两个名字在一个变量中。
java
public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String check;
ListView users;
int position;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friend = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friend = friends;
}
public int getCount() {
return friend.length;
}
public FriendInfo getItem(int position) {
return friend[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friend[position].userName);
holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
checkBoxState = new boolean[friend.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
check=friend[position].userName;
}
Toast.makeText(getApplicationContext(),friend[position].userName+"checked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
在您的适配器中声明并初始化它...
ArrayList<String> checkedFriends = new ArrayList<String>();
用这个替换您的点击侦听器....
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
checkedFriends.add(friend[position].userName);
check=friend[position].userName;
} else {
checkedFriends.remove(friend[position].userName);
}
Toast.makeText(getApplicationContext(),checkedFriends.toString()+" checked", Toast.LENGTH_LONG).show();
}
});