Paul Myec
Paul Myec

Reputation: 21

Setting a Button visibility to GONE when I click on a ListView

I want to make related button's visibility gone when I click on listview but it only works for first button on the listview.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          Button btn = findViewById(R.id.btn_refresh);
          btn.setVisibility(View.GONE);// this works for first button
        }
    });

Upvotes: 2

Views: 116

Answers (3)

Soultion:

You need to first get the position of which item you're clicking, try this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id {

      (your_list_view).getchildAt(position).findViewById(R.id.btn_refresh).setVisibility(View.Gone) // Setting the visibility of the selected view

    }
});

Hope it helps.

Upvotes: 0

Zankrut Parmar
Zankrut Parmar

Reputation: 2005

Your definition of button is wrong in this case. Edit it like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      Button btn = view.findViewById(R.id.btn_refresh);
      btn.setVisibility(View.GONE);// this works for first button
    }
});

Upvotes: 1

Yoldrim
Yoldrim

Reputation: 53

You're setting the visibility of the button you've selected in

Button btn = findViewById(R.id.btn_refresh);

not of the clicked item.

You need to do

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id {
      view.setVisibility(View.GONE); // Setting the visibility of the selected view
    }
});

Upvotes: 1

Related Questions