Search code examples
androidonitemclicklisteneronitemclickfocusable

implementing OnItemClickListener, but onItemClick doesn't work


In MainActivity there is onItemClick, and the activity iplements OnItemClickListener. all is very simple, the result of this listener is to print with a Toas a message on screen when an user touch an item of the ListView. But the onItemClick function doesn't work, why? [logCat without errors] thanks in advance.

this is my main activity code:

package com.prendonota;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.prendonota.activeRecord.Nota;
import com.prendonota.crud.NotaCrud;

public class MainActivity extends Activity implements OnItemClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //*** identifico la ListView  che ho usato nell'xml
        ListView listView = (ListView)findViewById(R.id.listaNote);

        //*** accedo al DB per prendere le note
        NotaCrud crud = new NotaCrud(this);
        List<Nota> listaNote = crud.getListNotes();

        //*** tramite il custom adapter inserisco ogni nota nel relativo item
        ListaNotaAdapter adattatore = new ListaNotaAdapter(this, R.layout.row, listaNote);

        //*** inserisco l'adapter personalizzato appena popolato nella ListView iniziale
        listView.setAdapter(adattatore);
        listView.setOnItemClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void addNote(View view){
        Log.i(this.getClass().toString(), "addNote()");
        Intent intent = new Intent( this, InsertNoteActivity.class );
        startActivity(intent);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Log.i(this.getClass().toString(), "onItemClick: +++ log +++");
        Toast.makeText( this, "Hello world", Toast.LENGTH_SHORT ).show();
    }


}

My activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/add_nota"
        android:onClick="addNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/add_nota_label" />

    <ListView
        android:id="@+id/listaNote"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/add_nota" >

    </ListView>

</RelativeLayout>

and my row.xml used for every item bt the adapter:

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



        <!--*** PARTE SUPERIORE ***-->
        <RelativeLayout
            android:id="@+id/button_row_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFEC9">

            <ImageButton
                android:id="@+id/button_row_delete"
                android:onClick="crudClick"
                android:layout_width="35dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:contentDescription="@string/icon_delete"
                android:src="@android:drawable/ic_menu_delete"/>

            <ImageButton
                android:id="@+id/button_row_edit"
                android:onClick="crudClick"
                android:layout_width="35dp"
                android:layout_height="30dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/button_row_delete"
                android:contentDescription="@string/icon_edit"
                android:src="@android:drawable/ic_menu_edit" />

            <TextView
                android:id="@+id/item_data"
                android:layout_width="20dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:textIsSelectable="true"
                android:layout_toLeftOf="@+id/button_row_edit"
                android:padding="10dip"
                android:textSize="12sp" />

        </RelativeLayout>
        <!--*** /PARTE SUPERIORE ***-->


        <!--*** PARTE INFERIORE ***-->
        <RelativeLayout 
            android:id="@+id/oggetto_row_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FCFBB5">
            <TextView
                android:id="@+id/item_oggetto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:textSize="18sp"
                android:paddingTop="5dp"/>
        </RelativeLayout>
        <!--*** /PARTE INFERIORE ***-->



</LinearLayout>

Solution

  • The OnItemClick method won't fire if the views in the list view are focusable.