从自定义适配器获取特定列

时间:2012-12-11 11:45:36

标签: android database onitemclicklistener custom-adapter

我在数据库中进行了查询并将其返回到我放在自定义适配器上的列表中,然后我将其放入listview中。 工作正常,直到我使用onItemClikListener。我想从查询中获取列“_id”,我不知道该怎么做(我想将此id作为另一个活动的参数传递)。 我从我的自定义适配器尝试了getItemId,但它返回列表中的位置,而不是列“_id”。 例:我的查询选择*其中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”吗?

3 个答案:

答案 0 :(得分: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();
......

答案 1 :(得分: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)设置此textview。因此,在单击列表项时,请通过

获取此column_id
String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
列表项侦听器中的

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

答案 2 :(得分:0)

在onItemClickListner

中编写此代码

resul.moveToPosition(position)

String Column_id = resul.getString(resul.getColumnIndex(“_ id”);