简体   繁体   中英

setOnItemClickListener doesn't work

I have a listview with an adapter but when I click on the listview I have no response. I have tried with some of the responses of other posts like setOnItemClickListener Not Working In Custom Listview or Android - setOnItemClickListener event of ListView not working in Fragment but I can't resolve the problem. I show you my code.

First, the mainActivity

public class MyListActivity extends ListActivity {

private MainView view;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

   Bundle b=this.getIntent().getExtras();
   String[] array=b.getStringArray("listaEventos");

   /**A continuación, generamos los distintos valores que ocuparán el LinearLayout, concretamente
    * las categorías de los eventos.
    */

   String[] values = new String[] { "Actividades vacacionales", "Aire libre y excursiones", "Cine",
    "Conferencias y otros", "Congresos y jornadas", "Cursos y talleres", "Deporte", "Escénicas",
    "Exposiciones", "Ferias", "Fiestas y festivales", "Música", "Viajes", "Otras" };

   /**
    * Llamamos al adapter personalizado para mostrar la vista.
    */
   MyListCategoryArrayAdapter adapter = new MyListCategoryArrayAdapter(this, values, array);
   setListAdapter(adapter);

   //Log.d("YA SE HA ADAPTADO", "YA SE HA ADAPTADO");

   view = (MainView)View.inflate(this, R.layout.all_products, null);
   //view.setViewListener(viewListener);
   setContentView(view);

}

Then, the adapterActivity:

public class MyListCategoryArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final String[] listanumber2;

public MyListCategoryArrayAdapter(Context context, String[] values, String[]   listanumber) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
this.listanumber2 = listanumber;
}

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

  if(convertView==null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.rowlayout, parent, false);
  }
        ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
        TextView textView = (TextView) convertView.findViewById(R.id.label);
        TextView textView2 = (TextView) convertView.findViewById(R.id.number);

        textView.setText(values[position]);

        String categoria = values[position];

        if (categoria.startsWith("Actividades vacacionales")) {
            imageView.setImageResource(R.drawable.pic_vacaciones);
            textView2.setText(listanumber2[0].trim()+" eventos");
        }
        if (categoria.startsWith("Aire libre y excursiones")) {
            imageView.setImageResource(R.drawable.pic_aire_libre_excursiones);
            textView2.setText(listanumber2[1].trim()+" eventos");
        }
        if (categoria.startsWith("Cine")) {
            imageView.setImageResource(R.drawable.pic_cine);
            textView2.setText(listanumber2[2].trim()+" eventos");
        }
        if (categoria.startsWith("Conferencias y otros")) {
            imageView.setImageResource(R.drawable.pic_conferencias);
            textView2.setText(listanumber2[3].trim()+" eventos");
        }
        if (categoria.startsWith("Congresos y jornadas")) {
            imageView.setImageResource(R.drawable.pic_congresos_jornadas);
            textView2.setText(listanumber2[4].trim()+" eventos");
        }
        if (categoria.startsWith("Cursos y talleres")) {
            imageView.setImageResource(R.drawable.pic_curso_taller);
            textView2.setText(listanumber2[5].trim()+" eventos");
        }
        if (categoria.startsWith("Deporte")) {
            imageView.setImageResource(R.drawable.pic_deporte);
            textView2.setText(listanumber2[6].trim()+" eventos");
        }
        if (categoria.startsWith("Escénicas")) {
            imageView.setImageResource(R.drawable.pic_escenicas);
            textView2.setText(listanumber2[7].trim()+" eventos");
        }
        if (categoria.startsWith("Exposiciones")) {
            imageView.setImageResource(R.drawable.pic_exposiciones);
            textView2.setText(listanumber2[8].trim()+" eventos");
        }
        if (categoria.startsWith("Ferias")) {
            imageView.setImageResource(R.drawable.pic_ferias);
            textView2.setText(listanumber2[9].trim()+" eventos");
        }
        if (categoria.startsWith("Fiestas y festivales")) {
            imageView.setImageResource(R.drawable.pic_fiesta_festival);
            textView2.setText(listanumber2[10].trim()+" eventos");
        }
        if (categoria.startsWith("Música")) {
            imageView.setImageResource(R.drawable.pic_musica);
            textView2.setText(listanumber2[11].trim()+" eventos");
        }
        if (categoria.startsWith("Viajes")) {
            imageView.setImageResource(R.drawable.pic_viajes);
            textView2.setText(listanumber2[12].trim()+" eventos");
        }
        if (categoria.startsWith("Otras")) {
            imageView.setImageResource(R.drawable.pic_otros);
            textView2.setText(listanumber2[13].trim()+" eventos");
        }
  return convertView;



}

Then, the View which I use to caught the user interaction:

    /**
 * Find our references to the objects in the xml layout
 */
@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    final ListView list = (ListView)findViewById(android.R.id.list);
    list.setItemsCanFocus(false);
    list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            Log.d("HERE IT DOES NOT ENTER", "HERE IT DOES NOT ENTER");
        }
    });


  final Button button0 = (Button)findViewById(R.id.button1);
    button0.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("Boton0000", "Boton0000");

        }
    });
  final Button button1 = (Button)findViewById(R.id.button2);

  button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("Boton1111", "Boton1111");

        }
  }); 

    final Button button2 = (Button)findViewById(R.id.button3);
    button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("Boton2222", "Boton2222");

        }
    }); 

    final Button button3 = (Button)findViewById(R.id.button4);
    button3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("Boton3333", "Boton3333");

        }
    }); 
}

Finally, I show the .xml for the listView and every row of the list:

all.products.xml

<?xml version="1.0" encoding="utf-8"?>
<com.agendajovenzgz.view.MainView  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="false"
>
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="389dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:focusable="true"  />


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Mapa" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button1"
    android:text="Lista" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Buscador" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/button3"
    android:text="Más" />
 </com.agendajovenzgz.view.MainView>

rowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:padding="6dip"

 >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:src="@drawable/ic_launcher"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="6dip"
    android:layout_toRightOf="@id/icon"
    android:gravity="center"
    android:textStyle="bold"
    android:focusable="false" 
    android:focusableInTouchMode="false"/>

<TextView
    android:id="@+id/number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    android:layout_toRightOf="@id/icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/label"
    android:layout_alignWithParentIfMissing="false"      
    android:gravity="center"
    android:textSize="12dp"
    android:focusable="false"
    android:focusableInTouchMode="false" />

 </RelativeLayout>

Can someone help me please??

Thanks in advance.

Regards.

您应该在扩展的ListActivity中重写onListItemClick。

Try not use a ListView, necessarily. Name the list view by some name in android:

android:id="@+id/my_list"

and then reference that in your MainActivity:

ListView myList = (ListView) findViewById(R.id.my_list);

You may then set myList to onItemClickListener with:

myList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

             // Your code here

    }
});

You need not worry about post-inflating, as there will be no items for the user to click on until the ListView is inflated. Without using a ListActivity, you can get rid of onFinishInflate() and the call to its parent, and simply put all your click listeners in your onCreate() method.

Also, another tip - declare your variables outside the onCreate() method and define them at the beginning on the onCreate() method. This will make them global across the Activity, so you will not need the final keyword.

In ListActivity , onContentChanged does various things, including setting its own OnItemClickListener .

This OnItemClickListener calls the onListItemClick method in ListActivity .

Therefore, to make it work, you need to either

  • Use onListItemClick instead of setting an OnItemClickListener
  • OR set the OnItemClickListener after you have called setContentView (possibly in onAttachedToWindow , which should occur after, but I have not tested this).

Try setting the clickable property to false for your all your textview.

android:clickable="false"

set the OnItemClickListener to the ListView during Activity#onCreate()

// inside activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    // calls to super and setcontentview

    listView = (ListView) findViewById(android.R.id.list);
    listView.setOnItemClickListener(new OnItemClickListener() { ...... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM