I have a tabhost with 3 activities A,B,C
activity A has a listview with current adapter, then inside adapter i do a sqlite insert, then in activity C also has a spinner listview with another adapter. In activity C, i added method to refresh spinner content and also method for onResume() with refresh spinner content inside it. If I did insert from adapter of listview from activity A, spinner did not get any changes (I hope it can get changes from onResume(), because there is some code to refresh spinner content), but nothing happened. Also if I call the method for refresh spinner content using below code :
Context mycontext;
((Activity C) mycontext).RefreshSpinner();
then also nothing happened. So how can I solve for this problem ? i did it with 2 ways. Thanks in advance.
Hie,
In your Adapter, if the context your application is getting is from Activity A suppose and you want to refresh Spinner content which is in Activity C, you can put your code in onResume() method of activity C, or adapter used to bind Spinner of Activity C. For this, you can either create a static method with a changes you want and update it first in the Adapter class of Activity A. Then that changes to reload a spinner with some random list/data which u have updated in Adapter for Activity A can be used directly in Adapter for Activity C or in onResume() method of Activity C. Else you create a list with updated data and bind that in Adapter for Activity A, and populate it in your onResume() method of Activity C passing context to it.
Related
I searched over it and found two results:
Call
1
recreate();
2
startActivity(getIntent());
finish();
But I want to refresh the activity NOT from within the activity.
I have more than one fragments, each fragment has listViews which has buttons.
My listView is populated by the data on the server. On button Click I am changing my data on the server and want to refresh all the listViews (so as to make them load new content). The onClick() function is in the listAdapter. So is there any way to refresh whole Activity from this listAdapter class.
OR
Way to refresh fragments from the listAdapter class.
You should call notifyDataSetChanged() on your Adapter. See this question
Pass the context of your activity to your ListView and call this method from your list view
((Activity)context).recreate();
I have a custom ListView inside a fragment that represents a list of activities. Each activity is saved in a database (I'm using Parse), and the ListView is populated with the Activities from the database. There is an add button in the fragment that takes the user to an screen where he/she can create a new Activity object. When the user is done, the activity is finished (I call finish() on the Activity), and the fragment is brought back up. The problem is that the ListView doesn't refresh- it's the same list before the user was sent to the Activity Create screen.
I tried to override the onStart and onResume in my fragment class and call notifyDataSetChanged() or invalidateViews() but neither method works, the list view doesn't refresh so it includes the new Activity that was just created by the user.
Start the activity that adds Activity with startActivityForResult(). Override the onActivityResult() in your listview fragment to get notified when the user has finished adding an Activity. Then, refresh your ListView by querying the database again or adding the new Activity to the adapter (depending on how you implemented the listview adapter).
In your case it looks like you forget to refill the activity list attached to adapter from which listview generate views.
Refill the list again from scratch and then call notifyDataSetChanged();. Your listview will be updated with new entries.
Cheers
I have a activity (Activity A) with a fragment that has a ListView inside of it and I'm calling another activity (Activity B) to add records to the fragment's list.
My problem is that calling adapter.notifyDataSetChanged on Activity A's onResume/onFragmentResume and the fragment's onResume changes nothing.
My adapter is populated in Activity A's onCreate.
I also tried inserting the adapter.notifyDataSetChanged in a runOnUiThread.
Any one tackled this situation before?
It is not a good suggestion but try to reinitialize the adapter and set it again and tell me if it is work fine.
You should populate your adapter in the fragment in either the onAttach(), or onViewCreated() methods. You could also populate it in the onCreate() of the fragment, then call notifyDataSetChanged in either of these lifecycle methods I suggest.
These methods will be called whenever the fragment is connected and drawn, thus, you can be sure that it will populate your Fragment correctly.
In general, have all your view related items (like adapter creation and population) in your fragment, and navigation (ActionBar, and flowing between Activities) in your Activity.
I have a custom adapter populating a listview.
Each row has a checkbox which has an OnCheckedChangeListener set in the getView() method.
All is working in that regard, however I want to open the options menu in my activity that is 'hosting' my listview from the event listener in the Adapter.
I've tried passing in an instance of my Activity to no avail and I can't access a static method with openOptionsMenu() in my Activity from the Adapter class because openOptionsMenu() is non-static.
Any ideas?
I'm assigning my adapter like so,
mAdapter = new CustomFileAdapter<String>(this, filenames, this); (context, array, activity)
And the constructor in the Adapter like so,
public CustomFileAdapter(Context context, String[] images, Activity a)
Is the Adapter inside your Activity class? It could access the parent with a simple
Activityname.this
Otherwise, post how you've tried to pass the activity and we can see where you're going wrong.
Solved this by just creating a callback in the Adapter class to notify my main activity.
I have a listview which I am populating with a custom SimpleCursorAdapter, each row contains a button which, when clicked should open a new activity and pass the ID of the original data object so I can display the related image on screen.
I am having problems implementing the onclick event for the button and I understand you can only use startActivity() within an activity - is this correct? if so, is there a workaround as my cursoradapter code is in it's own class which extends SimpleCursorAdapter (ie. not in an activity!)
Just one more question if I may? - how can I pass the dataobject ID (ImageID) of the button clicked to the new activity?
set click listener over Button in getView() method in your adapter and use startActivity from there.