Danny Ho
Danny Ho

Reputation: 149

How to setVisibility in a listview by a button outside it?

I have a custom Listview with 1 Button on a row I would like to add a button outside the Listview to set the visibility on button on all rows How can i achieve this?

And i DONT wanna use notifyDataSetChanged that reload all data Anyone got an idea of this??

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/notice_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/notice_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/notice_title"
        android:textSize="8dp" />

    <ImageButton 
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_delete"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:visibility="invisible" />

</RelativeLayout>

Adapter_notice.java

public class Adapter_notice extends ArrayAdapter<notice> {

    private Context context; 
    private int listRow;    
    private notice[] notice; 
    public List<String> listTag = new ArrayList<String> ();


    public Adapter_notice(Context context, int listRow, notice[] Rows) {
        super(context, listRow, Rows);
        this.listRow = listRow;
        this.context = context;
        this.notice = Rows;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        //Log.d("edittext", "items !empty="+listTag.contains(listTag.get(position)));
        LayoutInflater inflater = LayoutInflater.from(context);

            row = inflater.inflate(listRow,parent, false);
            notice row_notice = notice[position];
            TextView tv1 = (TextView) row.findViewById(R.id.notice_title);
            tv1.setText(row_notice.gettitle());
            TextView tv2 = (TextView) row.findViewById(R.id.notice_date);
            tv2.setText("Date:"+row_notice.getdate());
            ImageButton btn = (ImageButton) row.findViewById(R.id.btn_delete);

        return row;
    }
}

Upvotes: 1

Views: 6330

Answers (5)

Suhail Mehta
Suhail Mehta

Reputation: 5542

on button click event add

yourListView.setVisiblity(View.INVISIBLE);

to hide the list view

Upvotes: 0

stack_ved
stack_ved

Reputation: 721

in onClick() of the button outside the list, do something like

listview.setAdapter(changedAdapter) 

Where changedAdapter will be a new set of adapter OR

    adapter.setButtonVisibility(false);
    listview.setAdapter(adapter). 

Create a new method in your Adapter class which sets a boolean value based on which the getView will return view (if boolean is true, Button field will be made invisible)

public void setButtonVisibility(boolean hideButton){
    this.hideButton = hideButton;
}

getView(){
...
if(hideButton){
    //hide button
}
}

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33495

I have a custom Listview with 1 button on a row I would like to add a button outside the Listview to set the visibility on button on all rows How can i achieve this?

And i DONT wanna use notifyDataSetChanged that reload all data Anyone got an idea of this??

Create layout that contains ListView and Button. ListView has set height to WRAP_CONTENT. Then create onClickListener for Button and onClick() hide your ListView:

btn.setOnClickListener(new View.OnClickListener {
   public void onClick(View v) {
       if (listView.getVisibility() == ListView.VISIBLE) {
          listView.setVisibility(ListView.INVISIBLE);
       }
       else {
          listView.setVisibility(ListView.VISIBLE);
       }
   }
});

Note: Difference between INVISIBLE and GONE is that GONE property doesn't take place in layout.

Upvotes: 3

Mohan Raj
Mohan Raj

Reputation: 590

In your OnClickListener() of the decider button ,set the Adapter again for the list . and keep a public Boolean variable outside the List to decide visibility of the specific button, by validating Boolean variable.

Upvotes: 0

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Use

ListView.setVisibility(View.GONE / View.VISIBLE / View.INVISIBLE);

Since you want to temporarily disable the visibility use View.INVISIBLE and View.VISIBLE

Upvotes: 0

Related Questions