Using the HelloListView example it provides code for making a quick toast notification based on what the user clicks on in your application. I have a list I display that when an item is clicked I would like to display associated information.
My application displays a List of Tasks (custom class) by subject in a very simple ListView.
The ListView is just 1 TextView object with the Tasks Subject as it's content.
When a task's subject is clicked I would like to show other information about that task. To keep it simple the task should have an AccountNumber associated with it (integer value). I would like to display that.
The most direct method to do this I think is to display my list of tasks by subject then when a user clicks on a subject I could search my Task List for a task that has that subject and pull out the account number then. This has it's holes of course. Most obviously what about tasks with the same subject?
Any tips?
Some more information on my code:
Classes:
Task - A single task, has things like start date, end date, subject, account number of the client the task is for
TaskList - A List of Task objects with functions wrapped supporting a list of Task objects
I display a list of tasks in a ListView. Right now I am only displaying a task's subject. I want a toast notification to pop up the account number of the Task object they just clicked on.
Override the list adapter class. Save all your 'tasks' as objects and just display the portion ('subject') you want when the adapter is queried for a specific item. Then you can trap the onClick like you do now and display whatever segment of the corresponding object you want.
EDIT: Its explained better here.
Related
I am doing a side project of making an app (with Java since I already know it). I have a recyclerview which loads some data via the room database library. The elements of the recyclerview are clickable.
My problem is I want the user to be able to sort the recyclerview so that the most recently accessed items go to the top.
My original idea was to assign the entities to have two variables - a String list_name which also serves as the id, and an Int order_of_access. Also, in my ViewModel I have a getAllLists method which returns a livedata list. I have an onChanged listener in the fragment activity which nicely updates the recyclerview when data is added/removed.
When the user adds a new list, it is assigned an order_of_access of the listsize (+1). But when the user deletes a group of lists, or clicks on a list, I want to update the order_of_access, say with an updateOrderAccess method.
Do you think this is the best way of doing what I want?
Where should I place updateOrderAccess and how would you recommend it be written? Since the method getAllLists returns livedata, it is tempting to put updateOrderAccess in an observer in the fragment (in onChanged) - but this will obviously create an infinite loop. It seems more in the correct philosophy to put it in the ViewModel, but then how would you suggest the updateOrderAccess method to be written? I'm having some trouble conceptualising what I need.
I hope the question is not too vague - I will update it if you need more details.
Where should I place updateOrderAccess and how would you recommend it
be written?
I am so sure that you must write it in the view model, as long as updateOrderAccess() is editing the list which is observable then you have andexpose by that the ui state then you have to put it in view model, and the observers will be notified ( in this case it is recycle view) and it will redraw the list in the order you offered.
note: do not you ever update the state(ui data) outside the state holder so you implement UDF (unidirectional Data Flow) pattern.
see the references below to read more about UDF so you never get confused where to declare your functions by letting the architicture lead you:
Guide to app architecture
ui layer
state holders and ui state
Do you think this is the best way of doing what I want?
i am not very sure that i got exactly what your app do, but it seems like you want to re-order the elements of recycle view depending on the ui event (click) or data change (deleting or adding new element), now you have two choices:
if the order is very importnat to you that much you want to keep it even if the app has been destroyed
then you have to add a field in the room entity represent the ordering (let us call it order) and whenever the user click on the recycle view you have to update the rooms field "order" which is "flow" or "liveData" or any observable type, that will tell the view model that there is a changing in the data, now the view model have to re-order the new data by the field "order" and pass it to the recycle view to show it.
if your app do not have to save the order changes after the app been destroyed
then you can simply do that:
create list which is called "orderedList" you will put the list items in it by the right order, and another list called "unorderlist" which have getAllLists
for the first case where the ordering is being changed by user click, you
can declare a function in viewModel then use it in the ui
controller (your activity or fragment), so whenever the list item is
clicked this function just re-order the orderedList elements ( which
is observable, so the changes reflect on the ui ) just by change the
clicked item position to the front of the list.
for the second case where the ordering changes by data changes like
add or delet a list item in the database, then you have to compare
the legnth of orderlist and unorderlist legnth, if unorderList is
longer then it is an add situation else it is a delete situation, in
adding case just add the last item of unorderList to the orderList,
else you have to check the deleted item and delete it from
orderList.
Hello this is a simple design question for better practices:
Example and Context:
I'm creating an app that shows a list of items in a players stash. For this i need following requests:
Ask for all items IDS, one request.
Look for each ID and inside each response i'll get the ICON link. As many requests as ITEMS here
Request each ICON. As many request at items here.
All this will be presented on a Recyclerview list with cardviews and saved locally to the device database.
The question is: In which order should i do this???
Option A: get items IDs > Save to DB > Read DB > get ITEM OBJECTS > Save to DB > Read Database > GET ICON IMAGES > FILL UI LIST
Option B: get ONE item ID > get ONE item Object > get ONE item icon > present to UI > save Item Object to Database
ON both cases i will have timeout or try cach for items that fail to get.
ON both cases in the future I will only request ICONs for existant items or full requests for new items in stash.
If you have another suggestion or approach feel free to add it.
If i was you, I would create the loading so that the user sees items without the user having to wait too long.
I would (asynchronously) do the following:
Get Item IDs
Request all items. When a request completes, add a item with a placeholder ICON to the UI
For each requested item, request the ICON. When a ICON request completes, update the UI and save it to database
While the objects are loading, you can show a spinning progress bar on top of the RecyclerView, so the user sees the progess bar spinning, and the items being added one by one in the background. You can even add an animation very easily to the RecyclerView, so your app looks even cooler while loading.
I have ListView that is populated like so:
ArrayList<MyFullDataClass> myFullDataClassList = Utilities.getDataFromSQLite(getActivity()); // populate list from SQLite
ArrayAdapter<MyFullDataClass> adapter = new ArrayAdapter<MyFullDataClass>(getActivity(), android.R.layout.simple_list_item_1, MyFullDataClassList);
setListAdapter(adapter);
MyFullDataClass contains many things: Name, address, phone, email, web site, etc. So as it is, each row of the list contains all of this information. It's a little busy. I would like to make it so that each row in the list contains only, say, name and email (and then touching the row would popup with all information in MyFullDataClass)
I could do this by creating a class, call it MyPartialDataClass, that contains fields for only name and email, then create an ArrayList<MyPartialDataClass> and copy data from myFullDataClassList to myPartialDataClassList and use this partial class for the adapter. (Then when a row is clicked, use myFullDataClassList.)
Not particularly elegant, but it would work.
Is there a better way?
I don't think there's a need to have two separate classes which ultimately hold the same information.
Having one class to hold necessary information is fine, unless you absolutely need minute efficiency gains of having two classes. What I would do is make your Adapter take a MyFullDataClass object list, but only populate the views with name and email.
From there, you can listen for an onClick event on your Adapter and pass the MyFullDataClass object associated with the clicked view to a fragment which will display the rest of the information associated with the MyFullDataClass object (i.e. the fragment will display address, phone, etc in addition to name and email).
You wouldn't make two separate tables in a database to hold parts of the same information. You would select what rows/properties from each entry that you need. The same concept applies here, IMO.
I'm doing a listview in Android that takes the date from a json with some information, I want to know the fastest way to create a new activity with all the data of one item.
What is more efficiently pass and id of the item in the intent and make a new request in the new activity or pass all the data of the new activity in the intent?
I guess that make two requests is worse than make one and pass the data in the intent, but maybe this information is too big to pass in the intent.putextra();
Thank you!
If you have the same information in the Listview that you'll have in the activity, then passing that info should be faster than making another request.
Nevertheless, if you only show a small piece of info in your ListView (which will also make it smoother) and then you show the full item when an element is clicked, then the first aproach is better.
Example:
I have a list with my contacts names and when i select one, i open a new activity requesting and showing all data of that contact.
This will be faster than have all unused info in my ListView.
In my application I am fetching the data from a web service in XML format, and parsing it and showing the data in listview. The problem is that if the web service contains 5000 objects then it takes a lot of time to display the data.
Can it be possible to show some data in listview and fetch the data at the same time at the end of the list.
Please provide me some sample code.
If you use convertView in your ListAdapter´s getView method it should not matter how many items you have in the list since the Views are beeing reused.
If your Listadapter takes an array of som sort you could add items to the array continuosly and call
mListAdapter.notifyDataSetChanged();
every time new data is added to the list.
By Using AsyncTask you can do this easily as each object is being fetched can be shown in listview using publishProgress() method while also updating user about what percentage of data hasbeen loaded.
Update:
By the way according to your situation the tool below which is developed by commonsware https://stackoverflow.com/users/115145/commonsware will suits you best...
https://github.com/commonsguy/cwac-endless
cwac-endless: Provides the EndlessAdapter, a wrapper for an existing ListAdapter that adds "endless list" capability. When the user scrolls to the bottom of the list, if there is more data for this list to be retrieved, your code gets invoked in a background thread to fetch the new rows, which then get seamlessly attached to the bottom of the list.