RecyclerView with Clickable button

884 views Asked by At

So I'm using recyclerview adapter that takes it's data from ParseQuery Adapter. anyway, the problem now is , That in each item or row I have a button. like vote up button.

Every time I click on one button, it automatically click up randomly on other buttons in other rows,

any idea what the problem is ?

here is my code.

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

    private ViewGroup parseParent;
    private Context context;
    public Button voteUp;
    private RVAdapter recyclerAdapter = this;
    private ParseQueryAdapter<ParseObject> parseAdapter;



    public RVAdapter(Context context, ViewGroup parentIn) {
        super();
        this.context = context;


        // performance optimization for recycler view
        setHasStableIds(false);


        parseParent = parentIn;
        parseAdapter = new ParseQueryAdapter<ParseObject>(context, getImagesQueryFactory()) {


            @Override
            public View getItemView(ParseObject object, View v, ViewGroup parent) {
                if (v == null) {
                    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                }
                super.getItemView(object, v, parent);


                final ParseImageView Image = (ParseImageView) v.findViewById(R.id.list_item_image);
                final ParseFile imageFile = object.getParseFile("img");
                if (imageFile != null) {
                    //Get singleton instance of ImageLoader
                    ImageLoader imageLoader = ImageLoader.getInstance();
                    //Load the image from the url into the ImageView.
                    imageLoader.displayImage(imageFile.getUrl(), audioImage);
                }//end if


                voteUp= (Button) v.findViewById(R.id.like);
                voteUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        voteUp.setEnabled(false);
                        voteUp.setText("voted Up");
                    }
                });
                return v;
            }
        };//end parseAdapter


        loadImages();
        parseAdapter.addOnQueryLoadListener(new OnQueryLoadListener());
        parseAdapter.loadObjects();
    } //end of RVAdapter

    private ParseQueryAdapter.QueryFactory<ParseObject> getImagesQueryFactory() {

        return new ParseQueryAdapter.QueryFactory<ParseObject>() {
           //Query details.....
                return query;
            }
        };
    }

    public void loadImages() {
        ParseQuery<ParseObject> query = ParseObject.getQuery();
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, com.parse.ParseException e) {
                if (e == null) {
                    notifyDataSetChanged();
                } else {
                    //error
                }

            }
        });
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        parseAdapter.getView(position, holder.itemView, parseParent);

    }

    @Override
    public int getItemCount() {
        return parseAdapter.getCount();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {



        public ViewHolder(View itemView) {

            super(itemView);



            });

        }
    }

    public class OnQueryLoadListener implements ParseQueryAdapter.OnQueryLoadListener<ParseObject> {

        public void onLoading() {

        }

        public void onLoaded(List<ParseObject> objects, Exception e) {
            recyclerAdapter.notifyDataSetChanged();
        }
    }
}
1

There are 1 answers

1
HeatherSpence On

Remove Button declaration from the top and declare it here:

final Button voteUp = (Button) v.findViewById(R.id.like);

When you declare Button as a global variable at the top it is not assigning the variable properly for each row of the gridview or listview.

It needs to be assigned here so each iteration of the button is set with a separate onclick listener.