سؤال

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>
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Making text selectable (android:textIsSelectable) allows the TextView to be focussed, and prevents any (short) click events getting to the OnItemClickListener. OnItemLongClickListener will still work.

This represents a serious bug either in the Android libraries (if unintentional) or in the Android documentation (if intentional). In any case, just change android:textIsSelectable to false and all will be well.

I know it's a bit late but, I had the same problem and solved by setting onFocusable=false programmatically, i.e., in my custom ListViewAdapter, on getView(...), I retrieve the ImageButton calling findViewById and then change the focusable value by calling setFocusable(false) and setFocusableInTouchMode(false):

public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null)
        convertView = LayoutInflater.from(context).inflate(R.layout.alvappliances, parent, false);

    ImageButton delete = (ImageButton)convertView.findViewById(R.id.alvaDeleteIB);

    delete.setFocusable(false);
    delete.setFocusableInTouchMode(false);

    return convertView;
}

In your row.xml add following line in your Linear Layout tag

android:descendantFocusability="blocksDescendants"

Try the code below to see if it helps

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top