0

我正在尝试从列表视图开始一项新活动。我正在从数据库中加载一些数据并在另一个活动中打开它......当我使用来自我的 customAdapter 的 OnItemClickListener 和 getTag 时工作正常......但结果显示在 listview 上的某些项目中重复。搜索一些主题...我找到了资源 ViewHolder。

它解决了重复问题,但我无法从 holder.getTag() 开始新活动。

我的观点持有人

static class ViewHolder {
    TextView hs;
    TextView hi;
    TextView ha;
    ImageView ho;
    ImageView hm;   
}

我的 customAdapter 查看代码

public View getView(int position, View convertView, ViewGroup parent) {

    DataAdapter.ItemLista item = itens.get(position);
    final ViewHolder holder;

    if(convertView==null){
        convertView = inflater.inflate(R.layout.linha_listview, null);
        holder = new ViewHolder();

        holder.hi = (TextView)convertView.findViewById(R.linhaLista.id);
        holder.hs = (TextView)convertView.findViewById(R.linhaLista.musica);
        holder.ha = (TextView)convertView.findViewById(R.linhaLista.cantor);
        holder.ho = (ImageView)convertView.findViewById(R.linhaLista.cat_oracao);
        holder.hm = (ImageView)convertView.findViewById(R.linhaLista.cat_missa);


        /*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);
        }*/
        convertView.setTag(holder);
        Log.v(String.valueOf(holder),"valor do setholder");
    }else {
        holder = (ViewHolder) convertView.getTag();
        Log.v(String.valueOf(holder),"valor do getholder");
    }
    holder.hi.setText(String.valueOf(item.id));
    holder.hs.setText(item.musica);
    holder.ha.setText(item.cantor);
    String imgOra = item.oracao;

    if (imgOra.equals(null)) {
        holder.ho.setImageResource(R.drawable.blank);
    }else if(imgOra.equals("louvor")) {
        holder.ho.setImageResource(R.drawable.louvor);
    }else if(imgOra.equals("adoracao")) {
        holder.ho.setImageResource(R.drawable.adoracao);
    }

    return convertView;
}

在这种情况下,我需要将字段 holder.hi 作为标签。任何人都知道如何解决它?

4

1 回答 1

0

我解决了这个问题,在自定义适配器而不是片段中添加了一个意图。所以为了它的工作我不得不改变代码的某些部分添加一个字符串 [] 和一个上下文。以下是我找到的解决方案。希望对以后的疑惑有所帮助。

public class CustomAdapter extends BaseAdapter{
    LayoutInflater inflater;
    List<DataAdapter.ItemLista> itens;
    DataAdapter.ItemLista item;
    ViewHolder holder;
    Context context;

    public CustomAdapter(Context contexto, List<DataAdapter.ItemLista> itens) {
        super();    
        this.itens = itens;
        this.context = contexto;
        this.inflater = (LayoutInflater)contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
.
.
.
    public View getView(final int position, View convertView, ViewGroup parent) {
        final String[] extra = new String[itens.size()];
        item = itens.get(position);
        if(convertView==null){
            convertView = inflater.inflate(R.layout.linha_listview, null);
            holder = new ViewHolder();
            holder.hi = (TextView)convertView.findViewById(R.linhaLista.id);
            holder.hs = (TextView)convertView.findViewById(R.linhaLista.musica);
            holder.ha = (TextView)convertView.findViewById(R.linhaLista.cantor);
            holder.ho = (ImageView)convertView.findViewById(R.linhaLista.cat_oracao);
            holder.hm = (ImageView)convertView.findViewById(R.linhaLista.cat_missa);

            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.hi.setText(String.valueOf(item.id));
        holder.hs.setText(item.musica);
        holder.ha.setText(item.cantor);
        extra[position] = String.valueOf(item.id);
        String imgOra = item.oracao;
        if (imgOra.equals(null)) {
            holder.ho.setImageResource(R.drawable.blank);
        }else if(imgOra.equals("louvor")) {
            holder.ho.setImageResource(R.drawable.louvor);
        }else if(imgOra.equals("adoracao")) {
            holder.ho.setImageResource(R.drawable.adoracao);
        }
        convertView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent mostraLetra = new Intent();
                mostraLetra.setClass(context, ExibeMusica.class);
                mostraLetra.putExtra("id",extra[position]);
                context.startActivity(mostraLetra);
            }
         });
         return convertView;
    }
于 2013-02-25T12:46:55.040 回答