I am trying to achieve Many to one mapping in ListView and populate elements relating to that item from the database
Hello I am trying to solve a problem which involves launching an activity on click of an item
I am successful on launching an activity on click of an element
I have a requirement and need some ideas on how to achieve that
As I said onclick of an item I launch an activity as many to one
mapping, Suppose I click an element Item-1 the activity should
populate the text-views in the new activity from elements related to
that activity
Again same if I click item-2 it should populate the text-views of in
the same new activity with elements related to that item 2 activity
from the database
Many to one mapping is must I am trying to achieve
Any ideas on how to achieve this
Any links to understand the concept will be helpful
Ps:: I am retrieving data from server as JSON resonse
1) You can use an array adapter or base adapter to populate your list view. Syntax and concepts are well explained on the developer's website.
2) For the list, setup an OnItemClickListener and inside the function, pass an intent to the next activity. While passing the intent, use the putExtra feature to pass your item selected details to the next activity.
3) In your next activity, get the data from the previous activity and use the data to traverse the JSON response from the server. Using an Async Task will be essential at this juncture. You can get an idea about Async Task by just googling it. Many great tutorials are available.
4) When you get any data from the server, create dynamic textViews to display your data.
References:
Developer's Website
Related
In my app I get some data from server in json. Then I fetch fields from json and fill my RecyclerView list with custom adapter. This list is filled with data about jobs. When I click on a job of my recyclerView I move from hosting fragment to dialogfragment for viewing it. This dialogFragment contains homesUp button after pressing which I return to hosting activity. At this dialogFragment I have button which send request to server about this job for adding it to another DB at server.
But when I press this button and send request to the server then after returning to hosting activity I see that data of selected isn't changed. I have several ways of solving this problem:
Add to my Singleton class variable of ArrayList which will be filled by data of shown job and then I will check whether my Arraylist from Singleton contains this id
another method - notifyItemChanged()
All these methods don't work because my list is filled by old data and I only dismiss dialogFragment and return to fragment which contains the list with old data.
Only one way - send request to server for filling the list again.
So, I need your help or useful advice :)
You need to broadcast changed data from dialog fragment to previous screen. For doing this can use LocalBroadcastManager or rxjava or even live data.
Or create a pagedlistadapter from paging library released in android architecture complement and load data directly from query.
Reference: https://developer.android.com/topic/libraries/architecture/paging
Hello Everyone,
I had do a search on activity retain state.So I am getting to options/solutions for that:-
Using savedInstanceState() and retainInstanceState() methods.
Using Parent Child hierarchy declare in manifest and its working.
(Example Url:-How can I return to a parent activity correctly? I had same problem mention in Note scetion of the answer.That case is match with my problem.
So,I want to retain the activity states just like Whats app(call/chats/contacts fragments).
But In My scenario,I am fetching the contacts from server.So how can I persist my data while switching between chat and my fragment activity?
So while timing to fetch new data of my contact list from server.I want to save ui of my listview/recyclerview with old data previously I had.
Also suggest which method is good from above two methods or need to implement in other way.
I'm an Android newbie and I'm trying to update my (working!) code that uses a cursor so that it uses a CursorLoader. My app begins with an Activity with a ListView with three choices, Beginning, Intermediate and Advanced. Depending on which item is chosen, I want to open a second Activity with another ListView. That ListView will be populated from a SQLite database where all the items have a level of e.g., Beginning. Elsewhere on stackoverflow, it was suggested to me that I insert an Activity between these two activities to handle the actual data retrieval. So now I have three Activities. One has a ListView with three choices. That one sends a String to the second Activity via an Intent. The second Activity queries the database and retrieves one column from all the records matching the level chosen into a CursorLoader. And here's my question. I want to pass all this data to my third Activity so that the data can be displayed in a ListView. But how do I get the data out of the CursorLoader? I'm thinking I want to do something like:
Intent data = new Intent(getApplicationContext(), com.MyKnitCards.project.StitchList.class);
Bundle dataBundle = new Bundle();
dataBundle.getStringArrayList(myData);
where myData is an ArrayList of data from the CursorLoader. I have no idea how to do this. I'm wondering if I want to use LoaderManager to accomplish this, but I have no idea how to do this either.
Thanks in advance!
I would get rid of the middle man Activity that does the data retrieval and just do it in the Activity that displays the data. CursorLoader that automatically loads in the background so it won't freeze your UI, and that way you don't have to worry about passing data between activities.
I am still searching for the best solution howto use a layout with a menu and a toolbar and inflate or start activities in android. My question may sound confusing, but im trying to explain it in an example.
Lets say im programming an android app (surprise.. i really do)
My app can do following:
User can log in [3] or register [2]. If he logs in, a new activity starts and his dashboard will be shown. If he registers: an activity for the registrationprocess starts.
Registrationprocess: user puts in his desired username and password and presses a button to accept. His data will be formvalidated and if valid, a new activity starts where he can choose his settings. Backbutton works and data can be passed to the new activity. After the last registrationwindow data will be saved and dashboard started. Starting new Activities is fun!
Now THATS where it gets complicated. Dasboard has an 'actionbar'(top) and a 'toolbar' (bottom, like tabs). So everything should be viewed in the middle part of the viewport(from now called main view). No more activity switching :(, tho.
Currently each tabclick removes all views from the main view and adds its new view. Look great, can be animated and works like a charm. Except: its currently not dynamic.
So... i don't know how to solve it the best way. For example: i fetch data from a webservice, create a listview out of it and it's extending listactivity. This activity i can't start but this data need to be put into the main view. How can i do it the best way?
And is it efficient?
I'm practicing and it's actually my first small discussion i want to start. So... FIGHT! ;)
UPDATE:
I've seen an interesting way to start activities and get results.
Launching activity through intents
. Is it possible to insert new/ update views after activity started? I would then generate my results in a separate activity. Update the view. Return back to 'dashboard' and load the view that was just updated. Possible? Or inefficient? And how can i update a view out of another activity? There is so much i need to learn :/
UPDATE2:
A good example of an app that has done it: Google+
Too bad i don't have their sourcecode ;)
UPDATE3:
What is best?
load a new activity, disable animation and set selected toolbox tab +
disable backbutton functionality
startActionForResults, fetch results and update current view (still don't really know how that would be possible)
viewFlipper onflip changing+updating data in flipped view.
I still don't know any efficient solution. Or am i missing something essential? I've just finished my ListActivity to fetch data from my webservice. But it still runs in a separate activity. How can i implement it into my "main view" now? Ofcourse... i could set a list my custom adapter. But currently im updating and fetching data from the server when i create the listactivity.
Im afraid this could be the only answer i'll get: Embed external Intent in main Activity
UPDATE4: I'm trying something.
Based on nininho's answer (thank you!) im trying the following approach:
Start Dashboardactivity and create a ViewFlipper.
Each Toolbarclick represents a certain ViewFlipper page.
Each Page has a Listadapter implemented and shows different results (different webservice queries). (ListView, GridView, with profileimage, without profileimage)
On Toolbarclick start AsyncTask or Service and notify List in current Page that data has changed. (ofcourse IF data has changed). Switch to page that was clicked.
Implement updatefeature. On scroll to bottom of list = fetch more data and add it. Update other lists automatically after 5min. or update list on update-button click.
PROs so far: Backbutton standalone for whole activity. Page-flip-animation possible. Async updating of lists and still possible to switch to another list.
CONs: ... someone has any? What about efficiency of such an approach? Does the ViewFlipper carry all the information so the performance would go down or does the viewflipper recycle its Views (like ListView)?
UPDATE5:
If i have some time i will make everything here more read- and discussable. Don't be mad at me for reading my rubbish ;)
From what I understand you want your app to start, fetch some data from the internet and after show this data on the main screen.
I don't see the need of a second activity to fetch the data because from your explanation you want to use it only to fetch the data, so the best approach would be:
Create one Activity (your dashboard)
Start an AsyncTask or Service on the background to fetch the data.
When the fetch ends, notify the activity that it ended.
Change your dashboard to show the list (you can use a ViewSwitcher if you want some animation or just create a layout with the list invisible and then change to visible).
ps: you can use a ListView outside of a ListActivity, just create a ListAdapter to create the ListView items and add this as the adapter for the ListView.
Lets say I have 2 activities:
A: A ListView displaying articles titles. Data is fetched from a web server and converted from XML to a list of ArticleSummary. Only user titles and id are returned by the server. Click on a title starts activity B.
B: A form to edit an article. Article is fetched from server. When the user hits OK, modifications are sent to the server and activity closed.
When the user go back to activity A, I would like to update the article title without any additional web request.
I was thinking about the following solution:
When article is modified, send a broadcast event with article id and new attributes values.
Listen for this event on activity A
Update the ArticleSummary object
notify data changed on ListView
Is there a better approach ?
If you want to have a shared data model between different Activities, you can place it in an extension of the Application class. Or, you can use a singleton. Just reload the data from the shared location when the ListView activity is restarted.
As Fredley alluded to, if you have are communicating with a server you should be sure to do so in a separate background thread.
you can also use startActivityForResult() to launch activity and managed returned data.
Check the "Returning a Result from a Screen" section in the part below.
http://developer.android.com/guide/appendix/faq/commontasks.html