0

我在数据库中进行了查询,并将其返回到我放在自定义适配器上的列表中,然后放入列表视图中。在我使用 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”吗?

4

3 回答 3

0

getView像这样将标签设置为适配器的行视图

    .........
    }
    vi.setTag(String.valueOf(item.id))
    return vi;
    .........

进而

public void onItemClick(AdapterView<?> arg0, View arg1,int position, long id) {    
String Column_id = (String) arg1.getTag();
......
于 2012-12-11T11:56:09.210 回答
0

在 CustomAdapter 中设置 linha_listview.xml 时,您尝试在 linha_listview.xml 中再添加一个 textview,如下所示:

<TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

当您将此可见性设置为消失时,它在列表视图上不再可见。稍后您在 getView() 方法中通过 pidtextview.setText(column_id) 设置此文本视图。因此,在单击列表项时,通过以下方式获取此 column_id

String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();

在您的列表项侦听器中。通过上面的代码,您将获得 pid,通过 intent.putExtra() 将其传递给其他活动;在该活动中,从 getIntent() 检索数据并检索包含该 column_id 的特定行的数据,并显示该活动的特定行详细信息。希望这可以帮助。如果您仍然有任何疑问,请返回。

于 2012-12-11T11:58:15.393 回答
0

在 onItemClickListner 中编写此代码

resul.moveToPosition(位置)

字符串 Column_id = resul.getString(resul.getColumnIndex("_id");

于 2012-12-11T12:03:32.490 回答