setTag()getTag()整数问题

时间:2015-08-24 01:36:33

标签: java android adapter

当getTag用于String没有错误并且它运行顺利但如果它用于int我的应用程序崩溃。当启动onclick时会发生这种情况。我想通过setTag获取id。请参阅下面的代码。

MyAdpater

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Contact current = data.get(position);
        holder.itemId.setText(String.valueOf(current.getId()));
        holder.title.setText(current.getName());
        holder.email.setText(current.getEmail());
        holder.itemId.setTag(holder);
        holder.title.setTag(holder);
        holder.email.setTag(holder);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView title;
        public TextView email;
        public TextView itemId;


        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.listTextName);
            email = (TextView) itemView.findViewById(R.id.listTextUname);
            itemId = (TextView) itemView.findViewById(R.id.listTextId);
            title.setOnClickListener(this);
            email.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            MyViewHolder holder = (MyViewHolder) v.getTag();
            int position = holder.getAdapterPosition();

            Contact contact = data.get(position);
            Toast.makeText(context, contact.getId(), Toast.LENGTH_SHORT).show();

        }

    }

}

2 个答案:

答案 0 :(得分:2)

对不起。在打电话之前,我忘记将其转换为String

String new = String.valueOf(contact.getId()); 

解决!

答案 1 :(得分:0)

如果您想通过getTag方法获取它。

<audio controls>
<source src="nameofsong.ogg" type="audio/ogg">
<source src="nameofsong.mp3" type="audio/mpeg">
audio tag not supported message optional
</audio>

你应该记得getTag将此视图中存储的Object作为标记返回