How to pass value from base adapter to Activity in Android - android

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.

Related

Refresh Android Activity from outside mainActivity (Adapter class)

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();

Activity list view display different data (String array) on click of listitem

I have an activity which contains listview, when click on any list item, i want to display same activity with listview with different data and so on.
When i click on back button i need to display same activity listview with old data.
Is it possible? or is there is any other way to achieve this. I dont want to create any new activities or fragments for this?
Thanks
KrishIndia
You can write the function such that each time you click an item, it generates the list, assigns the data to your adapter and attaches it to your list view of choice.
If fetching the data won't be expensive for you, you could use only one adapter (array adapter or list adapter or simple adapter should be fine) and just re-assign it each time. (Ex. If you were listing files in a directory, inside your onClick() function you would have some function declaration like list_items(directoryName) that you would call each time. That function would declare and set an adapter for your data.)
If you are concerned about the expense of re-fetching the data, just store multiple adapters (outside your onClick() function) and set them to your list view as needed.
on click of list item just set new adapter or new data and call notifyDataSetChanged()
and for backpress override onBackPressed() and handle accordingly
Maintain separate adapter for each new data. When you want to return back just pass the relevent adapter object to that list view

How to reuse ListFragement in given setup on some event

This what I have
XListActivity.class
It inflates LinearLayout and creates one Fragment, YListFragment
YListFragment.class
Inflates LsitView from xml and setup a adapter which extends base adapter
Now on some event (e.g. onClick), I want to reuse same fragment and ListView whith different set of data.
If I handle OnClick() event in XListActivity then I don't have reference of ListView and Adapter created in yListFragment. I need them to empty adapter. I want to avoid static references.
How can I achieve this?
You can get your fragment by tag or id using the FragmentManager.
YListFragment fragment = (YListFragment)getFragmentManager().findFragmentByTag("ylistfragment");
fragment.somePublicMethodInYListFragment();
Activity -> Fragment and Fragment -> Activity communication guidelines are outlined here

Open options menu from custom Adapter Android

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.

start a new activity from button click within listview (using simplecursoradapter)

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.

Categories

Resources