为什么列表视图上的我的投票按钮被用于android中的其他行?

时间:2015-01-01 05:07:05

标签: android listview android-listview

我正在使用列表视图,其中有一个向上投票按钮。当用户点击向上投票按钮时,它将变为黄色并增加其下的分数。这按预期工作,但是当我向下滚动列表视图时,我注意到其他向上投票按钮被突出显示(意味着它们也只是"按下"),即使它们最近被按下了。我假设这是回收细胞的问题。此外,当我在列表视图中递增第一行并向下滚动时,当我向上滚动时,该数字将恢复为原始数字。任何帮助,将不胜感激。这里有什么可能导致我的问题吗?这是我的代码:

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 个答案:

答案 0 :(得分:0)

由于您的视图正在回收,因此每次调用getView()时都必须重置upvote计数值。您可以在这里查看我的一个答案..

How to stop ListView recycling?

RadioButton Android, selected the same value in other row

希望有所帮助.. !!