I've been searching for that for quite a while and haven't found a proper answer:
Is there a way to remove an Item from a RecyclerView without removing it from the underlying ArrayList?
I want to add a functionality, to temporarily hide Items from the List, when a certain condition is true. I still need those items back later, but managing 2 separate ArrayList seems overly complicated.
If I set the Visibility of the Item inside the Adapter to gone, I have a visible gap where the item was, so that is not a solution.
Is there any way by which I can avoid managing 2 separate ArrayLists?
I think you'll have to actually remove from the data model. Otherwise, you'll either end up with blank rows, as you say, or when you scroll, the "hidden" recycled elements will get jumbled up.
Just because you remove from the the Arraylist doesn't mean you can't add another list to (temporarily) store the items that were removed.
For example, define this in the adapter
private List<Item> removed = new ArrayList<>();
public void remove(int position) {
removed.add(items.get(position));
items.remove(position);
notifyDataSetChanged();
}
If you need better functionality about which items you have removed, a Hashmap may be better.
you have an ArrayList of object type
You have a model/pojo class of Object type
while iterating JsonData from JsonResponse just pass boolean value true/false in object type Model class. The data you want to show make it true else false, later you can set the 'false value to true' to your Model class.
Hope this will work.
In my case, I was using getApplicationContext() instead of getContext or this
Related
I am making a Javafx application and I have a List<String> that I update constantly and I want it to sync with a ListView I have on screen without me needing to update it manually each time. Like binding the ListView by reference. Is this possible? and if so how do I do it? Thanks.
From your question and comments I gather you are trying to do something like:
ObservableList<E> list = ...;
ListView<E> view = ...;
view.itemsProperty().bind(list);
This will not work since bind expects an ObservableValue and ObservableList does not extend that interface. You don't need to bind the items property though. Whatever ObservableList is in the items property will be observed by the ListView for changes (i.e. additions, removals, permutations, and updates1). This means you should be doing something like:
ObservableList<E> list = ...;
ListView<E> view = ...;
view.setItems(list);
And then modifications to list will be reflected in the ListView.
If you need to bind the items property then your ObservableList will either need to be held in an implementation of ObservableValue<ObservableList<E>> or an ObservableListValue<E>.
I recommend reading more about the basics of JavaFX; such as the tutorial on ListView or the documentation.
1. An update change event is fired when a property of an element has been invalidated. In order for an ObservableList to observe the properties of its elements, and thus be capable of firing update events, it must be created with FXCollections.observableArrayList(Callback).
Program Outline:
I plan to make a simple Java program that will load Vehicle objects (Vehicle being the superclass, EnginedVehicle and GoodsVehicle being the subclasses) from an XML file into an ArrayList which will then be displayed on a JList. The user will be able to show/hide the different Vehicle types using check boxes, add a new vehicle type or press the selected item in the JList and edit or delete it. The program will then put the Objects back into the ArrayList where it can be then saved back to the XML file.
Question: So, I am completely fine with the loading of the XML file into the ArrayList and putting that object onto the JList but the thing that is hurting my head is thinking about how I am going to:
What is the best way of getting the object back from the JList ready for it to be modified or deleted and put back into the ArrayList?
How would I show/hide the different types of Vehicles in the JList using the check boxes?
I understand this may seem a lot but, this is my first post and I am new to the community and I have fairly good knowledge of Java and OOP programming but I have just finished writing a fairly big website and going back to Java is a headache.
Since your ArrayList should be equal in size (item count) to your JList, your JList will have the index you're interested based on selection. Regardless if you want to modify or delete the item, store what index it was at and remove the item from the JList (You should be using a DefaultListModel). Use this index value to get the object from your ArrayList. If you're modifying, modify your object as needed, you shouldn't have to remove the object from the ArrayList for modifications, and place it back into your DefaultListModel. If it's a delete, then just remove the object from your ArrayList using the index value you stored.
As far as displaying (show/hide), clear your DefaultListModel (which will clear your JList), iterate through your ArrayList and add the items to the DefaultListModel that match your checkbox selection criteria.
EDIT:
I didn't take into consideration of possibly modifying/deleting items when items are hidden. For this, may want your objects to have a field that stores what index they are at in the ArrayList. This way when you do your filter, I would copy the items from your "Master" ArrayList into a sub list that you can populate your DefaultListModel. Then you apply the same logic to this sublist when selecting an item from you JList, then take your changes from your sublist and apply them to the "Master" ArrayList.
Keep in mind that when you remove an item, you'll have to reassign all items index location from that point on down.
I'm sure there is probably a cleaner way of doing this, but this is what first comes to mind for me.
I don't know if I'm horribly mistaken, but why change to a JList at all? Do you use your JList as a parameter to visualize the information in it? If yes, why dont you use your ArrayList instead? Then the checkboxes only change the visibility ot the Items of the List. So you dont have to care about indices, because they stay the same. And new entries, can be made as well... maybe im wrong but i guess you got kind of a GUI for the user to browse/alter/add new vehicles?
I'm working with a javafx listview in muliple selection mode, specifically a method which deletes selected items in the list.
the method with a observablelist
list.getItems()
.removeAll(
list.getSelectionModel()
.getSelectedItems());
the method with an array list
list.getItems()
.removeAll(new ArrayList<Object>(
list.getSelectionModel()
.getSelectedItems()));
Why would these implementations produce different results?
The arraylist implementation removes only selected items.
The observablelist implementation removes everything after the first selected item, whether it's selected or not.
Could this be a bug in the javafx removeAll method?
The ObservableList is backed by the actual model, and as such changes along with the listview while the entries are removed. By first copying the selected elements to an ArrayList, you eliminate the problem: the created ArrayList does not depend on the listview, and as such does not change during the removeAll operation.
list.getItems().removeAll(
new ArrayList<Object>(list.getSelectionModel().getSelectedItems()));
This is creating an ArrayList<Object> whose Object it contains is another type of list structure.
So in your first case the list.getSelectionModel().getSelectedItems() gets a list of items and the .removeAll looks through that list to see which items to remove.
In the second case the new ArrayList<Object>(list.getSelectionModel().getSelectedItems()) is an ArrayList of a list - the .removeAll looks for that list within the list that calls it.
hey guys i am new to ZK framework i have a listbox being sorted in the view later i pass the listBox to the controller and i need the items being selected by the user but in the model the items are syncronized with the sorting but in the getSelection array is not syncronized with the sort insted with the original data here is the code.
public void createPDFFromModel(Listbox list,String ref){
BindingListModelList model = (BindingListModelList)list.getModel();
for(int i=0;i<model.size();i++){
System.out.println((((ZamoraListitemAdapter)model.get(i)).getName()));
}
System.out.println("-------------------------------------------");
//Data Printed OK.
java.util.ArrayList<ZamoraListitemAdapter>selections = new java.util.ArrayList<ZamoraListitemAdapter>(model.getSelection());
for(int i=0;i<selections.size();i++){
ZamoraListitemAdapter clazz = (ZamoraListitemAdapter)selections.get(i);
System.out.println(clazz.getName());
//Out of sync with model and with sorting
}
my question is how i get the order of the items after the sort in getSelection model. i am using ZK 5.2.8
You could sort selections after you create it.
There are maybe other/better solutions, but you have
to write what you try to achieveif you need more help.
Reply to Comment
What I mean is
ArrayList<ZamoraListitemAdapter>selections =
new ArrayList<ZamoraListitemAdapter>(model.getSelection());
Collections.sort(selections);
So selections is a List of all your selected items and as long
as you compare the items the same way, they should be in the right order
In case you mean reorder and not sorting...
For me sorting means it is automatic and done by a algorithm.
Reordering instead means you, for example, drag the objects around.
If you mean reordering, and your Model hasn't a way to know if
a item is selected, you probably did something wrong.
Cos zk has got the two classes ListModelList and AbstractListModel
wich implement all methods needed for selection behavior and a custom
Model should, if the programmer wants a selectable Model, inherent
from one of them, cos it's the easiest way.
You should may also read this.
model.getSelection()
returns a Set (no order), instead use
model.getInnerList()
that returns ListModeList
I'm currently making an app for a forum, I want to add a favorites feature where there is a list of different sections of the forums people can add to their favorites list for quick access to them.
I'm having one slight problem though, I'm using a listview for the list of their favorites, so I need to add a new value to the listview when the user adds a new section to their favorites.
So really all I need to know, is how can I add to my string array for my list view, with a button? Or can I have all the options in the array and then decide witch ones show up according to what the user has chosen?
You could send an array of string with the current favourites to the list adapter. When more favourites are added, you can add them to the array and call
list.notfiyDataSetChanged();
This will update the list.
If you have a large enough array to hold all the possible options you can use setVisibleRowCount(int) to change how many are visible based on how many items are currently in the array.