2015-01-01 24 views
0

我正在使用列表視圖,我有一個向上投票按鈕。當用戶點擊向上投票按鈕時,它將變成黃色並在其下增加分數。這按預期工作,但是當我向下滾動列表視圖時,我注意到其他投票按鈕被突出顯示(意味着它們也被「按下」),即使它們最近被按下了。我認爲這是回收細胞的問題。另外,當我增加列表視圖中的第一行並向下滾動時,當我向後滾動時,編號會恢復爲原始值。任何幫助,將不勝感激。這裏有什麼可能導致我的問題嗎?這裏是我的代碼:爲什麼我的向上投票按鈕列表視圖被用於android中的其他行?

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; 
     } 
    } 
} 

回答

0

因爲你的意見被回收,所以你必須重置您給予好評計數值每次當你的getView()被調用。你可以看看我在這裏的一個答案..

How to stop ListView recycling?

RadioButton Android, selected the same value in other row

希望幫助.. !!

+0

不知道我明白。我使用Button btw而不是RadioButton – JasonFalcon

+0

其實我的基本意圖是,你應該看看實際上查看recylcing的工作原理。你說當你回滾時,你的計數重置。這僅僅是因爲當你上下滾動時,你並不是每次都用高價值設置它。您必須將某個位置對應的特定位置的更新值存儲起來,並在上下滾動時使用該值設置投票計數 –

相關問題