I have a list of items and set of users. Users can re-order list items. Each user has his own view for the list, he can arrange the items in his own way. One item can be placed in different locations in list for different users. I've to track the item location for users in list item. Please give a suggestion to implement this. The size of the list is not constant. I'm using Java. My intension is I have to show the list for every user in their ordering format. Please Masters give me a suggestion.
You can keep an ordered list of, well, the order to retrieve items from your master list:
List<Foo> items = new ArrayList<Foo>();
// add stuff to items (say, 6 things)
Map<String,List<Integer>> userOrders = new HashMap<String,List<Integer>>();
userOrders.put("A", Arrays.asList(0,1,2,3,4,5)); // add user A's order
userOrders.put("B", Arrays.asList(5,4,3,2,1,0)); // add user B's order
// display for userA:
for(Integer i : userOrders.get("A")){
show(items.get(i)); // show the userA's i-th item
}
Here I'm also using a Map to hold the orders. Depending on your application you might use some other scheme to retrieve the user's custom ordering, but the principle will always be the same: if the user can have a custom order, you should store that ordering somewhere, and then iterate through the list of things based on that ordering (as shown above).
Also take care with the type of List you're using, calling get for random access on a LinkedList is substantially more costly than on an ArrayList.
Why don't you do a classic Object Oriented Design for this first, as suggested by glowcoder. Then you will have User class and Item class. User class will have a list of Item instances.
class User{
String userId;
List<Item> userItems;
}
A reverse structure would reflect in the database as well (User Table, Item Table and a User-Item Table with foreign key mappings to the first two tables). The Map of List solution suggested by Mark works, but is not the 'correct' approach in an object oriented language like Java.
You have one location that stores the actual list of items. Then each user gets their own model of the data. It is backed by the actual list, but model is more of a mapping between the actual indexes of each item and the display index the user has for the item. Then you have one viewer that can use the model to display the list in the GUI. As far as the viewer is concerned, the model is telling it the correct indexes. But the model allows for the conversion from displayed order to real order. As the user manipulates the list through the view, you update the index map and never touch the actual data.
Related
In an Android app I have a list of Items which consist of name and Category. Items and categories are stored in Android's SQLite database.
class Item{
Long id;
String name;
Category category;
}
class Category{
Long id;
String name;
}
User can select categories using ComboBox list shown in dialog
and then search items of selected categories by name.
List should be filtered by categories when user click "OK" button in categories dialog
and by name when user type some text in SearchView.
How to do the filtering in proper way?
I think that filtering both categories and name on every character typed in SearchView isn't good way in term of performance.
On the other hand filtering both atributes seperately produces additional list:
Full list
Items from full list filtered by categories
3: Items from previous list fitlered by name.
The other option I thought of is to query the database on SearchView text change to get desired result instead of filtering the full list.
Thanks
This is not a simple question. All developers do it differently. I suggest few things:
Make select on database only once, when user wrote first 1 or maybe 2 chars or choose category.
Remember selected from database list. Let's name it - originalList. Visible list keep on the other object like filteredList.
Use something to avoid filtering everytime. If you know what is reactive programming you can use it's features. If you want to do it in oldschool way, you can for example make a task/job for filtering, which will be canceled, when new filter order will apear. In that way you have always 0 or 1 ongoing filtering.
It doesn't matter how many data is needed for filtering if your query is processes by one task/job. It has to also be able to work with null attributes if nothing is set for category or name.
When user added new requirement - filter filteredList for optimization.
Remember that, when you are clearing the letters - the list has to also change. This time you have no choice and has to filter originalList or make select on database.
But this is the most complicated way, which is used, when the list of item's can be very big. Otherwise you can simply select all of objects from the table in SQLite database as an oryginal list and work on it latter in the Java code.
The way of doing it depends from environemt so number of items, number of columns, lenght of the names etc. Try to find a way suitable to tour situation - I am sure, there is many articles in the network about it.
I have a list of items where each item in the list has a list of items associated with it. I.e. I have a list of parent objects which have an instance variable of a list of child objects. I am trying to find an efficient way to aggregate all the items in the child lists to a single list in Java. I have to do this using Java7 functionality because this is for an app on google app engine which does not currently support Java8. I am sure there has been an answer to something like this but I am having a difficult time searching for it.
So I have a list of Objects which say correspond to an Order and each Order has a list of line items associated with the order. I want a list of all of the individual line items associated with each of the orders in the parent list
What I want is to create a list where each item in that list is an data table like below: http://imgur.com/a/mQdi5 Example data table The user needs to be able to sort items in the data table and needs to be able to sort the data tables itself.
Each table is going to represent a client and each item in that table is an order. The user will then collect the orders, if he collected an order the order wil dissapear but the user is also able to bring them back.
I've tried to put a Recyclerview inside a Recyclerview but this caused unitended side effects and bugs, also I read online that it is basically a bad practice. My initial intention was to use a recylcerview with a sortedlist.
I did some searching online and a lot of people recommended using categories between items so that you only need one list. But because I have data tables (CardViews) that can be independent sorted this isn't an option.
If anyone want to give me a nudge in the right direction, I would be really thankful.
So I've got this school project, and I would really like to approach it with the best practices.
I need to make a list of customers for an insurance company. Each of these shall have a unique customer number, generated in ascending order.
Every customer can have zero to many insurances, also stored in seperate lists for each customer. Adding of insurances will happen more often than adding of customers.
Every customer can also have any numbers of claims. Every claim also has a unique id number.
If a customer cancels all insurances. All data on this customer will remain as history.
All data need to be stored via one of the file classes in the Java Standard Library. Databases are not allowed.
Actions such as showing of statistics will also be available.
Users of the program will be employees, with rights to edit every data field.
Questions:
What Collection class would be the most effective one to use? LinkedList, ArrayList, Hashmap or any other?
What file class would be the best one for saving the lists? ObjectOutputStream?
What is the best method of generating new unique ids for both customers and claims? As private fields in the customer list class? Information on the next unique id has to be restored every time the program exits and restarts.
Edit:
Not looking for help with any code. Just advice on the most common classes to use in a scenario like this.
What Collection class would be the most effective one to use?
LinkedList, ArrayList, Hashmap or any other?
Ans - LinkedList and ArrayList are types of List. HashMap is a type of Map.
What implementation of List you want to use depends on your requirement. If you are going to perform insertions and removals of elements at different points of a List frequently, then LinkedList makes more sense. It is more efficient at, say for example, removing an element in the middle of the List. Otherwise prefer to use ArrayList.
What is the best method of generating new unique ids for both
customers and claims? As private fields in the customer list class?
Information on the next unique id has to be restored every time the
program exits and restarts.
You may want to use a Singleton to generate IDs, and also persist them to a file.
I have a webstore that allows the user to add products into the cart and do a checkout.
The problem is that whenever I add multiple products into the cart and display them on the cart page the sequence in which they are added is inconsistent with the display. In short, the products that were added last end up getting displayed on top instead of in the last spot.
I am using HashMap to add products into the cart and then iterate over them while displaying the values on the cart page.
Here the first arguments in the HashMap is ProductID which is String and the second argument is the Product object itself.
Am I using the correct Collection class i.e. HashMap for storing values or is there any other collection object which could solve this inconsistent display issue that I am facing?
Change it to LinkedHashMap to preserve order