Android Spinners, changing Adapters - android

Alright, so I'm new to Android programming, so far my experience has been quite interesting and challenging. But I fear I have now encountered the first problem I'm not able to overcome on my own.
Simply put, all I want to do is have 2 Spinners:
1 for country selection
1 for province/state selection
What I want to accomplish is that when the user selects his/her country the province/state Spinner is updated with the correct adapter. Currently I'm only using 2 country for testing purposes.
When I launch the Activity, I get an exception and my app crashes.
Here's my code, any pointers would be appreciated !
public class ManageAccountActivity extends Activity {
final ArrayAdapter<CharSequence> adapterSexe = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.sex_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterProvince = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.province_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterStates = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.state_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.country_array_fr, android.R.layout.simple_spinner_item);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
Spinner spinSexe = (Spinner) findViewById(R.id.spin_sex);
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Spinner spinCountry = (Spinner) findViewById(R.id.spin_country);
adapterSexe.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterStates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinSexe.setAdapter(adapterSexe);
spinProvince.setAdapter(adapterProvince);
spinCountry.setAdapter(adapterCountry);
spinCountry.setOnItemSelectedListener(new CountryOnItemSelectedListener());
}
public class CountryOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
setContentView(R.layout.account_management);
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Here's the LogCat message I'm getting.
01-10 20:41:01.024: E/AndroidRuntime(1275):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{gggolf.android.minutegolf/gggolf.android.minutegolf.ManageAccountActivity}:
java.lang.NullPointerException

You cannot instantiate your ArrayAdapter directly on class attribut, because createFromResource() use Context, and it's not exists at this time, do it in onCreate() methode instead.
In addition, you get spin province wrong in your listener, you can't call findViewById on view local variable, because it's not your layout, but an inflate of android.R.layout.simple_spinner_item
The good way:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}

Try having your Activity implement AdapterView.OnItemSelectedListener itself. Notice I've moved the Spinners and left out some of your code and replaced it with comments - make sure you include it where necessary...
public class ManageAccountActivity extends Activity
implements AdapterView.OnItemSelectedListener {
// Your ArrayAdapters as before
Spinner spinSexe = null;
Spinner spinProvince = null;
Spinner spinCountry = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
spinSexe = (Spinner) findViewById(R.id.spin_sex);
spinProvince = (Spinner) findViewById(R.id.spin_province);
spinCountry = (Spinner) findViewById(R.id.spin_country);
// Call setDropDownViewResource on your ArrayAdapters
// Call setAdapter on your Spinners
spinCountry.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
}
}

Why do you have setContentView(R.layout.account_management); in your onItemSelected() method? That should not be necessary.
Furthermore, you should instantiate your adapters in the onCreate() method of your activity, and pass your actual activity instance as the context.
And the code for retrieving the Spinner object in the select listener should be changed from
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
into
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Calling findViewById() on the local view object in your select listener will return NULL because the view does not contain the Spinner.

Related

Order of OnItemSelectedListener Calls After OnCreate

I have two spinners in an Activity where the second Spinner's selection set is based on what the user picked for the first Spinner. I use a private class variable in the Activity which is set in the top Spinner's OnItemSelectedListener and then referenced in the bottom Spinner's OnItemSelectedListener to obtain the correct selection set.
This almost always works, but sometimes (mainly when app was run, not exited, and then started again by a user click some long time later) I get a null pointer exception in the second Spinner's OnItemSelectedListener due to this local variable not being set. This indicates to me that after the OnCreate that the second Spinner's OnItemSelectedListener was called before the first Spinner's.
Is there any method to force a certain order in the listeners being fired or is there a better design approach to handle this second Spinner's dependency on the first Spinner?
Example code:
package com.crashtestdummylimited.navydecoder;
public class Test extends Activity {
// Variable that at times is still null
private ReferenceData referenceData;
private void setupSpinnerFromArray (int spinnerId, String stringArray[], OnItemSelectedListener listener) {
Spinner spinner = (Spinner) findViewById(spinnerId);
ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence>(
this, android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(listener);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
// Setup Top (main) Spinner
Spinner spinner1 = (Spinner) findViewById(R.id.mainDecodeSpinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.level0_list_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MainDecoderItemSelectedListener());
// Setup Bottom (dependent) Spinner
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, R.array.level1_list_array, new SecondaryDecoderItemSelectedListener());
}
public class MainDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String selectedString = parent.getItemAtPosition(pos).toString();
if (selectedString.equals("AAA")){
// Problem variable is set
referenceData = new RatingCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
else if (selectedString.equals("BBB")){
// Problem variable is set
referenceData = new IMSCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
// TODO: Improve what occurs if no match which should not occur
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
public class SecondaryDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String key = parent.getItemAtPosition(pos).toString();
// **** referenceData being null at this point has caused crashed ****
String value = referenceData.getValue(key);
// ... Update text on activity screen ...
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
}
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
if(pos == 0)
{ //when it loads on onCreate() then pos is always 0
//donothing
}
else
{ //If user manually select item
//do what you need to do on manual user selection
}
}

chanding contents of spinners run-time (android), tried and failed

I recently posted a question on how to change the values of a spinner during program execution and was told to change the array used to make the adaptor and call notifyDataSetChanged();
I tried that but my spinner is not getting updated even though my array is. I attach the code below
public void onCreate(Bundle savedInstanceState)
{
res=getResources();
Boolean a;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// First spinner whose value determines the value of the second spinner
spinner = (Spinner) findViewById(R.id.spinner1);
// ArrayAdaptor of first spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(4);// spinner 2 is not visible initially
// ArrayAdaptor of first spinner
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_drop);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Context context = getApplicationContext();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos!=0)
{
//copying string array of second spinner from strings.xml to the current string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0] , Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.refreshDrawableState();
spinner2.setVisibility(0);//this command works and the spinner is visible, but it is empty
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
Any idea what I am doing wrong any ideas will help.
PS: I have tried removing the entire invisible, visible thing, doesnt help
Thanks in advance
Modify your onItemSelected method as follows, note the new addition I added below.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos!=0) {
//copying string array of second spinner from strings.xml to the current
//string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0],
Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2); // <--- New Addition
// spinner2.refreshDrawableState();
spinner2.setVisibility(0); //this command works and the spinner is visible,
//but it is empty
}
}

Android: Changing a textview with in a different Activity

I am trying to change textview properties of a textview that looks like this:
In a seperate Activity that looks like this:
I tried to do this with bundles but I can't get it to work.
This is how my BookActivity looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
//this is where the size property comes in
Integer size = getIntent().getExtras().getInt("SGRkey");
TextView test2 = (TextView) findViewById(R.id.booktext);
test2.setTextSize(size);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] theporn = getResources().getStringArray(R.array.allkapitel);
TextView text = (TextView) findViewById(R.id.booktext);
text.setText(theporn[pos]);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
(i pick the chapter string in the spinner and that works just fine.)
And this is how my SettingsActivity looks like:
public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SGRspinner.setAdapter(SGRadapter);
}
public class SGROnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Intent answer = new Intent();
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
int SGRint = Integer.parseInt(SGRstring[pos]);
Bundle size = new Bundle();
size.putInt("SGRkey", SGRint);
Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
nextActivity.putExtras(size);
com.asm.reader.SettingsActivity.this.startActivity(nextActivity);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I get an error when I try this. All activities are declared in the manifest. I really don't know how to go on. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
Make your textview static. That is, declare it as a public static class variable. Then you can call it directly from the other activity like this: firstActivity.myTextView.setText("foo");

Android onItemSelected - NullPointer

following code is throwing a NullPointerException:
public class test extends Activity implements OnItemSelectedListener {
private TextView explanation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.psqlpicker);
explanation = (TextView) findViewById(R.id.picker_explanation_text);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.picker_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new test());
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
explanation = (TextView) findViewById(R.id.picker_explanation_text);
}
}
It is thrown because of explanation = (TextView) findViewById(R.id.picker_explanation_text); in the onItemSelected(...) method, but I have no idea why. It is, after all, working in the onCreate(...) method.
Instead of
spinner.setOnItemSelectedListener(new test());
use
spinner.setOnItemSelectedListener(this);
You want to use your real activity as the target; you are creating a new object that is never initialized with a context so when it is called it crashes.

Using Spinner selection as a value in Android

I am trying to get a Spinner to work in Android. It displays fine and I can select any one of the options in the list. But how do I transfer that to a string?
I would have thought in the code below that 'selected' would hold the selected string, but I get an 'Illegal modifier for the local class YourItemSelectedListener; only abstract or final is permitted' error on the 'YourItemSelectedListener'.
What am I doing wrong?
Many thanks for any help.
Spinner spinnerFPS = (Spinner) findViewById(R.id.sp_FPS);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.framesps, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFPS.setAdapter(adapter);
spinnerFPS.setOnItemSelectedListener(new YourItemSelectedListener());
public class YourItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, items, android.R.layout.simple_spinner_item);
You will have to add the CurrentActivityName.this. This will fix the issue. You just can't pass the argument context as this. You will have to put ActivityName.this.
Since you are using an array resource for spinner create a resource handle
with local array declaration with getResources().getStringArray(R.array.framesps);
and then use that handle to access the selected item using position variable:
items[pos]
Heres a code edit:
Spinner spinnerFPS = (Spinner) findViewById(R.id.sp_FPS);
String[] items=getResources().getStringArray(R.array.framesps);//handle to your arrays
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFPS.setAdapter(adapter);
spinnerFPS.setOnItemSelectedListener(new YourItemSelectedListener());
public class YourItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected =items[pos]; // use handler to access select item
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}

Categories

Resources