nicole kidman nicole kidman - 1 year ago 193
Android Question

Android : Listview First button selected error

When listview Start i found First button was selected ?
i try make on click button event with listview but i don't know the reason ! how i can make it default like all buttons in list ?
take a look about image please.

android image button

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(;
contactHolder.tx_id = (TextView) row.findViewById(;
contactHolder.loadId = (TextView) row.findViewById(;
contactHolder.tx_name = (TextView) row.findViewById(;
contactHolder.image_tx = (ImageView) row.findViewById(;
contactHolder.sug_add = (Button) row.findViewById(;


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

final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position);
imgLoader.DisplayImage(contacts.getImage(), contactHolder.image_tx);

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

public void onClick(View v) {
contacts.setSelectedPosition(position); //Set position here

if(contacts.getSelectedPosition() == position){
} else{


return row;

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


Answer Source

This is because by default, the value of selectedPosis "0".

Change your Contacts_Sug class to have the selectedPos member variable initialized to -1.

private int selectedPos = -1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download