mercredi 23 mars 2016

OnItemClick listener does not work with custom adapter with checkbox

I am having custom adapter with item with checkbox and textview. Here is the layout of the item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/standard_margin"
>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />
<TextView
    android:id="@+id/checkBoxDesc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_marginLeft="@dimen/new_small_margin"
    android:layout_alignBaseline="@+id/isEbank"
    android:layout_alignBottom="@+id/isEbank"
    android:layout_toRightOf="@+id/isEbank"
    android:layout_alignTop="@+id/isEbank"
    android:gravity="left"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />

Here is how the list looks like:

enter image description here

I like when user clicks on list item , or on checkbox to perform same action. However onItemClick does not fire when user clicks on item.

Here is my code for the view:

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
    View mRoot = inflater.inflate(R.layout.fragment_user, container, false);

    ArrayList<Option> listOptions=new ArrayList<Option>();
    Option optionYes=new Option(true,getResources().getString(R.string.Yes));
    listOptions.add(optionYes);
    Option optionNo=new Option(false,getResources().getString(R.string.No));
    listOptions.add(optionNo);
    listView=(ListView)mRoot.findViewById(R.id.lst);
    adapter=new OptionAdapter(getActivity(),R.layout.item_option,listOptions);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox box = (CheckBox) view.findViewById(R.id.checkBox1);
            box.setChecked(true);
        }
    });
    return mRoot;
}

Here is my code for the adapter:

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    final Option selectedOption = optionsList.get(position);
    ViewHolder holder = null;
    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_option, null);

        holder = new ViewHolder();
        holder.optionDesc = (TextView) convertView.findViewById(R.id.optionDesc);
        holder.optionDesc.setText(selectedOption.getDesc());
        holder.optionCheckBox = (CheckBox) convertView.findViewById(R.id.isEbank);
        holder.optionCheckBox.setFocusable(false);
        holder.optionCheckBox.setFocusableInTouchMode(false);

        convertView.setTag(holder);

        holder.optionCheckBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

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


    return convertView;

}

However it does not work.




Aucun commentaire:

Enregistrer un commentaire