【问题标题】:How to add the friend with checkbox in android如何在android中添加带有复选框的朋友
【发布时间】:2015-01-09 18:58:29
【问题描述】:

嗨,在下面的代码中,我使用自定义适配器来显示带有图像和复选框的名称。现在,我的问题是基于我想用复选框添加的朋友位置。

我的布局是这样的:

带复选框的图片好友名

现在,我想根据我想要返回的朋友位置,是否将复选框添加到朋友。假设如果我选择用户名:user1,这个已被选中。然后我想返回带有选中位置的用户名,我我正在使用列表视图。

java

private class FriendListAdapter extends BaseAdapter {       
    class ViewHolder {
        TextView text;
        ImageView icon;
        CheckBox check1;                                                
    }
    private LayoutInflater mInflater;
    private Bitmap mOnlineIcon;
    private Bitmap mOfflineIcon;            
    private FriendInfo[] friends = 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.friends = friends;             
    }


    public int getCount() {         
        return friends.length;
    }


    public FriendInfo getItem(int position) {               
        return friends[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(friends[position].userName);
        holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
        holder.check1.setChecked(checkBoxState[position]);              
        holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkBoxState[position] = isChecked;

            }    
        });                             
        return convertView;
    }    
}

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    你可以使用

     holder.check1.setTag(getItem(position));
    

    为每一行设置一个标签,然后yoju可以使用

    检查标签
     int position = (Integer) buttonView.getTag();
    

    这将返回位置

    【讨论】:

    • 嗨 Neha Shukla 上面的代码我写了我应该在哪里添加的代码
    • holder.check1.setTag(getItem(position)); 这个应该加在`holder.check1.setChecked(checkBoxState[position]);`之后,并且要获得位置int position = (Integer) buttonView.getTag(); public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkBoxState[position] = isChecked; } 这个函数中添加这个代码×一个错误提交评论时出错×提交评论时出错×提交评论时出错
    • 这个函数×提交评论时出错
    • holder.check1.setTag(getItem(position)); 这应该添加在holder.check1.setChecked(checkBoxState[position]); 之后并获得位置int position = (Integer) buttonView.getTag(); 在这个函数中添加这个代码public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkBoxState[position] = isChecked; }
    • holder.check1.setChecked(checkBoxState[position]);
    【解决方案2】:
     holder.text.setText(friends[position].userName);
        holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
        holder.check1.setChecked(checkBoxState[position]); 
        holder.check1.setTag(position);     
        holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int position = (Integer) buttonView.getTag();
    
    
    
                checkBoxState[position] = isChecked;
    
            }    
        });            
    

    【讨论】:

    • holder.check1.setChecked(checkBoxState[position]);这一行给出错误
    • 如果你想选中复选框,即如果你想保留一个勾号然后将代码更改为holder.check1.setChecked(true);,如果你想保持它不选中然后添加holder.check1.setChecked(false);
    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多