简体   繁体   中英

Android Listview & Checkbox Issue

I am attempting to check my checkbox every time the listview item is clicked. I can't seem to get it to work. I've cut down the code to what I believe is causing my problem:

MainActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    //What I am trying which does not seem to work
    if(checkBox.isChecked()){
        fld.setToSelected(false);
        Toast.makeText(getApplicationContext(), "Unchecked", Toast.LENGTH_SHORT).show();

    }else{
        fld.setToSelected(true);
        Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show();

    }

    handler.removeCallbacks(runnable);

    super.onListItemClick(l, v, position, id);

    AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.editTextComplete);
    auto.setText("");

    selectedFriend.add(names.get(position));
    names.get(position);

}

FriendListData.java

boolean selected = false;

public boolean isSelected() {
    return selected;
}

public void setToSelected(boolean selected) {
    this.selected = selected;
}

FriendBaseAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder mViewHolder;

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.friend_item, null);
        mViewHolder = new MyViewHolder();
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    mViewHolder.friendName = detail(convertView, R.id.friendName, myList.get(position).getFriend());
    mViewHolder.checkedName = detail(convertView, R.id.checkedFriend, myList.get(position).isSelected());

    return convertView;
}


private CheckBox detail(View v, int resId, boolean isChecked){
    CheckBox checkBox = (CheckBox)v.findViewById(resId);
    checkBox.setChecked(isChecked);
    return checkBox;

}

friend_item.xml

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkedFriend"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:checked="false"
    android:focusable="false"
    android:clickable="false"/>

All help is appreciated. If more code is needed, please ask.

If you want your CheckBox to be clicked when the whole list item is clicked you need to add android:duplicateParentState="true"

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkedFriend"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:checked="false"
    android:duplicateParentState="true"/>

This will make it so every time the list item is clicked all children marked with android:duplicateParentState=true will also receive click events.

Note that if your CheckBox is nested inside of multiple view groups you will have to add the duplicateParentState flag for each ViewGroup it is a part of.

In your onListItemClick(), at least have this code:

checkBox = (CheckBox) v.findViewById(R.id.checkedFriend);
...
checkBox.setChecked(true);

Notice I am referencing the parameter view (v) to find the correct Checkbox in the Listview row.

I know you're cutting code down, but at this point we're trying to find bugs. So the full code may save time in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM