列表项android中的多个复选框

时间:2014-09-29 21:45:38

标签: android checkbox android-listview

我正在尝试构建一个自定义数组适配器,每个列表项中都有两个复选框。单击复选框时,我想取消选中另一个复选框(如果已选中)。因此,要么既未选中复选框,要么只检查其中一个。

我认为我遇到的问题是我不清楚如何引用onClickListener中的其他复选框。到目前为止,这是我的代码:

public class PetWalkerAdapter extends ArrayAdapter<ParseObject>{
    protected Context mContext;
    protected List<ParseObject> mPets;

    public PetWalkerAdapter(Context context, List<ParseObject> pets) {
        super(context, R.layout.pet_walker_item, pets);
        mContext = context;
        mPets = pets;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView ==  null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pet_walker_item, null);
            holder = new ViewHolder();
            holder.petPictureView = (ImageView) convertView.findViewById(R.id.petPicture);
            holder.petNameLabel = (TextView) convertView.findViewById(R.id.petNameLabel);
            holder.ownerCheckBox = (CheckBox) convertView.findViewById(R.id.ownerCheckBox);
            holder.walkerCheckBox = (CheckBox) convertView.findViewById(R.id.walkerCheckBox);
            convertView.setTag(holder);

            // If owner check is toggled, uncheck walker
            holder.ownerCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CheckBox cb = (CheckBox) view;
                    ViewHolder petHolder = (ViewHolder) cb.getTag();

                    // Here's where the app crashes, 
                    if (petHolder.walkerCheckBox.isChecked()) {
                        petHolder.walkerCheckBox.setChecked(false);
                    }
                }
            });

        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }

        ParseObject pet = mPets.get(position);

        holder.petNameLabel.setText(pet.getString(ParseConstants.KEY_PET_NAME));



        return convertView;
    }

    private static class ViewHolder {
        ImageView petPictureView;
        TextView petNameLabel;
        CheckBox ownerCheckBox;
        CheckBox walkerCheckBox;
    }

    public void refill(List<ParseObject> pets) {
        mPets.clear();
        mPets.addAll(pets);
        notifyDataSetChanged();
    }
}

这里是logcat

10-02 01:53:16.412    1270-1270/com.base8.wlkr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.base8.wlkr, PID: 1270
    java.lang.NullPointerException
            at com.base8.wlkr.adapters.PetWalkerAdapter$1.onClick(PetWalkerAdapter.java:50)
            at android.view.View.performClick(View.java:4438)
            at android.widget.CompoundButton.performClick(CompoundButton.java:100)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

如果您需要更多信息,请与我们联系。任何批评或引用都表示赞赏。谢谢你的期待。

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试为rowview设置标记,然后您尝试检索CheckBox。你应该这样做:
更新即可。就像我说我没有彻底检查代码一样,你所要做的就是做一个小修改。

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView ==  null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.pet_walker_item, null);
        holder = new ViewHolder();
        holder.petPictureView = (ImageView) convertView.findViewById(R.id.petPicture);
        holder.petNameLabel = (TextView) convertView.findViewById(R.id.petNameLabel);
        holder.ownerCheckBox = (CheckBox) convertView.findViewById(R.id.ownerCheckBox);
        holder.walkerCheckBox = (CheckBox) convertView.findViewById(R.id.walkerCheckBox);
        convertView.setTag(holder);

    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }

    setClickListeners(convertView);   

    ParseObject pet = mPets.get(position);
    holder.petNameLabel.setText(pet.getString(ParseConstants.KEY_PET_NAME));
    return convertView;
}

private void setClickListeners(final View convertView)
{
   ViewHolder petHolder = (ViewHolder) convertView.getTag();
   // If owner check is toggled, uncheck walker
   petHolder.ownerCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
              //CheckBox cb = (CheckBox) view;//Wrong

              //Do a check here.
              if(petHolder==null)
              {
                 //or other thing that doesn't lead to a crash.
                 return;
              }       
              // Here's where the app crashes, 
              //here u can do better - like setting different than it was.//
              //not sure if I got what you were trying to do in the next lines.
              petHolder.walkerCheckBox.setChecked(!petHolder.walkerCheckBox.isChecked());
              //the next lines look weird.
              CheckBox wcb = petHolder.walkerCheckBox;
              wcb.setChecked(false);
              if (petHolder.walkerCheckBox.isChecked()) {
                   petHolder.walkerCheckBox.setChecked(false);
              }
        }
    }); 
}

请注意,上述代码未经过测试,因此复制粘贴可能无效。这些是基于您的代码的观察。
为了更好地理解Holder模式,请看这里 Android ViewHolder Pattern