android - 在 recyclerview 中单击按钮如何增加计数器并在 textview 中显示

标签 android button android-recyclerview

我正在开发一个使用回收 View 和 Volley 显示图像的应用程序。我在每张卡片中都有 ImageView 、 TextView 和按钮。我正在尝试向我的按钮添加功能,单击该按钮时,将计算点击次数并将其显示在 TextView 中。我在尝试执行此操作时遇到错误“变量'count'是从内部类中评估的,需要声明为最终的”。我做错了什么?这是代码

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Belal on 11/9/2015.
 */
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {


    //Imageloader to load image
    private ImageLoader imageLoader;
    private Context context;

    //List to store all superheroes
    List<SuperHero> superHeroes;

    //Constructor of this class
    public CardAdapter(List<SuperHero> superHeroes, Context context){
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }



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

        int count = 0;

        //Getting the particular item from the list
        SuperHero superHero =  superHeroes.get(position);

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));

        //Showing data on the views
        holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
        holder.textViewName.setText(superHero.getName());
        holder.textViewPublisher.setText(superHero.getPublisher());
        holder.textViewLikes.setText(superHero.getLikes());
        holder.txtCount.setText(String.valueOf(count));
        holder.custom_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count ++;


                }

            });
    }



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



    class ViewHolder extends RecyclerView.ViewHolder{
        //Views
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewPublisher;
        public TextView textViewLikes;
        public TextView txtCount;
        public ImageButton custom_button;
        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
            txtCount = (TextView)itemView.findViewById(R.id.txtCount);
            custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);
        }


    }

}

最佳答案

holder.txtCount.setText(String.valueOf(count)); 放在 onclick 函数中,并在声明类的下方声明 int count =0;。像这样

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

int count =0;

  //Imageloader to load image
    private ImageLoader imageLoader;

关于android - 在 recyclerview 中单击按钮如何增加计数器并在 textview 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35835211/

相关文章:

java - 如何在 RecyclerView 适配器中用 ViewBinding 替换 ButterKnife

android - 共享元素转换不起作用 - 我无法解析方法 ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, p1, p2);

android - 在 Android 中更快地对文档文件数组进行排序

Android - 如何从相机捕获图像或从库中添加

java - Android:RectF 和碰撞检测不起作用

java - 如何获取特定时间间隔的位置

css - 我可以使用 CSS 命令保留文本行吗?

javascript - 单击按钮后如何获取 console.log 的值并显示在我的输入文本中

javascript - 如何将按钮加载到同一页面上?

Android Recyclerview 与 ListView 与 Viewholder