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.
Related
I have some questions about ListView. The Post that I've searched doesn't satisfy me.
If I have five list items and call notifyDataSetChanged() method in
customAdapter, How many times does getView method called?
I have a checkbox on each list and it must be shown only when the delete button is clicked. The Delete button is created on the Activity Class, and the checkbox is created in the Adapter Class (I mean findViewById). Then how can I handle this?
Currently my code changes flag value on Activity Class and call
notifyDataSetChanged() method on Adapter Class.
How can I handle UI without call notifyDataSetChanged() method?
(I've implemented in this way...)
Sorry about not posting my code.
Get view calls every time when item appears at the screen, so in your case it is 5 times.
You can do something like that:
2.1 Create a public method in your adapter, for example:
public void setIsDeleteModeEnabled(boolean isEnabled) {
//Logic here
}
2.2 In your Activity, when Button is clicked call adapter.setDeleteModeEnabled(true);
3 You should call notifyDataSetChanged() only when dataset is changed. For handling UI events you should:
In list item: you should set onClickListeners in getView();
In Activity : onCreate() method in adapter and call it in Activity's onClicks
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'm contributing to my first large project and am struggling with a design decision. I have two fragments that populate a ListView and set a custom ArrayAdapter. Let's say the list items are emails, and the two Fragments are MessageListFragment and SearchMessagesFragment. The ArrayAdapter uses a ViewHolder pattern to inflate each message and attach an onClickListener to the "star" icon.
Now, regardless of which fragment I am in, I want the onClickListener to do the same basic thing: change the status of the email on the server to "starred," and determine if the email needs its position moved in the list (i.e. left alone if there are no filters, moved the to top/bottom if the emails are sorted by starred messages, or removed from the current list if the filter is only non-starred messages).
How can I create this behavior in a reusable manner so that I do not have to repeat this code in both fragments?
If I understood you need to perform an action triggered by one of the Fragments or both or further ones you should create an Interface ie:
interface MyCustomAction {
void triggerAction(boolean isTriggered);
}
Assign it to the Fragments and implementing it in the Activity responsible of Fragments
Declare Action in the Fragment:
private MyCustomAction myCustomAction;
Expose method to be assigned by Activity in the Fragment:
public void assignAction(MyCustomAction myCustomAction) {
this.myCustomAction = myCustomAction;
}
In the Fragment, when an onClickListner() is called, do that:
myCustomAction.triggerAction(true); //or False or what you want...
...and in the Activity:
myFragment.assignAction(new MyCustomAction() {
#Override
public void triggerAction(boolean isTriggered) {
//Doing something can be triggered by any Fragments using the same Interface
});
One way would be to create a separate class, which implements View.OnClickListener , and then pass an instance of that class to the setOnClickListener(...) method.
Im not entirely sure I fully understand you, are you saying that you have two onClickListeners and you want them to do the same thing without repeated code? If so, you can simply put the repetitious code in a subroutine and call it from both listeners. However if you mean that you have one onClickListener and you want it to listen for two different actions, then you can change those actions so that they act on the same listener. I am not sure what language you are using so I could not tell you how to do this but it should be fairly simple.
I've created a Master-Detail application from the given Android templates. I need to create a method to delete an item on longclick, but all the tutorials I've found on the subject involve
getListView().setOnItemLongClickListener...
// and so on
My problem is, I don't know where the ListView is! When I run the program with no modifications, just the base template Android creates, it runs fine. When I add "getListView" to the onCreate method, the program crashes.
There's obviously a ListView, because I see a list, but I don't know where in the default template it's buried. It's not listed in any xml or java files, so I don't know how to access it.
This may be a stupid question, but I've been at this for hours, and I would really appreciate any advice.
If you've created from Master-Detail template, you have a Fragment generated that extends ListFragment. Trying to call getListView() from onCreate() method in the Fragment will result in the following exception:
java.lang.IllegalStateException: Content view not yet created
You can't call this method before the view of the Fragment has been created (as the exception states). You can use it in the onViewCreated() method (or any method called after onViewCreated() was called). Hope this helps.
getListView is a method from ListActivity. If you are not extending that, you won't see it
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).