kiduxa
kiduxa

Reputation: 3351

setOnItemClickListener NOT WORKING and setOnItemLongClickListener WORKS in a ListView

I hope someone can help me out with this. I have a list view and I want each row of the list be clickeable and long clickeable. The setOnItemLongClickListener is working but I cant get the row clickeable. I have seen this post ListView with clickable/editable widget and similars but I dont have items focusable in my row, so I dont know what else could be.

This is my definition of listView in the xml:

<ListView
    android:id="@id/android:list"
    android:textFilterEnabled="true"
    android:layout_width="match_parent"
    android:fadeScrollbars="false"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>

This is the definition of my row (kind of ugly):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:longClickable="true"
android:descendantFocusability="blocksDescendants">


<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"

    tools:ignore="UselessParent" >

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
             android:id="@+id/imageViewAgenda"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:contentDescription="@string/historicos"
             android:src="@android:drawable/ic_menu_agenda" />

        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/avisoVerdeFilaHist"
                android:textIsSelectable="false"
                style="@style/CuadroNotificacion"
                android:background="@drawable/borde_blanco"/>

            <TextView
                android:id="@+id/avisoRojoFilaHist"
                android:textIsSelectable="false"
                style="@style/CuadroNotificacion"
                android:background="@drawable/borde_blanco"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/linearLayoutTarea"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textViewTarea"
                android:textIsSelectable="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginRight="5dp"
                android:text="@string/tarea2"/>

            <TextView
                android:id="@+id/textViewTitulo"
                android:textIsSelectable="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titulo"/>

        </LinearLayout>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0,1">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textViewFil1Col1"
                    android:textIsSelectable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="f1c1"/>

                <TextView
                    android:id="@+id/textViewFil1Col2"
                    android:textIsSelectable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="f1c2"/>

            </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textViewFil2Col1"
                    android:textIsSelectable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="f2c1"/>

                <TextView
                    android:id="@+id/textViewFil2Col2"
                    android:textIsSelectable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="f2c2"/>

            </TableRow>
        </TableLayout>
    </LinearLayout>

</TableRow>
</TableLayout>

If you are wondering why I'm using a tableLayout is because the android:stretchColumns="1" property. Note that Im using android:descendantFocusability="blocksDescendants". just in case.

This is my custom cursor adapter:

public class ListaTareasCursorAdapter extends SimpleCursorAdapter implements Filterable{

private Context context;
private int layout;
private String[] from;
private int[] to;
private FragmentManager fragmentManager;


public ListaTareasCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to, int flags, FragmentManager fragmentManager) {

    super(context, layout, c, from, to, flags);

    this.context = context;
    this.layout = layout;
    this.from = from;
    this.to = to;
    this.fragmentManager=fragmentManager;
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    return v;
}

@Override
public void bindView(View v, Context context, Cursor cursor) { 

    //Columna de la BD que queremos recuperar
    String columna = null;

    //Indice de la columna de BD 
    int nombreCol = 0;

    //Resultado de obtener el indice de la columna de BD 
    String nombre = null;

    //Nombre del textView en nuestro Layout donde queremos
    //que aparezca el resultado.
    TextView nombre_text = null;


    String nombreCompleto = ""; //almacena nombre completo del empleado
    String fechaCompleta = ""; //almacena fecha y hora
    String nombreTarea = ""; //almacena nombre de la tarea

    //Para cada valor de la BD solicitado, lo mostramos en el text view.
    for (int i=0; i<from.length; i++){
        columna = from[i];
        nombreCol = cursor.getColumnIndex(columna);
        nombre = cursor.getString(nombreCol);

        if(nombre==null){
            nombre = "--";
        }

        if(columna.equals(Tarea.NOMBRE)){
            nombreTarea = nombre;
        }

        if(columna.equals(Tarea.NOMBRE_EMPLEADO)){
            nombreCompleto = nombre;
        }

        if(columna.equals(Tarea.APELLIDO_EMPLEADO)){
            nombreCompleto = nombreCompleto + " " + nombre;
            nombre = nombreCompleto;
        }

        if(columna.equals(Tarea.FECHA)){
            fechaCompleta = "Pautado: " +nombre;
        }

        if(columna.equals(Tarea.HORA)){
            fechaCompleta = fechaCompleta + " " + nombre;
            nombre = fechaCompleta;
        }


        if(columna.equals(Tarea.FECHA_FINALIZACION)){
            nombre = "Finalizado: " + nombre;
        }

        nombre_text = (TextView) v.findViewById(to[i]);
        if (nombre_text != null) {
            nombre_text.setText(nombre);
        }
    }

}
}

And here is where I call the listeners:

public class TareasActivity extends ListFragment{
....


        ListView lvTareas = (ListView) view.findViewById (android.R.id.list);


        final ListaTareasCursorAdapter listCursorAdapterTareas = new ListaTareasCursorAdapter(context, R.layout.activity_fila_tarea, mCursorTareas, from, to, 0,this.getFragmentManager());
        lvTareas.setAdapter(listCursorAdapterTareas);
        lvTareas.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                // TODO Auto-generated method stub

                Log.v("CLICK","pos"+" "+pos);


            }
        }); 

        lvTareas.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                // TODO Auto-generated method stub

                Log.v("long clicked","pos"+" "+pos);
                return true;


            }
        }); 

}

There must be something dumb that I'm missing.

Any idea will be appreciated, thanks for reading.

MORE INFO: I dont know if this can be usefull but I'm using TabHost.

Upvotes: 1

Views: 5129

Answers (4)

Supriya
Supriya

Reputation: 24

For me it is worked with adding

android:descendantFocusability="blocksDescendants"

in parent tag in row_list.xml. In my row_list.xml there is toggle button.

Upvotes: 0

kiduxa
kiduxa

Reputation: 3351

Turns out that my problem was with android:longClickable="true" in my TableLayout definition of my row. I changed to android:clickable="false". And overrided onListItemClick method as suggested by @yyunikov. Now I have:

myRow.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:clickable="false"
android:descendantFocusability="blocksDescendants">


<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"

tools:ignore="UselessParent" >

....

</TableRow>
</TableLayout>

And in my ListFragment:

public class TareasActivity extends ListFragment{

....

    ListView lvTareas = (ListView) view.findViewById (android.R.id.list);


    final ListaTareasCursorAdapter listCursorAdapterTareas = new ListaTareasCursorAdapter(context, R.layout.activity_fila_tarea, mCursorTareas, from, to, 0,this.getFragmentManager());
    lvTareas.setAdapter(listCursorAdapterTareas);


    lvTareas.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.v("long clicked","pos"+" "+pos);
            return true;


        }
    }); 

}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Object a = getListView().getItemAtPosition(position);
     Log.v("clicked","pos"+" "+position);

}

Now, when I click or long click a row, I get the calls right. I found the idea from ListFragment OnListItemClick ignoring me, Not focusable views

Upvotes: 1

KBusc
KBusc

Reputation: 663

Instead of using onItemClick try OnItemSelectedListener for your ListView. I had to do something similar in my project and had similar issues and this is how I was able to solve it. Here is a some example code that I know works and may help you.

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {

                    Object selected = lv.getItemAtPosition(position);
                    Log.e("DEBUG", selected.toString())
                }

            });

Upvotes: 0

yyunikov
yyunikov

Reputation: 5897

You need to override onListItemClick method in ListFragment instead of using onItemClick. You don't need to call setOnItemClickListener.

onListItemClick

Upvotes: 2

Related Questions