how to change background image with spinner
I'm new to android programming so I'm doing simple programs. Although simple, I am having a lot of troubles figuring it out. I have an开发者_JAVA百科 app that I am creating and i have created a spinner. I want the background image to change corresponding to the spinner. For example, if someone selects the option "forest" in the spinner, i want to change the background image to a picture of a forest. I don't know how to do this part and I have searched around for a solution but i couldn't find one.
Thanks in advance! Matt
P.S - I'm trying to do this for Android 2.2
Im not gonna write the full thing for you because quite frankly I think you're being a bit lazy there. You're basically asking us to write the entire application for you. However here is a basic idea of what you need to do (if someone can be bothered they can implement the code).
First add items to the spinner control. Here is the "hello world" for the spinner control: http://developer.android.com/resources/tutorials/views/hello-spinner.html
Once you have done that you wire up one of the spinner events such as
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Here is a full description of how Listeners work in Android: http://developer.android.com/guide/topics/ui/ui-events.html
And inside the listener you would do an switch statement along the lines of
switch (arg0.getSelectedItemPosition()) {
case 0:
//do action
break;
case 1:
//do another action
break;
}
精彩评论