nicole kidman nicole kidman - 4 months ago 12x
Android Question

Android : Listview Change Button Text On clilck

When I click on the first button only clicked on one button it sets the button background ok . but problem when I scroll down i found more buttons was changed randomly look like picture.

See picture here.

part of code :- 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);

public void add(Object object) {

public int getCount() {
return super.getCount();

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

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_name = (TextView) row.findViewById(;
contactHolder.sug_add = (Button) row.findViewById(;


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

final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position);

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

public void onClick(View v) {



return row;

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

}/********* act


This is due to recycling of views. You need to set the text "Selected" for the items you want to be and set the default text for the other items. You can do that using if-else statement.

For that you need to have a member variable in Contacts_Sug to hold the selectionPos like this -

 private int selectionPos; 

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

public int getSelectedPosition(){
    return selectionPos;

And set it in button onClick() -

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

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


And outside this onClick() where you are setting the values for your views. Add this -

if(contacts.getSelectedPosition() == position){
       //Set your button state to "selected" here
} else{
       //Set your button state to default here