ListView Activity & Dialog - android

I've a listView Activity where user could open a dialog to display more information about a line of the list.
On the Dialog user could modify a line. So my purpose is to refresh listView when User modify it by the Dialog.
The problem is that I couldn't use myArrayAdapter.notifyDataSetChanges() so How I could do that ?

Presumably you are using a database to populate the list, or perhaps some kind of array. Why don't you simply create a function that fills the list. Every other method that is called that might change the data in the list would simply modify the content in the database for instance (update, add) and then call the list filler method. This puts all the code for filling the list in one place that can be easily called whenever you need it.

Related

How can I update item of recyclerview from dialogfragment?

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

How to get data from custom dialog to activity?

I open a dialog from a custom adapter of button click in row item.
In the dialog I update that item.
My all data comes from SQLite Database.
The problem is the dialog updates data in the database and get dismissed but the adapter is not notified. So I don't get updated value in that item after dialog is dismissed.
How can I fix this?
Any help will highly appreciated.
You have to do it manually. Just add this line somewhere in your dialog:
((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();
To call it on the UI-Thread, use the runOnUiThread() of Activity.
The most popular function for refreshing adapter values is notifyDataSetChanged(). Have you tried to call this one on your custom adapter after you update values in your dialog?
Another thing you can try to do is to populate the adapter with all the new values, so after you edit an item in dialog, you get all items together from database again and set up a new adapter for a listview (with new values) and them use it by calling listview.setAdapter(adapterwithnewvalues)
This answer is not really any specific solution to your problem, it is just an idea what you could try :)

Prepend item in ListView and show in top

i am facing a problem like , i am developing an android application where all the records coming from database using REST call and displayed using ListView. now on the same screen one button is there to post the new record.
when user click in the button to post the new recored it will come as popup(DialogFragment). if user post any record from there, the record should prepend to the ListView without any rest call.
the user should feel that recored add to the db and immediately displayed on the screen.
Thanks in advance and any help is appreciated.
So, I guess you are using a list adapter which extends from BaseAdapter or ArrayAdapter.
After the user added a new record, you should add this into the list of records and than call the method notifyDataSetChanged() of the adapter. This way, the new record will be immediately displayed on the screen and you can later on add it into your database (before the activity be destroyed).
Here you can see a example with notifyDataSetChanged()
How to correctly implement BaseAdapter.notifyDataSetChanged() in Android

view selected items from listview to next tab android

I'm working on sales app. I have a listview that contains 10-15 items, which are selected through check button. I want that the selected items should display on next Tab.
Any suggestion/help? How i should do that?
Have you considered using Loaders ? I never use this class myself but it seems to be what you are looking for. Here is the link
Introduced in Android 3.0, loaders make it easy to asynchronously load
data in an activity or fragment. Loaders have these characteristics:
They are available to every Activity and Fragment.
They provide asynchronous loading of data.
They monitor the source of their data and deliver new results when the content changes.
They automatically reconnect to the last loader's cursor when being recreated after a
configuration change. Thus, they don't need to re-query their data.
You can also write into a file when you are selecting your items and read it when you display the selected items tab.
Just save your selected item to some global storage and use mTaHost.setCurrentTab(your_next_tab); and then in that Tab fetch the selected item that you stored and display it.
You could have a static public field in the class of the next tab that loads that specific item/items when that tab is reach and clears after it is used.

How to manage activities in Android?

I identified a problem in changing one activity using tab. In one tab activity I'm adding data to my SQLite database, and in the other tab activity I am displaying them using listview(array adapter). But when I come back to add data after adding new items to SQLite, the newly added records are not updated in my listview.
How do I fix this?
You seem to be pulling the list data from a DB. Is there a reason why you are using an ArrayAdapter instead of a CursorAdapter?
Anyway, you should call notifyDataSetChanged() on your list adapter when the data has changed so it can refresh the view.
you can add code to update your listview (via notifyDataSetChanged or some such) by overriding the onResume() method in your activity which is called whenever the activity is brought back to the foreground.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Categories

Resources