Setting the height to zero will not work indeed! Many suggest to use View.Gone for the visibility of the row. However this is a ListView and therefore, setting a Visibility of Gone to one of the views will actually keep its space occupied while making its content vanish.
Anyway, here is an implementation of ArrayAdapter that has two public functions hide(int) and unHide(int) allowing the user to hide/unhide the row at the position.
The approach taken here is based on keeping the views used by the ListView intact. This means we do not want memory to be overflowed and at the same time we want to be able to hide some entries.
My way depends on, as usual, an array of objects. Without loss of generality, lets consider that this Adapter takes an array of String type and keeps it locally. To be able to hide, we need an array of Boolean type of the same size. These two arrays are items and hidden respectively. We initialize the hidden array to false in the constructor.
public class SherifHidingAdapter extends ArrayAdapter<String> { String[] items = null; boolean[] hidden = null; public SherifHidingAdapter(Context mContext, String[] objects) { super(mContext, R.layout.some_layout, objects); items = objects; hidden = new boolean[objects.length]; for (int i = 0; i < objects.length; i++) hidden[i] = false; } }
Now ArrayAdapter needs to override the function getCount(). This function usually returns the number of objects (rows) in the ListView. Typically, that would be the size of array items. In our case however, this size is not the real size of the ListView. We should subtract the number of hidden items from the size of items. And thereby:
@Override public int getCount() { return (items.length - getHiddenCount()); } private int getHiddenCount() { int count = 0; for(int i=0;i<items.length;i++) if(hidden[i]) count++; return count; }
Our public interface consists of two functions: one function hide() used to hide a certain row and another function unHide() used to unHide() a row. These function simply set the chosen index of the hidden array to true or false accordingly.
public void hide(int position) { hidden[getRealPosition(position)] = true; notifyDataSetChanged(); notifyDataSetInvalidated(); } public void unHide(int position) { hidden[getRealPosition(position)] = false; notifyDataSetChanged(); notifyDataSetInvalidated(); }
Now the real work is done in the getView() function. The idea is very simple. Hopefully you can get it easily. We have a data source which is our array items in this case. We will create our Views as normally. However, we need some kind of translation between the position supplied by getView() or, more generally, by the ListView and the position of data. For example, if index 0 is hidden. The ListView will call getView with position = 0 as expected. The only difference will be that the data supplied will not be from index 0 of the data but from index 1. This translation is supplied by the function getRealPosition as follows:
private int getRealPosition(int position) { int hElements = getHiddenCountUpTo(position); int diff = 0; for(int i=0;i<hElements;i++) { diff++; if(hidden[position+diff]) i--; } return (position + diff); } private int getHiddenCountUpTo(int location) { int count = 0; for(int i=0;i<=location;i++) { if(hidden[i]) count++; } return count; }
The last step is to translate the position supplied by getView to the new position using getRealPosition.
@Override public View getView(int index, View convertView, ViewGroup arg2) { //this is the secret step: int position = getRealPosition(index); if(convertView == null) { LayoutInflater vi = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE); convertView = vi.inflate(R.layout.some_layout, null); } // bla bla bla // initialise the view // bla bla bla return (convertView); }
This is all what you need to get a ListView that has the capability of hiding and unhiding its rows.
Enjoy
Click here for Sample Project + Source Code (download)
This doesn't appear to function properly at all.
ReplyDeletePlease check the sample project. It does work actually.
DeleteHi,
ReplyDeletegreat article.
Hi Sherif,
ReplyDeletewant to add a new item to adapter with the add method, but get unsupportedoperationexception.
do u have an example of howto add and remove items?
regards
eardgyl
Thank you! Works like a charm
ReplyDeleteHi Sherif,
ReplyDeleteVery useful example.thank you.
Hi Sharif, i have one issue. it worked for hiding rows but i hav implemented sorting on users selection. when am doing sorting hidden row again showing, when listview refreshed after sorting.
ReplyDeleteYou have to implement your own data filtering because the hidden array will be useless after filtering your data. A simple way would be to reinitialize the array hidden with values that reflect the real value from the original hidden array.
DeleteVery useful!
ReplyDeletetoo bad it throws an indexoutofbounds exception in getRealPosition
ReplyDeleteGot it fixed now. I just removed the call to getRealPosition in the hide and unhide functions. That was needed if the position parameter is absolute position in itemlist.
ReplyDeleteClever! Thanks!
ReplyDeleteGreat this article is very helpful. Thank you for the valuable information.
ReplyDeleteMore Info
ReplyDeleteA great information you have shared keep it up also try this website download instagram video
very infromative data u have shared ,plz also visit this site for entertainment youtubevideos
ReplyDeleteEngineering mathematics 1
ReplyDeleteVTU 2nd SEM syllabus
Basic Electronics Engineering Tuition Classes in Vijayanagar, Bangalore
Basic electrical engineering Tuition classes for B.tech students
Best Java Training in Bangalore
Java Training institute in Bangalore
Best Java Training institute in Bangalore
Java Training institutes in Bangalore
Best Java Training institutes in Bangalore
TreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
ReplyDeleteCheck out the best
furniture
tv stand nz
bike stand nz
Thank you for excellent article.You made an article that is interesting.
ReplyDeleteInformatica online job support from India|Informatica project support AWS online job support from India|AWS project support|ETL Testing online job support from India|ETL Testing project support||Pega online job support from India|Pega project support|Pentaho online job support from India|Pentaho project support|Python online job support from India|Python project support
Keep on the good work and write more article like this...
Thank you for excellent article.Great information for new guy like instagram image downloader
ReplyDeletepinoy tambayan lambingan and all the filipino Tv replays you will be watch online in hd. We will share with you pinoy1tv replays online in hd.
ReplyDeleteAwesome keep sharing Wonderfull post really appreciating Instagram Video Downloader
ReplyDeleteReally Nice Article! Your post inspires me too much.
ReplyDeletedownload video instagram from instafinsta.com
Internet Business Ideas for 2020
ReplyDeleteBenefits Of Micro Switch and More About the China Micro Switch Company
ReplyDelete