0

I've read some SO questions like this and this but still couldn't figure out whats wrong with my code. I have a ListFragment, and each row has a TextView and a CheckBox. Clicking the CheckBox is working, but clicking on the TextView does nothing, and OnListItemClick doesn't get called. I've also tried to dynamically add an OnClickListener, which make it work, but it's not the correct way to do it, and it also lacks the GUI feedback of the click (item being "highlighted" for a sec).

This is my textview_with_checkbox.XML I'm using for each item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:gravity="left"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >
<TextView
    android:id="@+id/textview_event_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingLeft="3dp"
    android:paddingTop="5dp"
    android:focusable="true"
    android:singleLine="false" />

<CheckBox
    android:id="@+id/checkbox_for_event_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.05"
    android:focusable="false"
    android:clickable="false" />

</LinearLayout>

now the code:

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);
    displayEventsLogFiles();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);

    String chosenItem = (String) getListAdapter().getItem(position);
    mCallBack.onEventItemSelected(chosenItem);

}

static class myCustomAdapterViewHolder
{
    public TextView eventName;
    public CheckBox eventCheckBox;
}

private class myCustomAdapter extends ArrayAdapter<String>
{   
    private ArrayList<String> sortedList;
    private Context m_oContext;
    List<String> m_oValues = null;

    public myCustomAdapter(Context cont, int viewResId, List<String> objects) 
    {
        super(cont, viewResId, objects);
        m_oContext = cont;
        m_oValues = objects;
        sortedList = new ArrayList<String>();
        for (String str : objects)
            sortedList.add(str);
        java.util.Collections.sort(sortedList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View l_oRowView = convertView;
        myCustomAdapterViewHolder l_oInitializedViewHolder = null;
        final int l_nPosition = position;
        // Use convertView if possible, otherwise inflate a view:
        if (l_oRowView == null)
        {
            LayoutInflater inflater = (LayoutInflater) m_oContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            l_oRowView = inflater.inflate(R.layout.textview_with_checkbox, parent, false);

            // PlaceHolder pattern:
            final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
            l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
            l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);

            l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox)v;
                    //cb.setChecked(!cb.isChecked());

                    String l_sFilename = l_oViewHolder.eventName.getText().toString();
                    if (cb.isChecked())
                    {
                        if (!m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.add(l_sFilename);
                    }
                    else
                    {
                        if (m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.remove(l_sFilename);
                    }
                }
            });
            l_oViewHolder.eventCheckBox.setFocusable(false);
            //l_oViewHolder.eventCheckBox.setClickable(false);
            // "Add" the viewHolder as a tag:
            l_oRowView.setTag(l_oViewHolder);
            l_oInitializedViewHolder = l_oViewHolder;
        }
        else
            l_oInitializedViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();

        // By now, the rowView is initialized, just get the viewHolder and then get the views from it, to update:
        //myCustomAdapterViewHolder l_oViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
        /*l_oInitializedViewHolder.eventName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCallBack.onEventItemSelected((String)getListAdapter().getItem(l_nPosition));

            }
        });*/
        l_oInitializedViewHolder.eventName.setText(m_oValues.get(position));
        return l_oRowView;
        //return super.getView();
    }

    public myCustomAdapter(Context cont, int viewResId, String[] strings) 
    {
        this(cont, viewResId, Arrays.asList(strings));
    }

    @Override
    public String getItem(int position) 
    {
        return sortedList.get(position);
    }

    @Override
    public int getPosition(String item) 
    {
        return sortedList.indexOf(item);
    }

}

What am I doing wrong here? All I want is to be able to select "files" for deletion, using the CheckBoxes

Community
  • 1
  • 1
Hummus
  • 559
  • 1
  • 9
  • 21
  • As I know that CheckBox in ListView does not work properly so I have a better solution with MultichoiceItem. If you want I can post this.. – Avishek Das Mar 25 '15 at 08:38
  • try this it's best answer http://stackoverflow.com/questions/18162931/get-selected-item-using-checkbox-in-listview – Jignesh Jain Mar 25 '15 at 08:43

2 Answers2

0

Try only using only onclick for textview and checkbox, not onListItemClick -which you can remove- as well, so you should change some properties for the linear layout as well.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:gravity="left"
    android:orientation="horizontal" >
<TextView
    android:id="@+id/textview_event_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingLeft="3dp"
    android:paddingTop="5dp"
    android:focusable="true"
    android:singleLine="false" />

<CheckBox
    android:id="@+id/checkbox_for_event_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.05"
    android:focusable="false"
    android:clickable="false" />

</LinearLayout>

Implement in the adapter

    // PlaceHolder pattern:
        final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
        l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);


         l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);

  l_oViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     // your code for textview click
               }
           }
            l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox)v;
                    //cb.setChecked(!cb.isChecked());

                    String l_sFilename = l_oViewHolder.eventName.getText().toString();
                    if (cb.isChecked())
                    {
                        if (!m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.add(l_sFilename);
                    }
                    else
                    {
                        if (m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.remove(l_sFilename);
                    }
                }
            });
            l_oViewHolder.eventCheckBox.setFocusable(false);
A B
  • 338
  • 3
  • 10
  • Well it seems that changing the XML was enough, the layout should have been non-focusable and non clickable, now the `onListItemClick` gets called and I get the proper GUI feedback. So it solved my problem, without the need to use `l_oViewHolder.eventName.setOnClickListener` which seem to me a bit odd (maybe it's the correct way). Thanks – Hummus Mar 25 '15 at 09:16
  • Great that it was useful! It's just another aproach using setOnClickListener on a textview. If it worked without this, then good. Personally I would also prefer to use the onClick on a listview item as well, but just wanted to give you an alternative :). – A B Mar 25 '15 at 09:21
  • Well as you can see in the code I've tried using it before (it's commented) but there was no GUI feedback when clicking an item. Maybe it was due to the XML settings that was incorrect for my needs. – Hummus Mar 25 '15 at 09:23
0

add following attribute:

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

for example in my xml file:

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:button="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:buttonTint="@color/gray"
android:focusable="false"
android:clickable="false"
/>

and in my code:

public class MyTestFragment extends ListFragment {
   .....
   @Override
   public void onListItemClick(ListView l, View v, int position, long id) {

   }
}
M.Namjo
  • 374
  • 2
  • 13