How to get data from custom dialog to activity? - android

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 :)

Related

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

CursorAdapter.swapCursor not refreshing the first time

I have a ListActivity and use a CursorLoader to load the data which is stored in a ContentProvider. I use a custom class extending CursorAdapter to display the list items. In the LoaderCallbacks onLoadFinished I have the following:
public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
cursorAdapter.swapCursor(newCursor);
}
I have a custom layout for the ListActivity which includes a TextView with android:id="#android:id/empty".
The problem is that when I open the application for the first time calling swapCursordoes not refresh the ListView even though there is data to show in the ContentProvider. When I add a new item to the ListView, the list is refreshed properly. However, if I comment out the TextView, which displays a simple text when no data is available, the application works as expected. The swapCursor call automagically updates the ListView accordingly.
Any thoughts on why this occurs or if there is a proper way to do this since I believe calling notifyDataSetChanged won't do the work as the refreshing fails on a very particular case?
You're having this problem because ListActivity automatically sets the empty view (if available) to your ListView.
I'd suggest you try one of these:
Extend activity and after swapCursor call
listView.setEmptyView(findViewById(android.R.id.empty);
Make the empty view gone: android:visibility="gone" and after swapCursor call
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
#onCreate call listView.setEmptyView(null) and after swapCursor call
listView.setEmptyView(findViewById(android.R.id.empty);
Not sure about all of them, but one of em will certainly work :)

ListActivity with Recursion Android

my problem is the next one:
I want to use a ListActivity with recursion, so that it is called "x" times, and everytime i click in one article on the list, it is re-calling again the same Activity but it is loading different data, cause what I wanna do is to make menu and sub-menus, and the expandableList is not enough for me because there are gonna be n-levels(i will know dinamically.....).
Anyone has an idea how can I implement it??
Thanks!
Store every data in some kind of N level ArrayList. Write your Adapter class to accept a level of this list. On the item click, it passes to the next level of your ArrayList and you call a notifyDataSetChanged() on your list.

ListView Activity & Dialog

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.

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