1

我的应用程序有 Listview,Listview 项目有一个 TextView 和一个 Button 和其他图像等......我使用 ViewHolder 模式。

单击按钮时,我想更改 TextView 中的文本

如何在按钮 Onclick 中控制 TextView?

 public class AListAdapter extends ArrayAdapter<MyData> {

    static class ViewHolder {

        TextView viewCountView;

        ImageButton myButton;

        ImageView profileImageView;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final AListAdapter adapter = this;
        ViewHolder holder = null;


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cell parent, false);
            TextView viewCountView = (TextView) convertView.findViewById(R.id._view_count_);
            Button likeButton = (ImageButton) convertView.findViewById(R.id._like_button);
            LoadImageView profileImageView = (LoadImageView) convertView.findViewById(R.id.albumlist_profile_image);

            holder = new ViewHolder();
            holder.viewCountView = viewCountView;
            holder.likeButton = likeButton;
            holder.profileImageView = profileImageView;

            holder.likeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // how to controll TextView??????????????????
               // when click i want to change textview
                   //like holder.viewCountView.setText("clicked");
                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MyData data = (MyData) adapter.getItem(position);
        if (data != null) {

            holder.viewCountView.setText(String.valueOf(data.viewCount) + "views");
            //image settings 
        }
        return convertView;
    }
4

2 回答 2

7

在这种情况下,您无法访问使用局部变量,到调用 onClickListener 时,变量将超出范围。

因此,您也可以将 设置ViewHolder为按钮的标签,然后您可以在onClick处理程序中访问它

holder.likeButton.setTag(holder);
holder.likeButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       ViewHolder mH = (ViewHolder) v.getTag();

       mH.viewCountView.setText("clicked");
    }
});
于 2013-03-12T06:15:04.940 回答
0

将此代码放在 else() 部分之后

        holder.likeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               holder.viewCountView.setText("clicked");
            }
        });

像这样

  if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell parent, false);
        TextView viewCountView = (TextView) convertView.findViewById(R.id._view_count_);
        Button likeButton = (ImageButton) convertView.findViewById(R.id._like_button);
        LoadImageView profileImageView = (LoadImageView) convertView.findViewById(R.id.albumlist_profile_image);

        holder = new ViewHolder();
        holder.viewCountView = viewCountView;
        holder.likeButton = likeButton;
        holder.profileImageView = profileImageView;

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    MyData data = (MyData) adapter.getItem(position);
    if (data != null) {

        holder.viewCountView.setText(String.valueOf(data.viewCount) + "views");
        //image settings 
    }


    holder.likeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           holder.viewCountView.setText("clicked");
        }
    });
    return convertView;
于 2013-03-12T06:15:43.087 回答