I have an activity contains expandable list in its layout, the expandable list fle extends BaseExpandableListAdapter. and what i want to so is, when an item
in the list is clicked a new activity should starts containg the clicked item data.
my question is, what is the recommended way to launching the new activity when an item is clicked, should i launch the new activity from the listener in the class
that extends BaseExpandableListAdapter or i should handle that from the activity itself through an interface passes a flag for an example from the listenr to the
activity?
Well I usually do this by using an interface between the class and the owner activity. I would assume though that there isnt much a difference between the methods. It just seems cleaner to use an interface as oppose to passing a reference to the activity.
Responsibility of your BaseExpandableListAdapter should not be handling events as its a data adapter. you should route your events to activity and handle them over there as you said in your second option.
you can use the intent for the launching the activity from the non-activity class.
Related
I'm needing a way to pass data from multiple fragments to an activity. This data can only be sent to the activity when I change the fragment tab.
To pass the data I am using the interface method, but I do not know in which function of the class fragment I can put the method that will take the text of the EditText and send to the activity through the interface.
Is it important to pass the data only when the fragment changes? If not, you could add a callback to the edit text that calls the interface method your activity implements. This answer has some good examples on how to do that.
I got an RecyclerView in my Main Activity, where the Items got a Like Button. In my RecyclerViewAdapter Class I got the OnClickListener which receives the ID of the Item.
What I want to do is:
Pass the ID of the Item to the MainActivity. I tried it with Intents in the OnClickMethod of the RecycerViewAdapter Class, but if I would use it like this I would need something like a Listener for the Intent in the MainActivity. The other Idea was to set an OnClickListener for the Button in the MainActivity. But I cant set an OnClickListener for an Item of the RecyclerView in the Main Acitivity.
Does someone know how I can solve my Problem?
There are two straightforward ways I can think of:
Use a Handler
Define an interface and pass the instance to the Adapter. Then call the appropriate method when the click is performed.
BTW, why do you want to pass the value back to the Activity? Can't you perform the work withing the adapter?
I need to pass an object between 2 activities that have no connection between them (meaning, neither of them calls the other).
My Main_activity extends TabActivity. I have 2 tabs : CurrencyList (extends ListActivity implements OnItemSelectedListener) and CurrencyCalculator extends Activity.
I also have class currencyData that saves data about different currencies.
In the CurrencyList activity I created a new currencyData object and initiate it with data.
How can I pass it also to the CurrencyCalculator activity?
2 quick ways:
1. use an static method in your activity to retrieve current ticket id
2. Design and implement an interface and register the fragments as listeners from the activity
Static Method is preferable for large data.
If there is absolutely no connection between them, then one method of accessing data in different Activity classes would be to declare the data members as static class members. Keep in mind that static objects from Activities persist even after you destroy the activity and Android keeps this around for some time even after the you leave the application.
These might not be the easiest ways (just some alternate approaches to the two answers given). You can use a Handler or a BroadcastReceiver to pass the data to the other activity through an intent.
Note
that your object would have to implement either Serializable or
Parcelable if you want to pass it through an intent. I have used
Serializable before and as long as your object does not have any
nested custom objects, you actually have to do no additional work.
Also the assumption is that the receiving activity is alive and is
able to receive the broadcast and/or message.
Another approach would be to write the object to a file (again it would need to be serializable) and read it back in the other activity.
I have a TabActivity with two Activities. One ListView for the inbox and another for the outbox.
When I click on an item(a thread) on the ListActivityThreads, I get the messages of that thread, in another ListActivityThreadDetail. When I am on that activity and answer a message I want to modify that single thread and passing it back to the ListActivityThreads.
Following the normal way to set results back to the previous activity I do this:
I use startActivityForResult in ListActivityThreads and in ListActivityThreadDetail I set a result for the intent. But I cant get any data on onActivityResult in ListActivityThreads.
I have tried different ways to start activities and passing values on a TabActivity. But no luck so far...
I cant use a BroadcastReceiver as my class already extends ListActivity.
Also I have been trying this example using TabgroupActivity and startChildActivity. But cant receive anything using the method onActivityResult, neither implementing finishFromChildActivity on my parent acitivty.
I am really stuck with this, any help would be really appreciate!
There is one alternative for this problem is that you can take one static variable in parent activity and you can access that static variable in child activity and in child activity you can modify that variable and so you will get modified value in parent activity
I have an activity which shows a Spinner (selection of category of items), a listview to show the orders and another listview -articles- which dynamically gets filled with buttons according to the selected category (spinner).
When an article button gets clicked, I want to add the article to the orderAdaptor.
How can I get a reference to the list adapter when I'm in the View.OnClickListener?
You'll need to post your code to get decent help with this, but it sounds like you are implementing the View.OnClickListener as an anonymous inner class inside of your Activity.
You can generally always get to the adapter by fully qualifying the reference:
MyActivity.this.mListView.getAdapter();
where MyActivity is a presumed class name that extends Activity and holds a member ListView named mListView.
You can pass it to your onClickListener's constructor and keep it in a member variable on the listener, or you can use setTag on the view to stash a reference to the adapter in the button and then retrieve it in the onClick listener with getTag. Or you could make the view listener a non-static inner class on your activity and access the adapter the same way everything else in your activity does (although I'd usually prefer being explicit and keeping the listener static / in a different class).