2016-06-09 92 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

谢谢先生,您的完美答案和时间:) –