嗨,在下面的带有复选框的用户代码列表中。当我选择第一个用户时,它正确显示,也与第二个用户相同。
现在我想一次返回第一个用户和第二个用户将这两个值存储到一个变量中。
的java
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;
}
}
答案 0 :(得分:1)
要在“列表视图”中获取所选项目,请执行以下操作:用这个:
List<String> checkUsers = new ArrayList<String>();
SparseBooleanArray checkedPositions = listView.getCheckedItemPositions();
for (int i = 0; i < yourListView.getCount(); i++) {
if (checkedPositions.get(i) == true) {
checkUsers.add(yourListView.get(i));
}
}
然后,您拥有来自“ListView”的所有已检查用户&#39;在列表中。如果您能够使用&#39; ListView&#39;为了你的目的然后好吧。如果没有,您可以将所有项目添加到由字符分隔的字符串中,例如&#34; - &#34;像这样:
String users = "";
for(int i = 0; i<checkUsers.size(); i++){
if(i = 0){
users = checkUsers.get(i);
}else{
users = users + "-" + checkUsers.get(i);
}
}
并且您将使用&#34; - &#34;分隔相同字符串中的所有用户。这样您就可以再次拆分用户并逐一使用。
希望它有所帮助!祝你好运!