2016-06-07 44 views
0

當我點擊第一個按鈕只點擊一個按鈕它設置按鈕背景確定。但當我向下滾動時發現問題,我發現更多的按鈕被隨機地改變看起來像圖片。Android:列表視圖更改按鈕文本上clilck

查看圖片 here。代碼

部分: - ContactSug_Adapter

public class ContactSug_Adapter extends ArrayAdapter { 

    List list = new ArrayList(); 
    ImageLoader imgLoader = new ImageLoader(getContext()); 
    private Context context; 
    public ContactSug_Adapter(Context context, int resource) { 
     super(context, resource); 
    } 

    @Override 
    public void add(Object object) { 
     super.add(object); 
     list.add(object); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return this.list.get(position); 
    } 


    @Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 
     View row; 
     row = convertView; 
     final ContactHolder contactHolder; 
     if (row == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.row, parent, false); 
      contactHolder = new ContactHolder(); 
      contactHolder.tx_id = (TextView) row.findViewById(R.id.usersName2); 
      contactHolder.tx_name = (TextView) row.findViewById(R.id.usersName); 
      contactHolder.sug_add = (Button) row.findViewById(R.id.sug_id); 

      row.setTag(contactHolder); 


     } else { 
      contactHolder = (ContactHolder) row.getTag(); 

     } 
     final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position); 
     contactHolder.image_tx.setImageResource(R.mipmap.ic_launcher); 
     contactHolder.tx_id.setText(contacts.getId()); 
     contactHolder.tx_name.setText(contacts.getName()); 
     contactHolder.sug_add.setTag(position); 

     contactHolder.sug_add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       contactHolder.sug_add.setText("Selected"); 

      } 
     }); 


     return row; 
    } 


    public class ContactHolder { 
     TextView tx_id, tx_name,loadId; 
     ImageView image_tx; 
     public Button sug_add; 

    }/********* act 
+0

列表視圖,回收他們的意見,有時當你向下滾動,在'row'不會是空的,因爲它是被重用。在這種情況下,您正在使用'contactHolder =(ContactHolder)row.getTag();'''''我會使用'position'參數來跟蹤狀態的舊視圖中的'contactHolder' – TychoTheTaco

回答

1

這是由於意見回收。 您需要爲要設置的項目設置文本「選定」,並設置其他項目的默認文本。你可以用if-else語句來做到這一點。

對於您需要有一個成員變量在Contacts_Sug舉行這樣的selectionPos -

private int selectionPos; 

public void setSelectedPosition(int position){ 
    selectionPos = position; 
} 

public int getSelectedPosition(){ 
    return selectionPos; 
} 

,並設置按鈕onClick() -

contactHolder.sug_add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       contacts.setSelectedPosition(position); //Set position here 
       contactHolder.sug_add.setText("Selected"); 

      } 
     }); 

onClick()你在哪裏之外爲您的視圖設置值。 添加此 -

if(contacts.getSelectedPosition() == position){ 
     //Set your button state to "selected" here 
     contactHolder.sug_add.setText("Selected"); 
} else{ 
     //Set your button state to default here 
     contactHolder.sug_add.setText("Follow"); 
} 
相關問題