简体   繁体   中英

Is there a way to update listView item layout in android studio?

I'm making a button that when i click on him the visibility of a checkbox withing a listView will change. however it appears that the code run as expected but the visibailty is not udpateing. is there a way to update the item's visabilty?

mButtonEdit.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {

         for(int i = 0 ; i<calanders.size();i++){
             View view = mListView.getAdapter().getView(i,null,mListView);

            if(mButtonEdit.isSelected()){
                print("button is selected");
                CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
                checkBox.setVisibility(View.GONE);

             }else{
                print("button is not selected");
                 CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
                 checkBox.setVisibility(View.VISIBLE);


             }


         }
         mListView.getAdapter().
         if(mButtonEdit.isSelected()){
             mButtonEdit.setSelected(false);
         }else{
             mButtonEdit.setSelected(true);
         }
     }
 });



You should never call the ListAdapter 's getView() method. It is only supposed to be called by the system when scrolling though the ListView . Instead you need to update the list by calling mListView.getAdapter().notifyDataSetChanged() .

Add a boolean field in the adapter and update its value when the button is clicked.

You can create a model/data class based on your data and keep a Boolean variable for checkbox visibility. So default make it false and on button click get the position of list-view item and update Boolean variable to true, and do adapter.notifyDataSetChanged() .

You can also try with :

((YourAdapter) yourListView.getAdapter()).notifyDataSetChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM