如何使用自定义适配器在列表视图中单击项目中的文本

时间:2015-11-05 21:11:54

标签: android

我试图获取所选项目文本的值,但它不起作用。

适配器:

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;
}

}

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;
        }
    });

具体路线: elementoSeleccionado = parent.getItemAtPosition(position).toString(); 给我一个Long Value,如:com.package.app.Data@423316e8

1 个答案:

答案 0 :(得分:3)

$(function() {
   $("#left").css("float", "left");
    $("#right").css("float", "left");
});

返回elementoSeleccionado = parent.getItemAtPosition(position).toString(); Data.toString()),这不是您想要的。你想要的是

com.package.app.Data@423316e8

或你需要的吸气剂