0

I am using a list view where I have an up vote button in it. When the user clicks on the up vote button, it will turn yellow and increment the score under it. This works as intended, however when I scroll down the list view I notice that other up vote buttons are highlighted (meaning they were also just "pressed"), even if they were recently pressed. I am assuming this is a problem recycling the cells. Also when I increment the first row in list view and scroll down, the number reverts back to original when I scroll back up. Any help would be appreciated. Is there anything here that may be causing my problem? Here is my code:

class MyAdapterListings extends BaseAdapter
{
    Globals g;

    private JSONArray dataArray;
    private Activity activity;

    private static LayoutInflater inflater = null;

    public MyAdapterListings(JSONArray jsonArray, Activity a)
    {
        g = Globals.getInstance();

        dataArray = jsonArray;
        activity = a;

        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount()
    {
        return dataArray.length();
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        final ListCell cell;
        Button theUpVoteButton = null;
        Button theDownVoteButton = null;

        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.daily_layout, null);
            cell = new ListCell();

            TextView imageButtonText = (TextView)convertView.findViewById(R.id.imageButtonText);
            theUpVoteButton = (Button)convertView.findViewById(R.id.upVoteButton);
            theDownVoteButton = (Button)convertView.findViewById(R.id.downVoteButton);
            Button imageLinkButton = (Button)convertView.findViewById(R.id.imageButton);
            Button reportButton = (Button)convertView.findViewById(R.id.reportButton);

            cell.nickname = (TextView)convertView.findViewById(R.id.nicknameTextView);
            cell.squeal = (TextView)convertView.findViewById(R.id.messageTextView);
            cell.timeSincePosted = (TextView)convertView.findViewById(R.id.timePostedTextView);
            cell.upVotes = (TextView)convertView.findViewById(R.id.scoreCountTextView);

            final Button finalTheUpVoteButton = theUpVoteButton;
            final Button finalTheDownVoteButton = theDownVoteButton;

            theUpVoteButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    finalTheUpVoteButton.setBackgroundResource(R.drawable.up_vote_button_gold);
                    finalTheDownVoteButton.setBackgroundResource(R.drawable.image_button);

                    cell.upVotes.setText("" + (Integer.parseInt(cell.upVotes.getText().toString())+1));
                    System.out.println("squealID = " + cell.squealID);
                    System.out.println("byUserID = " + cell.byUserID);

                    new IncrementUpVote(cell.squealID, cell.byUserID).execute();
                }
            });

            theDownVoteButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    System.out.println(position + " DOWN VOTE BUTTON PRESSED!!!");
                }
            });

            imageLinkButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    System.out.println(position + " IMAGE/LINK BUTTON PRESSED!!!");
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(cell.link));
                    activity.startActivity(browserIntent);
                }
            });

            reportButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    System.out.println(position + " REPORT BUTTON PRESSED!!!");
                }
            });

            convertView.setTag(cell);
        }


        else
        {
            cell = (ListCell)convertView.getTag();
        }

        try
        {
            JSONObject jsonObject = dataArray.getJSONObject(position);

            cell.squealID = jsonObject.getString("ID");
            cell.nickname.setText(jsonObject.getString("nickname"));
            cell.link = jsonObject.getString("link");
            cell.squeal.setText(jsonObject.getString("message"));
            cell.byUserID = jsonObject.getString("byUserID");
            cell.timeSincePosted.setText(getTimeSincePostedLabel(jsonObject.getString("TIME_TO_SEC(TIMEDIFF(NOW(), timePosted))")));
            cell.upVotes.setText(jsonObject.getString("upVotes"));
            cell.comments = jsonObject.getString("comments");
            cell.count = jsonObject.getString("count");
            cell.reportsCount = jsonObject.getString("reportsCount");


            if(Integer.parseInt(cell.count) > 0) // User already selected upvote or downvote
            {
                if (theUpVoteButton != null)
                    theUpVoteButton.setVisibility(View.GONE);

                if(theDownVoteButton != null)
                    theDownVoteButton.setVisibility(View.GONE);
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

        return convertView;
    }

    public String getTimeSincePostedLabel(String secondsString)
    {
        int num_seconds = Integer.parseInt(secondsString);

        int days = num_seconds / (60 * 60 * 24);
        num_seconds -= days * (60 * 60 * 24);
        int hours = num_seconds / (60 * 60);
        num_seconds -= hours * (60 * 60);
        int minutes = num_seconds / 60;

        if(days > 0)
        {
            return (days + " d");
        }

        else if(hours > 0)
        {
            return (hours + " h");
        }

        else if(minutes > 0)
        {
            return (minutes + " m");
        }

        return (num_seconds + " s");
    }

    private class ListCell
    {
        private String squealID;
        private TextView nickname;
        private String link;
        private TextView squeal;
        private String byUserID;
        private TextView timeSincePosted;
        private TextView upVotes;
        private String comments;
        private String count;
        private String reportsCount;
    }

    public class IncrementUpVote extends AsyncTask<String, Void, Void>
    {
        String squealID;
        String byUserID;

        public IncrementUpVote(String squealID, String byUserID)
        {
            this.squealID = squealID;
            this.byUserID = byUserID;
        }

        @Override
        protected Void doInBackground(String... params)
        {
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                String phpRequestLink = null;


                if(g.getSchool().equals("School1"))
                {
                    phpRequestLink = myURL;
                }

                else if(g.getSchool().equals("School2"))
                {
                    phpRequestLink = myURL;
                }

                else if(g.getSchool().equals("School3"))
                {
                    phpRequestLink = myURL;
                }


                HttpPost httppost = new HttpPost(phpRequestLink);
                HttpResponse response = httpclient.execute(httppost);
            }
            catch(Exception e)
            {
                //
            }

            return null;
        }
    }
}

1 Answers1

0

Since your views are recycling, so you have to reset your upvote count values everytime when your getView() is called. You can have a look at one of my answers here..

How to stop ListView recycling?

RadioButton Android, selected the same value in other row

Hope that helps..!!

Community
  • 1
  • 1
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
  • Not sure I understand. I am using Button btw not RadioButton – JasonFalcon Jan 01 '15 at 06:14
  • Actually my basic intention is that you should see how actually view recylcing works. You said when you scroll back, your count resets. It is just because you are not setting it everytime with upadated value when you scroll up and down. You have to store that updated value somewhere corresponding to a particular position and use that value to set upVote count when you scroll up and down – Mukesh Rana Jan 01 '15 at 06:20