Adding to a string array later with a method - java

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.

Related

Recyclerview: Hiding Items without removing them from the ArrayList

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

Populate list dynamically in Codename one

I had some issues with a List concept in Codename one. I need a list of items populate dynamically at run time, but I don't know how doing it, so anyone helps me regarding this issue?
Depending on how your list is constructed... E.g. when you have a form with a container, which includes your list as labels.
Container c = new Container(BoxLayout.y());
//adding strings as labels
c.add("String1").add("String2");
Then you can later on add a String to this container
c.add("new String");
But the form won't update itself. So you'd have to
form.revalidate() OR form.animateLayout(150)
to be able to show the changes on the screen.

How to organize communinaction between 3 list view adapters

I have 3 list views and 3 adapters correspondingly. Then class Menu, that contains ArrayList of Products, where Product is also a class.
1st adapter contains ArrayList and display all menus in ListView1.
2nd must contain and display all Products from Menu, selected in 1st adapter.
3rd must contain all available products, that are not in 2nd ListView.
The question is, how organize architecture in such way, that selecting one item from first ListView affect 2nd and 3rd Adapters. I mean do I need to create a new class, that will hold all changes, or it is better to make ArrayLists static and call their updates from OnClickListeners?
I would think that the 2nd and 3rd views wouldn't even be displayed until a selection was made on 1st view, no?
Regardless, if 2 and 3 are exclusionary drawing from the same list of products, it sounds like you should be able to build them using the same underlying data source. How are you populating these adapters? A content provider sounds like it would suite this well if you have a large enough product list.
In terms of behaviour, when a selection is made on the 1st view, that should cause a refresh of your 2nd and 3rd views on the newly changed parameters. So if in the 1st view the user has selected a number of menus which contain products, the 2nd view should do a query of some kind using the selected menus as a key for which products to then display. The 3rd view would just be what's left over, or use the selected menu as sort of a reserve key, i.e. only get products not in said menus, which would be a minor change to the query.

Object to ArrayList to JList and back again (A few questions)

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?

Java Swing Updating JList

I'd like to know if there is any way that I can update a Jlist after the user adds or removes an item to it and after a user sorts it. Is there any way that I can write a standardized method to update the display based on the order of items in an array or vector, and when the user remove or adds an object from the array that the JList is based on?
Thank you.
Updates should be made to the ListModel, not the Array that was used to create the model.
However, if you want to refresh the list with completely new items or change the order of the items then you create a new DefaultListModel and use the setModel(...) method of the JList to update the view.

Categories