2011-11-25 74 views
1

我有以下問題。我有一個ListView,我正在使用延伸ArrayAdapter類的自定義適配器。在每一行中有一個關於Button,當我點擊它時,我需要改變它的風格。如何更新ArrayAdapter的getView方法中的視圖?

到目前爲止,我有:

public View getView(final int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 

     if (convertView == null) { 
      vi = inflater.inflate(R.layout.people_item, null); 

      mViewHolder = new ViewHolder();    
      mViewHolder.follow = (Button) vi.findViewById(R.id.people_item_btn_follow); 
      mViewHolder.name = (TextView).... 
      vi.setTag(mViewHolder);   
     } else { 
      mViewHolder = (ViewHolder) convertView.getTag(); 
     } 

     mViewHolder.follow.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Changing the style of the button 
       if(mData[position].getFollow().equals("0")) { 
        mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border)); 
        mViewHolder.follow.setText(mCtx.getString(R.string.unfollow)); 
       } else { 
        mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.follow_button_border)); 
        mViewHolder.follow.setText(mCtx.getString(R.string.follow)); 
       } 

       mSharedAsyncTasks.getFollowerTask().execute(mData[position].getId()); 
      } 
     }); 

     if(mData[position] != null) { 
      // Setting data 
     } 

     return vi; 
    } 

private static class ViewHolder { 
    TextView fullName; 
    Button follow; 
} 

的問題是,在任何一行的按鈕點擊時,新的風格是在另一行的按鈕應用(雖然被應用於以下的效果到右邊一排)。

我知道它與行正在回收/重用的事實有關。

但如何真正解決這個問題?

謝謝!

回答

1

不確定你想要做以下事情。

public void onClick(View v) { 
     mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border)); 
     ... 
} 

具體而言,我認爲這個問題是你引用mViewHolder它看起來像它掛在和滾動可以指向任何按鈕。它的一個範圍問題,你應該能夠解決以下問題。在onClick(View v)v我相信是你點擊的按鈕。

相反,你應該能夠做到以下

public void onClick(View v) { 
     ((Button)v).setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border)); 
     ... 
} 
+0

當然!謝謝埃米爾! –

相關問題