我在数据库中进行了查询,并将其返回到我放在自定义适配器上的列表中,然后放入列表视图中。在我使用 onItemClikListener 之前工作正常。我想从查询中获取列“_id”,但我不知道该怎么做(我想将此 id 作为参数传递给另一个活动)。我从自定义适配器尝试了 getItemId,但它返回列表中的位置,而不是“_id”列。例如:我的查询选择 * where name = x; 返回 id 为 1 和 3 的行...但 getItemId 使其位置为 1="_id1" 和 2="_id3"...当我没有传递其他活动时,查询返回具有 "_id" 1 和2;
//Here i get the item position and pass to another activity
listagem.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long id) {
String Column_id = String.valueOf(adapter.getItemId(position));
Intent mostraLetra = new Intent(musica.this.getActivity(), ExibeMusica.class);
mostraLetra.putExtra("id", Column_id);
startActivity(mostraLetra);
}
});
//Here i pass the Id when i click on a item on the list
public String[] exibeMusica(String idMusica){
String r = idMusica;
String[] resultado = {};
Cursor resul = bancodados.query(Tabela_musica, new String[] {"_id", "Musica","Cantor","letra_musica","cat_oracao","cat_missa"}, "_Id = " +r+"", null, null, null, null, null);
resul.moveToFirst();
resultado[0] = String.valueOf(resul.getString(resul.getColumnIndex("_id")));
resultado[1] = resul.getString(resul.getColumnIndex("Musica"));
resultado[2] = resul.getString(resul.getColumnIndex("Cantor"));
resultado[3] = resul.getString(resul.getColumnIndex("letra_musica"));
resultado[4] = resul.getString(resul.getColumnIndex("cat_oracao"));
resultado[5] = resul.getString(resul.getColumnIndex("cat_missa"));
resul.close();
return resultado;
}
//This is my custom adapter
public class CustomAdapter extends BaseAdapter{
LayoutInflater inflater;
List<musica.ItemLista> itens;
public CustomAdapter(Activity a, List<musica.ItemLista> itens) {
super();
this.itens = itens;
this.inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return itens.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
musica.ItemLista item = itens.get(position);
View vi = convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.linha_listview, null);
TextView id = (TextView)vi.findViewById(R.linhaLista.id);
TextView musica = (TextView)vi.findViewById(R.linhaLista.musica);
TextView artista = (TextView)vi.findViewById(R.linhaLista.cantor);
ImageView oracao = (ImageView)vi.findViewById(R.linhaLista.cat_oracao);
ImageView missa = (ImageView)vi.findViewById(R.linhaLista.cat_missa);
id.setText(String.valueOf(item.id));
musica.setText(item.musica);
artista.setText(item.cantor);
String imgOra = item.oracao;
String imgMissa = item.missa;
if (imgOra.equals(null)) {
oracao.setImageResource(R.drawable.blank);
}else if(imgOra.equals("louvor")) {
oracao.setImageResource(R.drawable.louvor);
}else if(imgOra.equals("adoracao")) {
oracao.setImageResource(R.drawable.adoracao);
}
if (imgMissa.equals(null)) {
missa.setImageResource(R.drawable.blank);
}else if(imgOra.equals("louvor")) {
missa.setImageResource(R.drawable.louvor);
}else if(imgOra.equals("adoracao")) {
missa.setImageResource(R.drawable.adoracao);
}
}return vi;
}
}
任何人都可以帮助我获得正确的“_id”吗?