sorry_I_wont
sorry_I_wont

Reputation: 658

ListView of CheckBox does not respond to clicks

I have a listview of checkboxes in a fragment, and I have implemented the adapter class too, and it populates the data correctly on the listview, and when I click on a checkbox in the list, the tick will go off and on, but I can't get it to perform anything else (like putting log messages). Here's my code:

Fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_contact_checklist, container, false);
    // Inflate the layout for this fragment
    database = new ContactDatabase(getActivity().getApplicationContext());
    database.open();
    ArrayList<Contact> contactList = database.getAllContacts();

    checkBoxListView = (ListView) view.findViewById(R.id.contacts_listview);
    ArrayContactCheckboxAdapter checkBoxListAdapter =
            new ArrayContactCheckboxAdapter(getActivity(), R.layout.item_checkbox, contactList);
    checkBoxListView.setAdapter(checkBoxListAdapter);
    // Setting up listener for items that are clicked on the list
    checkBoxListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CheckBox nameCheckBox = (CheckBox) view;
                    String name = nameCheckBox.getText().toString();
                    Log.d(TAG, name + " This does not get printed");
                }
            });
    Log.d(TAG, " This gets printed");
    return view;
}

Adapter

public class ArrayContactCheckboxAdapter extends ArrayAdapter<Contact> {

public ArrayContactCheckboxAdapter(Context context, int resource, List<Contact> objects) {
    super(context, resource, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Contact c = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.item_checkbox, parent, false);
    }
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox1);
    checkBox.setText(c.getName());
    return convertView;         // Return the completed view to render on screen
}

}

Ideas? I need the "This does not get printed" to get printed in the log!

Upvotes: 0

Views: 52

Answers (2)

Aldi Renaldi Gunawan
Aldi Renaldi Gunawan

Reputation: 510

try this ?

public class ArrayContactCheckboxAdapter extends ArrayAdapter<Contact> {

public ArrayContactCheckboxAdapter(Context context, int resource, List<Contact> objects) {
    super(context, resource, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Contact c = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.item_checkbox, parent, false);
    }
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox1);
    checkBox.setText(c.getName());

    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) 
                    ((FragmentName)mContext).printName(c.getName());
            }
        });
    return convertView;         // Return the completed view to render on screen
    }

on your Fragment put this function :

public void printName(String name)
{
     Log.d(TAG, name + " This does not get printed");
}

Upvotes: 0

Moien.Dev
Moien.Dev

Reputation: 1058

You need to add the the focusable flag and clickable flag to the checkbox

android:focusable="false"
android:clickable="false"

Than you control the checkbox state from within the ListView onListItemClick event.

public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Get related checkbox and change flag status..
CheckBox cb = (CheckBox)v.findViewById(R.id.rowDone);
cb.setChecked(!cb.isChecked());
}

It should be using findViewById subclass of View object

Upvotes: 1

Related Questions