How to hide items of listview in android - android

I'm trying to develop an app in which i need to create a list which contain many items.
There is also some button represent categories , and i want to populate my list according to the category.
For example: if there are 10 items in the list out of which item 1,5,7,8 are belongs to 1st category and rest of them belongs to 2nd category,now if user press 1st category button then listview show items belonging to 1st category only.
How can I do it?

I think ExpandableListView will work for you. Refer this tutorial: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

If you wish to be able to filter (hide/show) items in a ListView you can use an adapter, for example by creating your own implementation of BaseAdapter. This adapter has methods for sorting, clearing and adding elements to the dataset used by your ListView, as stated in this list tutorial. Just remember to call notifyDataSetChanged() after you've altered the dataset to refresh the ListView.
Setup example:
ListView list = (ListView)findViewById(R.id.list);
adapter = new MyAdapter(...); // Probably send along data
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Filter example:
int[] newData = { 1, 5, 7, 8 };
adapter.clear();
for(int i : newData)
adapter.add(i);
adapter.notifyDataSetChanged();

You need to hide item in list (remove from list) before you set adapter. When your list is ready set notifyDataSetChanged() to adapter

Related

Add item to spinner after it has already been filled

I create a spinner somewhere in my code and fill it in with values
val dataAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, some_list)
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
mySpinner?.adapter = dataAdapter
My Question is, How do I add more items to this spinner after I have applied the adapter in a completely different section of code in Kotlin? for example, after a server request
I understand you can access the adapter via
mySpinner?.adapter
But how do I add to it?
EDIT.
I Understand there is a solution in the following code
some_list.add("Apple");
dataAdapter.notifyDataSetChanged();
However this requires having access to 3 data items at a class or global level instead of just 1 (the spinner). I understand this may be the only way, it is just sub-optimal, thank you for your responses
you can create a new adapter with a the newest list and then assign it to the spinner , or you can create a custom adapter with your item list and then just add the new item to the list and call notifyDataSetChanged()

Add new items to the start of List and notify adapter

I have an ViewPagerAdapter, and a List with ViewPager items inside of it. In some moments, I have to add items to my list. I've add they by List.addAll(Collection), call notifyDataSetChanged(), and all is working well. But sometimes I need to add an items to start of my List. I can add they, but notifyDataSetChanged() isn't working in case when start of my list is chanegd.
Any ideas?
Try this code
List items = new ArrayList();
//some fictitious objectList where we're populating data
items.add(0, obj);
listAdapter.notifyDataSetChanged();
This may work. You need not to use Loop and Notify List again and again
Add new items to the start of List
use following code for adding the item to List in onCreate() or where you want.
listName.add("item-1");
listName.add("item-2");
.
.
listName.add("item-n");
notify adapter
after that, use notifyDataSetChanged() to intimate to listAdapter for new Items added in list,
listAdapter.notifyDataSetChanged();
hope this will helps you.

Should we pass a copy of the items to RecyclerView, so that when we filter, we don't remove from the actual list?

I have 10 items in my activity.
They are displayed in a RecyclerView.
The itms are stored in a list;
List<CustomItem> items = new ArrayList<>();
I pass them to the RecyclerView adapter like so:
recyclerView.setAdapter(new RVAdapter(this, items));
Now, whenever I do filter the items by a string query, so that the
recycler view displays a subset of the items by description, for instance,
I have to pass a filtered list of items to the recycler view, like so:
List<CustomItem> filteredItems = CustomItem.filterByQuery(items, query);
and then I:
adapter.setItemList(filteredItems);
notifyDataSetChanged();
-> which sets the filtered items in place of the items I sent to it in the constructor, and effectively overrids the activity items because they both point to the same objects in memory.
And I realized that by filtering in order to display a subset of results, I am overriding the acitivy's list of items, because it is shared by the activity and the RecyclerView
However, of all the code samples and tutorials I've seen on RecyclerView, I've never seen anyone do a deep copy of the list of items and pass it to the RV, so what am I missing here?
I found the answer in the comments below the accepted answer here:
How to filter a RecyclerView with a SearchView
To quote:
#LalitThapa Just look at the constructor of my ExampleAdapter above. The second line should be interesting for you because that's where I make a copy of the List passed into the Adapter. What you want to have is two separate Lists. One in your Activity or Fragment which contains all items and a second List in your Adapter which contains only the filtered icons. Your problem is that you use the same List instance for both. – Xaver Kapeller Jan 17 at 19:05

add items on top of the listview android

I have a listview and I am using list.add(mylist) and adapter.notifydatasetchanged() ,its adding the items below the existing list ,How Do I add the Items on Top of the List.
Eg: If I receive new msgs It should be on top of the existing list.
Thanks in Advance.
Use add(int index, E object). Where index = 0.
Actually, if you bind your adapter to your ListView, you can also just insert the item into your adapter with the position (in this case 0)
list.setAdapter(adapter);
...
adapter.insert(data, 0);
It shoudl update by itself because you had it bound

Which is the correct way to add item into ArrayAdapter

Consider the following code.
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, resource, textViewResourceId, list);
// Method 1 : Add an item.
adapter.add("ITEM1");
// Method 2 : Add an item
list.add("ITEM2");
I was wondering, which is the correct way to add item into ArrayAdapter? As seems to me, both methods just work fine.
Method 1 updates the associated AdapterView, if you have already attached the ArrayAdapter to the AdapterView. Method 2 does not, requiring you to call notifyDataSetChanged() on the ArrayAdapter.
Typically, you populate the ArrayList before creating the ArrayAdapter, then use Method 1 to add new entries dynamically later on (e.g., based on user data entry).
This is what I do, especially for my Search Results page - where it grows as the user scroll down (list changes).
I'd keep a local ArrayList of Strings that is global to the class,
Initialize the adapter (also locally and globally),
Have a designated method alter the ArrayList of strings,
Then call the adapter.notifyDataSetChanged();
This will not only update your list and also update the adapter to work in sync.
Hope this helps,
best,
-serkan

Categories

Resources