2016-06-09 94 views
0

當listview開始我發現第一個按鈕被選中? 我嘗試使點擊按鈕事件與列表視圖,但我不知道原因!我可以如何讓它默認像列表中的所有按鈕? 請看看圖像。Android:Listview第一個按鈕選擇錯誤

android image button

@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_id = (TextView) row.findViewById(R.id.usersName2); 
     contactHolder.loadId = (TextView) row.findViewById(R.id.loadMoreID); 
     contactHolder.tx_name = (TextView) row.findViewById(R.id.usersName); 
     contactHolder.image_tx = (ImageView) row.findViewById(R.id.usersPic); 
     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()); 
    imgLoader.DisplayImage(contacts.getImage(), contactHolder.image_tx); 

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

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


    }); 
    if(contacts.getSelectedPosition() == position){ 
     contactHolder.sug_add.setText("Selected"); 
    } else{ 
     contactHolder.sug_add.setText("Follow"); 

    } 

    return row; 
} 


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

} 

回答

0

這是因爲通過缺省情況下,selectedPos的值是 「0」。

更改您的Contacts_Sug類以將selectedPos成員變量初始化爲-1。

private int selectedPos = -1; 
+0

謝謝先生,您的完美答案和時間:) –