0

I have generated a ListView using BaseAdapter. I have 2 Button in my Listview. I want to make my Button1 invisible when a Button2 in the row is clicked. Then when another row's Button2 is clicked I want to make the Button1 in the current row invisible and the previously invisible Button1 visible i.e at a time only one row's Button1 should be invisible. I have done the following, but I don't know how to set the visibility of the Button1 at the clicked row. Please guide me step by step. My adapter code is as follows:

    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;
    }
    private final boolean[] mHighlightedPositions = new boolean[getCount()];
    @Override
    public View getView(final 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.Button1 = (Button) convertView
    .findViewById(R.id.btn_update);
    convertView.setTag(viewHolder);
    viewHolder.Button2 = (Button) convertView
    .findViewById(R.id.btn_update2);
    convertView.setTag(viewHolder);

        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
 viewHolder.Button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 if(mHighlightedPositions[position]) {


                                viewHolder.Button1.setVisibility(convertView.INVISIBLE);     

                    }

            }
        });
        return  convertView;
    }

1 Answers1

0

You need to keep hold of the last hidden Button view , to make it visible when another item Button is clicked.

First, declare a static variable in your BaseAdapter to hold the hidden Button

private static Button lastHiddenButton = null;

Next, Add click listener to Button2 , and hide Button1 and make the last hidden Button visible

final Button hideButton = placeHolder.button1;
placeHolder.button2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       // if the last hidden button is valid make it to visible
       if(lastHiddenButton != null){
           lastHiddenButton.setVisibility(View.VISIBLE);
           lastHiddenButton = null;
       }
       // hide button1
       hideButton.setVisibility(View.GONE);
       lastHiddenButton = hideButton;
        }
    });

This will make sure when Button2 is clicked the Button1 belongs to the clicked row will be hidden.

With this change , you will note that few other row Buttons will also get hidden due to the ListView recycle. To fix this you need to inform the ListView that all my list item views are different . This will have impact in ListView performance when you have hundreds of items, but you can't avoid this

Finally , Override the getItemViewType and getViewTypeCount in BaseAdapter and let the ListView know that all the items view types are different

@Override
public int getItemViewType(int position){
    // return a unique number
    return position;
}

@Override
public int getViewTypeCount() {
    return getCount();
}
Libin
  • 16,967
  • 7
  • 61
  • 83