1

I have a ListView with a custom Adapter and a Layout defined in item_song.xml. Inside each element, I have 3 TextViews and one ImageView. I want to implement an OnClickListener on the ImageView inside each element. I've tried implementing OnClickListener in the custom Adapter, but I don't get access to the element position and data. How do I achieve this?

My custom Adapter:

package clases;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import es.grupobcn.jaggerpub.R;

public class AdapterVotacion extends BaseAdapter {

    private Context context;
    ArrayList<Cancion> arrayitms;

    public AdapterVotacion(Context context, ArrayList<Cancion> listarry) {
        super();
        this.context = context;
        this.arrayitms = listarry;
    }

    // devuelve el tamaño del arrayList
    @Override
    public int getCount() {
        return arrayitms.size();
    }

    // devuelve el objeto de la clase Item_objct del array
    @Override
    public Object getItem(int position) {
        return arrayitms.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // declaro una clase STATIC que representa una fila
    public static class Fila {
        TextView song_name;
        TextView song_artist;
        TextView song_votes;
        ImageView icono;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fila view;
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            view = new Fila();

            // creamos el objeto item y lo traemos del array
            Cancion itm = arrayitms.get(position);
            convertView = inflator.inflate(R.layout.item_song, null);

            // song_name
            view.song_name = (TextView) convertView
                    .findViewById(R.id.nombre_cancion);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_name.setText(itm.song_name);

            // song_artist
            view.song_artist = (TextView) convertView
                    .findViewById(R.id.nombre_artista);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_artist.setText(itm.song_artist);

            // song_votes
            view.song_votes = (TextView) convertView
                    .findViewById(R.id.numero_de_votos);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_votes.setText(String.valueOf(itm.song_votes));

            // ICONO
            view.icono = (ImageView) convertView.findViewById(R.id.boton_votar);
            // seteamos el icono correspondiente
            view.icono.setImageResource(R.drawable.votar);
            // on click listener

            //view.icono.setOnClickListener(this);
            /*view.icono.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Has hecho click en el temazo "+v, Toast.LENGTH_LONG).show();
                }
            });*/

            convertView.setTag(view);
        } else {
            view = (Fila) convertView.getTag();
        }
        return convertView;
    }

/*  @Override
    public void onClick(View v) {
        Toast.makeText(context, "Has hecho click en el elemento "+v.get, Toast.LENGTH_LONG).show();
    }*/

}

My item_song.xml:

<?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="48dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="6" >

    <!-- Linear de nombre de artista y cancion -->

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="4"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <!-- Textview nombre artista (pequeño) -->

        <TextView
            android:id="@+id/nombre_artista"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:textColor="#FFFFFF"
            android:textSize="16sp" />

        <!-- Textview nombre cancion (grande) -->

        <TextView
            android:id="@+id/nombre_cancion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>

    <!-- Linear de icono de votar y textview de numero de votos actuales -->

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="2"
        android:gravity="right"
        android:orientation="vertical" >

        <!-- Textview numero de votos -->

        <TextView
            android:id="@+id/numero_de_votos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <!-- image view boton votar -->

        <ImageView
            android:id="@+id/boton_votar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:contentDescription="votar"
            android:src="@drawable/votar" />
    </LinearLayout>

</LinearLayout>
aboger
  • 2,214
  • 6
  • 33
  • 47
jacho981
  • 87
  • 1
  • 10

2 Answers2

1

The main idea is you have to add OnClickListener every time when getView() is called. Try this code

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Fila view;
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            view = new Fila();

            // creamos el objeto item y lo traemos del array
            Cancion itm = arrayitms.get(position);
            convertView = inflator.inflate(R.layout.item_song, null);

            // song_name
            view.song_name = (TextView) convertView
                    .findViewById(R.id.nombre_cancion);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_name.setText(itm.song_name);

            // song_artist
            view.song_artist = (TextView) convertView
                    .findViewById(R.id.nombre_artista);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_artist.setText(itm.song_artist);

            // song_votes
            view.song_votes = (TextView) convertView
                    .findViewById(R.id.numero_de_votos);
            // metemos en el campo titulo el nombre de la opcion obtenida del
            // objeto
            view.song_votes.setText(String.valueOf(itm.song_votes));

            // ICONO
            view.icono = (ImageView) convertView.findViewById(R.id.boton_votar);
            // seteamos el icono correspondiente
            view.icono.setImageResource(R.drawable.votar);

            convertView.setTag(view);
        } else {
            view = (Fila) convertView.getTag();
        }


        view.icono.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "Position " + position, Toast.LENGTH_LONG).show();
                    }
                });
            return convertView;
        }

Note that position parameter must be final. It allows access to position in anonymous OnClickListener implementation

olegr
  • 1,999
  • 18
  • 23
0

Try this: (I haven't tested it though but it should work)

public class AdapterVotacion extends BaseAdapter {
private Context context;
int currentPos;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Fila view;
    LayoutInflater inflator = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
      ......

        currentPos = position;
        // on click listener

        view.icono.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Current position is "+currentPos, Toast.LENGTH_LONG).show();
            }
        });
 ......

The reason you couldn't access the position is because it's not accessible from within the scope of the anonymous inner class. If you make a class variable with the same data in, the onClickListener should see it too. Same with the view.

MSpeed
  • 8,153
  • 7
  • 49
  • 61