Removing an android menu item from code - android

I'm trying to make a subscription list in a navigation view where when you subscribe to an item the category of that item will show in the navigation view, and when you unsubscribe from every item in a category it should remove that category from the navigation view's menu.
Problem is to remove the category I need to clear the whole list and then add item by item, but the navigation view doesn't hold just the list of subscription: It also allows the user to navigate through different activities. If I use the menu.clear() method the navigation items will be removed as well.
To add them again, I need as parameters the item's ID, the group's ID, the Order int and the title. I know where to get the IDs and titles since they are coded in the XML, but I know not how to find the order in the list, or what that is.
Also if you have an easier or better way to resolve this, please let me know.

You can use menu
<group...>
Search for that in the Android docs.
Then setGroupVisible/enabled as needed.

Since you know the id of the menu item, you can just call menu.removeItem(id) to remove the item.

I was told the RemoveItem could cause problems, so I tried to avoid that.
What I did was find the item and group ID from the items I didnt want to get removed, clear the list, and then add the items with a made up order (I just made a variable and incremented it for each item I needed), then add the item names I needed for the items without interaction.

Related

Android. RecyclerView with collapsing items

I need to create recyclerView with collapsing items. For example, I have a list of 10 items. By default, I only need to flip the first 2 elements. But by clicking on the "See all" button, I need to display all the items in the list. And vice versa, by clicking on the "Hide" button, you need to leave only the first two items in the list. Here is an example:
I suppose to implement it like this: create a flag that determines whether the full list is displayed or not, and by clicking on the button, depending on the flag, send the full list to the adapter or cut it to two elements and call notifyDataSetChanged().
But this solution seemed to me not very good, perhaps there is a more elegant solution.
Note: I don't need nested collapsing elements. I just need to display two items from a list or all items.
Please help me.

Updating list item in array adapter

I have been trying to update the last element of my list view by doing the following
mNavTitles.remove(last_position); //mNavTitles is an ArrayList<string>
mNavTitles.add("Notifications Disabled");
((BaseAdapter)mDrawerList.getAdapter()).notifyDataSetChanged();
However this doesn't work. If I choose to add or remove exclusively, this works just fine. Why can't I update my list by removing and then adding? And if this is not possible how would I go about updating this in the simplest way possible? I've seen solutions that involve recreating the whole array list but I really don't understand why that's necessary when adding and removing both work.
EDIT, more context:
This is happening inside of the onitemclick listener from my nav drawer. When the last nav item is clicked it is supposed to update the title text of the item. I am unable to update onclick, merely add xor remove a list item.
mNavTitles.set(index-number, "new value");
For example if you want to update your 5th element in your arraylist. You shoul try follow
mNavTitles.set(5, "new value");

Extended list view: Show text input inside once clicked

I am working on a UI where I have a list view. Each row has 2 information. One is Product name and the other is product ID stacked one over other. So each row has 2 lines of data.
What i want to do with this:
Once a user clicks on each row, it will further expand and show one input and one spinner. So it is basically for putting in Quantity and unit of measure.
Please let me know if this is achievable and if I can refer to some example.
What I need is more like
Taskos To Do List | Task List
https://play.google.com/store/apps/details?id=com.taskos&hl=en
All i need is that the category will not be expandable. Only the items will be expandable. All i need is an example of such app and then I can take it forward.
I think it could be done... you would need to put the extra widgets in your row layout, populate them and hide them upon list creation, then in your onListItemCLick you could unhide them.
Note I have no idea if this would work, but it seems reasonable that it might and it's what I would try and do to achieve the goal.
listView cannot be expanded. however you can use an expandablelistView
But the kind of functionality you want cannot be done this way(I guess.).
You can use
registerForContextMenu("your list View");
to register your list view
onCreateContextMenu
for the kind of action you want to be performed in your case its dialog with EditText and Spinner to have input from user

android - list items open context menus

I'm new to Android. I have a list of items and trying to associate single context menus to each list item. I have set setListAdapter and onListItemClick but when I click on any list item I always get the same context menu.
Ideally, list item A should trigger menu A when cliccked and list item B should get menu B. Can't figure out how to do it. Could anybody help me find an example code I could use to learn how to do it?
I don't have any example code that shows the technique -- my best example is something I did for a consulting client.
However, let me point you to this sample project that uses context menus and use it as a basis for this explanation.
You need to return the customized menu in onCreateContextMenu(). If you always return the same menu here, you will always see the same menu. To determine which menu to display, you will need to know which list item was long-tapped. In the case of a context menu for a ListView, you can cast the ContextMenu.ContextMenuInfo supplied to onCreateContextMenu() to be an AdapterView.AdapterContextMenuInfo. That object can tell you the position and _ID of the item in the list that was long-tapped, so you can choose the proper menu.
In the sample code linked to above, I do that cast in onContextItemSelected(), so I can know which item the user is deleting. However, the same cast works in onCreateContextMenu().
The Android team recently released a number of new samples. I believe what you're trying to do has an excellent example here.

How can I "set View" in right place where I remove an item in ListView (Android)

I choose an item to remove from my ListView. And after the Item was removed, my ListView was scrolled back and display at the first Item.
I want my ListView display in right place where the Item I had removed (It like remove a contact in Android Contact list). How can I do that?
I suppose you want to update your ListView display, after you remove an item, right?
When you remove the item, you have to modify your data adapter, and let the ListView change accordingly (something like the MVC pattern behavior). I've seen somewhat similar questions (and the corresponding answers :) ) here and here.
Edit:
Aha, in that case, try using setSelection(int), after recalculating new indices and new item count.

Categories

Resources