0

I have generated a Listview using a BaseAdapter . At present I have 4 rows in my Listview. I have some Textviews and a Button in the row.At present the color of my button is the default grey color. I want to do the following:

  1. When I click the button on the first row I want to change the color of the Button to Black.
  2. When I click on the second row I want the button in the first row to change back to the grey color and change the color of the button in the second row to black.

In short I want only one button to have a black color at a time when clicked. I have done the following code but I get the following problems.

  1. If I click the first row button, the third row button gets a black color.
  2. If I click the second row button,it turns black but the previously clicked button does not go back to its original color.

I am posting my codes, please guide me step by step as I am very new to this.

mycontactstemp.java

public class contactstemp extends Fragment {
 public ArrayList<ProductModel> _productlist = new ArrayList<ProductModel>();
    @Override
     public View onCreateView(final LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     final View view = inflater.inflate(R.layout.viewrecord, container,
            false);
          return view;
}
    private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _productlist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return _productlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listview_row, null);
            viewHolder = new ViewHolder();
            viewHolder.txt_pname = (TextView) convertView
            .findViewById(R.id.txtdisplaypname);
    viewHolder.txt_pprice = (TextView) convertView
            .findViewById(R.id.txtdisplaypprice);

    viewHolder.txtidno = (TextView) convertView
            .findViewById(R.id.txtdisplaypid);
    viewHolder.buttons = (Button) convertView
    .findViewById(R.id.btn_update);
    convertView.setTag(viewHolder);


        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.txt_pname.setText(_productlist.get(position)
                .getProductname().trim());
        viewHolder.txt_pprice.setText(_productlist.get(position)
                .getProductprice().trim());

        viewHolder.txtidno.setText(_productlist.get(position).getIdno()
                .trim());
        viewHolder.buttons.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewHolder.buttons.setBackgroundColor(Color.BLACK);
            }
        });
        return  convertView;
    }

1 Answers1

1

You could take advantage of single item selection mode in the listview (see setChoiceMode()).

Create a new background drawable for the buttons and set the selected state to black. Then set your button to android:duplicateParentState="true", so that it gets the selection from the list item.

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28