-1

I am trying to show all the question in a custom adapter. I have set the arrows for upvote and downvote as well. The question is well displayed in custom form. The problem is that when I click the upvote button of any of the elements in the list, the upvote functionality of last item is done. I need that the up vote to be done to that specific question whose button is clicked. To check it, I kept a toast in upvote button so that it displays the question of that element, but the result was it showed only of last item.

Code is given here

 public class CustomForumAdapter extends ArrayAdapter {

    List<Question> mList;
    Context mContext;
    LayoutInflater inflater;


    public CustomForumAdapter(Context context, List<Question> list) {
        super(context, R.layout.design_forum, list);
        mContext = context;
        mList = list;


    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = LayoutInflater.from(mContext);

        Holder holder = null;


        if (convertView == null) {

            holder=new Holder();
            convertView = inflater.inflate(R.layout.design_forum, parent, false);
            holder.upVote = (ImageButton) convertView.findViewById(R.id.upVote);
            holder.downVote = (ImageButton) convertView.findViewById(R.id.downVote);
            holder.timeAsked = (TextView) convertView.findViewById(R.id.timeAsked);
            holder.asker = (TextView) convertView.findViewById(R.id.asker);
            holder.mainAnswer = (TextView) convertView.findViewById(R.id.mainAnswer);
            holder.mainQuestion = (TextView) convertView.findViewById(R.id.mainQuestion);
            holder.rating = (TextView) convertView.findViewById(R.id.rating);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();

        }




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



                Log.v("adsl", "upvote");


            }
        });
        holder.downVote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });

        holder.mainQuestion.setText(mList.get(position).getMainQuestion());
        holder.mainAnswer.setText(mList.get(position).getMainAnswer());
        holder.asker.setText(mList.get(position).getAsker());
        holder.timeAsked.setText(mList.get(position).getTime());
        holder.rating.setText(mList.get(position).getRating() + "");


        return convertView;


    }

    class Holder {

        private ImageButton upVote, downVote;
        private TextView timeAsked, asker, mainQuestion, mainAnswer, rating;


    }


    @Override
    public int getCount() {
        return mList.size();
    }


    @Override
    public Question getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getPosition(Object item) {
        return super.getPosition(item);
    }

Updated: I kept the part of actual rating there. I also kept the new onclick listener. But bro can you please briefly tell what are you trying to say. (My question model has mainQuestion, mainAnswer, asker, time and rating)

second update: I again edited the question and used view holder this time. The last thing that is remaining is the individual button click of a invidual item. What should I do further?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
hamro doko
  • 29
  • 5
  • 2
    Possible duplicate of [Custom Adapter for List View](http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – Janki Gadhiya Feb 27 '17 at 06:12
  • how can you say that it is duplicate? My question is working with individual button clicks in a layout – hamro doko Feb 27 '17 at 07:43
  • Try adding setTag(position) to your ImageButtons in getView(). Inside OnClickListener(), use getTag() to get back the position and update your corresponding item in mList, then call notifyDataSetChanged(). I have a blog on ListView: http://programandroidlistview.blogspot.com/ with samples on how to do it, Hope it help! – i_A_mok Mar 01 '17 at 03:03

2 Answers2

0

Your logic is screwed up. You have 1 virtual rating for the adapter, you need one per item. You have one global onClickListener for the adapter, you need one per item. The easiest way to solve this is to create a new OnClickListener for all objects in getView every time its called. Then you can make sure that it points to the right views. You will also need to maintain a separate rating for every item in the adapter. Either the score needs to be added to the Question object, or you need to keep a list of objects with a Question and a score rather than just a list of Questions..

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • ImageButton upVote, downVote; TextView timeAsked, asker, mainQuestion, mainAnswer, rating; int currentRating; All of those need to be per item, not per adapter. Google ViewHolder pattern for help on that. – Gabe Sechan Feb 27 '17 at 04:45
  • bro edited question again can you please further explain – hamro doko Feb 27 '17 at 05:33
0

Very Simple you just have to get the current position model on click of the button like this:

upVoteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                //1) get the current click model from the list
                yourModel = yourList.get(position);//now yourmodel contains current clicked position data
                //now just set the upVode click true
                Toast.makeText(context, "up vote clicked", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.firebaseCrashReport(e);
            }
        }
    });

Hope this will help you

  • my upvote is in the custom adapter .. can you please explain liitle bit more then how can you access list there – hamro doko Feb 27 '17 at 07:46
  • Sure, declare yourlist as global variable in the adapter and on click of the button you use the list.....or you send me the adapter ill fix it and ill give you @sanatchandravanshi@gmail.com – Sanat Chandravanshi Feb 27 '17 at 08:51