spinner dependent on another spinner in android - android

http://developer.android.com/resources/tutorials/views/hello-spinner.html
I use this link as the base..
I have 3 spinners, first spinner for country , second for states and third for cities.
I had already seen the links on the stackoverflow , but it is not that much proper.
my code is as follows
public class SpinnerActivity extends Activity
{
String state="";
ArrayAdapter adapter1,adapter2;
Spinner spinner1,spinner2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter1 = ArrayAdapter.createFromResource(
this, R.array.states_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
if(state.equalsIgnoreCase("Gujarat"))
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
if(state == "Rajasthan")
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
state=parent.getItemAtPosition(pos).toString();
Toast.makeText(parent.getContext(), "The state is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
}
the bug I found is , I have created all he thing in the onCreate() method..
but I do not find the alternate thing that can I do ...
I don't know why this not work ..
please help if possible
thanks...

In onSelectItem fill second spinner and so on.

Related

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 Spinners, changing Adapters

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.

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.

How do I change which String Array my Spinner is using?

I'm trying to change the contents of my two Spinners. So I've got my Spinners created:
Spinner spinner01 = (Spinner) findViewById(R.id.Spinner01);
Spinner spinner02 = (Spinner) findViewById(R.id.Spinner02);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner01.setAdapter(adapter1);
spinner02.setAdapter(adapter2);
spinner01.setOnItemSelectedListener(new MyOnItemSelectedListener1());
spinner02.setOnItemSelectedListener(new MyOnItemSelectedListener2());
Now from a different method, I want to be able to do something like this:
adapter1 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
adapter2 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
Is there anyway to get the adapters to read from a different String Array? Or should I be replacing the values in the String Array with new ones?
Create separate adapters for each array you wish to assign to spinner2.
Declare them as final.
Place the adapter declarations before you set spinner1 ItemSelected Listener.
In spinner1 ItemSelected listener, set the appropriate adapter as needed.
In the code below I have two spinners.
spinner1 has 4 entries containing sensor types, when one of the sensor types is selected spinner2 is then populated with the various models for that sensor type.
public class MainActivity extends Activity {
void showToast(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner s1 = (Spinner) findViewById(R.id.spinner_sensor_type);
final Spinner s2 = (Spinner) findViewById(R.id.spinner_sensor_model);
final ArrayAdapter<CharSequence> sensor_adapter = ArrayAdapter.createFromResource(
this, R.array.sensors,android.R.layout.simple_spinner_item);
sensor_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<CharSequence> pa_adapter = ArrayAdapter.createFromResource(
this, R.array.pa_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ca_adapter = ArrayAdapter.createFromResource(
this, R.array.ca_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_fe_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_fe_models,android.R.layout.simple_spinner_item);
pa_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ca_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_fe_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(sensor_adapter);
s2.setAdapter(pa_adapter); // set default array values in spinner 2
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Type: position=" + position + " id=" + id);
switch (position) {
case 0:
s2.setAdapter(pa_adapter);
break;
case 1:
s2.setAdapter(ca_adapter);
break;
case 2:
s2.setAdapter(ni_adapter);
break;
case 3:
s2.setAdapter(ni_fe_adapter);
default: // Do this...
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Type: unselected");
}
});
s2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Model: position=" + position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Model: unselected");
}
});
}
}
You can use the ArrayAdapter's getters/setters:
E.g:
adapter1.clear();
adapter1.addAll(theNewListOfValues);
adapter1.notifyDatasetChanged();
You should be able to just call setAdapter on your spinners with the new adapters. I would suggest storing the adapters as fields in your activity after you create them so you can easily re-use them if you need to swap between the adapters again.

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:
public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)this.findViewById(R.id.bt1);
et = (EditText)this.findViewById(R.id.et);
spinner = (Spinner)this.findViewById(R.id.spr);
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);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String temp = et.getText().toString();
adapter.add(temp);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
}
thanks! ...still waitting
When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.
Try something like this:
List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
R.array.planets_array, planets);
//now you can call adapter.add()
You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.
Javi is right except don't reference an array for the second parameter.
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.
you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator

Categories

Resources