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?
Related
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.
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
My problem is the following : at the moment I have a spinner called "projects" that I am populating in my code, but for some reasons I need to add it a title so I just do it using my adapter :
adapter.add("Choose a project");
Now I would like to remove it (via adapter.remove, this is not a problem) when the spinner in my activity is clicked (when it's opening), but the only method i am able to find is onItemSelected, which is not what I'm looking for.
Is there such thing as a onOpenListener or onClickListener for Spinner to actually execute some code when the spinner is opened, and not only when an item is selected ??
Thank you
This is probably because you are trying to call it inside the listener which is probably an anonymous class. Assuming your activity's class name is MyActivity use MyActivity.this.someactivitymethod where someactivitymethod is the method that you want to execute.
Spinner inherits from AbsSpinner which in turn inherits from AdapterView, which means it inherits the setOnClickListener method from AdapterView and this can be used to perform an action on clicking the spinner.
http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener(android.view.View.OnClickListener)
EDIT: While this is true, it throws a runtime exception. Sorry.
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).
I have ListActivity with CustomArrayAdapter. My extension of ArrayAdapter allows me to do add/remove operations with the rows. It works fine...inside ListActivity. But then I need to add/edit rows in my list from different Activity and troubles begin. How can I do that? All I need inside my EditActivity is CustomArrayAdapter object. I made it static and tried. It worked, but is it a right thing?
Thanks for the answers, masters!
What comes in my mind:
One way could be to make your objects in the ArrayList serializable you could pass then the actual list to the next activity via intent and use it there for the adapter.
Other way would be: if you know the ID of the element which you want to delete, pass it as intent, delete it from the ArrayList and set adapter.notifyDataSetChanged();
I don't think to have a static ArrayAdapter class is the right way. This means you affect both activities at the same time. Because none of them have an own object.
Hope this helps