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.
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 added a button in expandable list view header. On clicking the button I need to pass a value from adapter to activity.
You can add listener or simply use ((YourActivity)mContext).setMyValue();
The mContext is the context from your adapter constructor.
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.
I have two fragments.
The checkNodesFragment creates dynamically an ArrayList of checkBoxes and then
the ReserveNodesFragment should access which of them were checked in checkNodesFragment.
What storage method is better to use, so as ReserveNodesFragment can see what checkBoxes where checked?
After searching, i found that SharedPreferences is a method to store data permanently. Is SharedPreferences suitable for making data visible through fragments?
I am newbie in android, so sorry if the question is obvious.
The communication between fragments should be done via Activity.
Sharing Data between Activity to Fragment
Create a bundle and use fragment.setArguments(bundle) to pass data to Fragment.
Sharing Data between Fragment to Activity
create an interface in your fragment and in your Activity implement the interface
More info:
Communicating with Other Fragments (Download the sample).
If you wish to transfer data across fragments in runtime you can simply use Activity.
Every fragment has method getActivity(). It's a kind of parent for your fragments so you can use it as a "bridge". Nice solution would be defining callbacks in your fragments. Then your Activity could be working as registered observer for these callbacks and react accordingly e.g. listen for changes in the first fragment and then set something on the second fragment.
In more details.
Define interface in your fragment that provides information about changes. Create method that will be called in fragment when checkbox is selected.
In onAttach() method check if getActivity() is instance of that interface
Make you Activity to implement that interface
Implement method in Activity. Inside that method call second fragment and pass updated information.
Nice example can be found when you create an Activity from a template with fragment in Android Studio.
As i mentioned, i have two fragments. checkNodesFragment creates dynamically an ArrayList of checkBoxes and then the ReserveNodesFragment should access which of them were checked in checkNodesFragment .
But i have six Arraylists of checkBoxes that ReserveNodesFragment should know:
//The ArrayLists with the checkBoxes
public ArrayList<CheckBox> orbitCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> gridCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> usrpCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> disklessCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> icarusCheckBoxes = new ArrayList<CheckBox>();
public ArrayList<CheckBox> baseStationsCheckBoxes = new ArrayList<CheckBox>();
So according to your suggestion i should pass six parameters to the method of the interface, so as ReserveNodesFragment will have access to the six ArrayLists:
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onReserveSelected(ArrayList<CheckBox> orbitCheckBoxes, ArrayList<CheckBox> gridCheckBoxes, ArrayList<CheckBox> usrpCheckBoxes, ArrayList<CheckBox> disklessCheckBoxes,
ArrayList<CheckBox> icarusCheckBoxes, ArrayList<CheckBox> baseStationsCheckBoxes);
}
So it is elegant to pass so many parameters to the callback method?
Also, the checkNodesFragment is a nested fragment to the AvailableNodesFragment. So its parent is not an activity but a Fragment.
So the app hierarchy is like this:
NitosSchedulerActivity --> AvailableNodesFragment-->CheckAvailableNodesFragment
NitosSchedulerActivity --> ReserveNodesFragment
The CheckAvailableNodesFragment is a nested Fragment to AvailableNodesFragment.
The ReserveNodesFragment is triggered by a Button of the AvailableNodesFragment.
So, there is not a way to trigger a callback method from CheckAvailableNodesFragment to
ReserveNodesFragment for passing the ArrayLists.
So, until now the only way i found to make the ArrayLists of CheckBoxes visible to the ReserveNodesFragment is to make the ArrayLists declared globally.
However, making ArrayLists global is an elegant solution?
So, there is a button in a ListFragment. The onCLick method of the button is implemented in the MainActivity(not sure if it is a proper solution, but it is what it is). When I click the button the AlertDialog pops up and when I choose one of the dialog options it changes the dataset my fragment is working with.
The problem is when the AlertDialog disappears, my ListFragment is still displaying old data.
Is there any way to update my ListFragment from the MainActivity?
I've tried making certain ListFragment methods static so that they could be called from the main activity, but those methods use non-static fields, etc. and thus cannot be static.
You should be able to update ListFragments by calling notifyDataSetChanged() on it's adapter (assuming that your adapter derives from BaseAdapter or any of it's subclasses). The easiest way to do this would probably be set an an DialogInterface.OnDismissListener on your dialog.
myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog){
myBaseAdapter.notifyDataSetChanged();
}
});
You can either keep the reference to the Adapter or get it directly from the ListFragment depending on your implementation.
So, I declared an adapter of my ListFragment fragment as static, as well as the I declared a list from which this adapter is being filled - as static.
From the main activity I do this:
ListFragment.item.add(mChosenFilePath);
ListFragment.fileList.notifyDataSetChanged();
where:
item - is a list that contains the elements that are to be displayed
mChosenFilePath - path of file that has been added into the item as a result of the dialog
fileList - is my adapter
Set a tag, or id for the fragment. You can then call a method directly on the fragment from the Activity:
Fragment myne = findFragmentByTag( "MyFragment" );
MyFragment target = (MyFragment) myne;
target.refresh(); // 'Refresh' method to be declared by MyFragment implementation
There are three possible solutions.
Listen for the click in your fragment instead of the Activity.
Set a listener on the cancel button of your dialog, and reload the fragment as needed.
Add your fragment with a tag, get it from the manager by that tag, and call the appropriate method.