Meli Alfaro
Meli Alfaro

Reputation: 1

How get text from item clicked on a List view with a Custom Adapter

I'm trying to get the value of the text of the selected item but It doesn work.

The adapter:

public class DataMyAdapter extends BaseAdapter {

private ArrayList<Data> listaData;
private LayoutInflater linflater;

public DataMyAdapter(Context context, ArrayList<Data> listaData) {
    this.linflater = LayoutInflater.from(context);
    this.listaData = listaData;
}

@Override
public int getCount() {
    return listaData.size();
}

@Override
public Object getItem(int position) {
    return listaData.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ContenedorView contenedor = null;
    if (convertView == null){
        convertView = linflater.inflate(R.layout.customadapterlayout, null);

        contenedor = new ContenedorView();
        contenedor.nombre = (TextView) convertView.findViewById(R.id.nombreTextView);
        contenedor.realizado = (CheckBox) convertView.findViewById(R.id.realizadoCheckBox);

        convertView.setTag(contenedor);
    }else {
        contenedor = (ContenedorView) convertView.getTag();
    }

    Data datos = (Data) getItem(position);
    contenedor.nombre.setText(datos.getNombre());
    contenedor.realizado.setChecked(datos.getCheck());

    return convertView;
}

class ContenedorView{
    TextView nombre;
    CheckBox realizado;
}

}

The implementation in the MainActivity:

elementosList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            elementoSeleccionado = parent.getItemAtPosition(position).toString();
            return false;
        }
    });

The specific line: elementoSeleccionado = parent.getItemAtPosition(position).toString(); give to me at Long Value like: com.package.app.Data@423316e8

Upvotes: 0

Views: 42

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

elementoSeleccionado = parent.getItemAtPosition(position).toString();

returns Data.toString() (com.package.app.Data@423316e8), which is not what you want. What you want is

Data data = (Data) parent.getItemAtPosition(position)
elementoSeleccionado = data.getNombre();

or the getter you need

Upvotes: 3

Related Questions