2015-11-05 47 views
0

我試圖獲取所選項目的文本值,但它不起作用。如何使用自定義適配器從列表視圖中單擊項目的文本

適配器:

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(位置)的ToString(); 給我的長像值:[email protected]

回答

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

回報Data.toString()[email protected]),這是不是你想要的。你想要的是

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

或者,消氣,你需要

相關問題