Search code examples
androidsimplecursoradapter

ListView Delete Icon


I am trying to fire a event when a user click the delete icon in the list , But I could not able to do it.I am using a simple cursor adapter with setviewbinder to display items in the list.In the setviewBinder method I set the textview layout clicklistener to be null that's why, view is not clicked when a user click in the textview.When a user is swipe in the list immediately delete icon is appeared.I need to fire a click event in the deleteIcon.Currently, when a user is clicked the delete icon, the whole view is clickable.I need to achieve when a user is clicked the delete icon, need to fired the event, only in the delete icon.

public boolean setViewValue(final View view, final Cursor data, int columnIndex) {
LinearLayout layoutTaskView=(LinearLayout)view.findViewById(R.id.layoutTaskView),
layoutTaskDelete = (LinearLayout) view.findViewById(R.id.LayoutDeleteTask);
boolean crossed = Boolean.valueOf(data.getString(columnIndex));
    if(crossed){
        icon.setImageState(new int[] { android.R.attr.state_checked }, true);
        text1.setPaintFlags(text1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        text2.setPaintFlags(text2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        layoutTaskView.setOnClickListener(null);
    }
    }

Layout Xml File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/content" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/layoutTaskView"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:padding="15dp" >

    <TextView
        android:id="@+id/taskTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/taskName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:paddingLeft="10dp"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/LayoutDeleteTask"
    android:clickable="false">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:padding="15dp"
        android:src="@drawable/checkmark" />

    </LinearLayout>

 </LinearLayout>

enter image description here

ListView xml

<com.myexample.CrossView
    android:id="@+id/crossview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
       </com.myexample.CrossView>

Adapter Code

       String[] from = new  String []  

        {
       DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,
    DatabaseHelper.COLUMN_CROSSED};
     int[] to = new int[] {R.id.taskTime,R.id.taskName, android.R.id.content};
    adapter = new SimpleCursorAdapter(context, R.layout.layout_list_item, data, from, to, 0);
     adapter.setViewBinder(new CrossBinder());
    lv.setAdapter(adapter);

    public void onItemClick(AdapterView<?> parent, View v, final int position, final long id) {

     ImageView icon = (ImageView)v.findViewById(android.R.id.icon);
icon.setOnClickListener(new OnClickListener() {

    @Override
        public void onClick(View v) {
        if(v.getId() == android.R.id.icon){

        }
}
      }

Solution

  • You need to use android:descendantFocusability = "afterDescendants" parameter to root layout of list item. In your case, the root LinearLayout.

    For more information check this link.

    EDIT : remove android:clickable="false" from your LinearLayout containing ImageView. Put android:clickable="true" in ImageView. Moreover, you can use ImageButton instead ImageView.

    P.S. I have suggested to use android:descendantFocusability in main parent LinearLayout.