Update a spinner in Android - android

In my application I have 1 spinner option. In that I am displaying 6 values (which is array from values file). Now in that when I click its 5 items of the spinner value the selected item will show in the spinner in the activity screen.
When I select 6th item in spinner I have to start a new activity. Is it possible? Do I have to do this by position value?
Also in the new activity (after selecting 6th item) I have 5 edittext values in which user can enter a string which should replace the old value in the spinner. Is it possible to dynamically update the spinner?
My spinner code:
Spinner s2=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.group_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter1);

It's possible
Codings:
ArrayAdapter yearadp = new ArrayAdapter(this,android.R.layout.simple_spinner_item, yearlist);
yearadp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
year_spinner.setAdapter(yearadp);
index=0;
year_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
spintext=parent.getItemAtPosition(pos).toString();
if(spintext=="6")
{
intent = new Intent(ListMonthActivity.this,NewChart.class);
startActivity(intent);
}
}
}
Ya we can do with setting with
mon_spinner.setSelection(iSelectedMonth);
intent = new Intent(ListMonthActivity.this,NewChart.class);
You may or not mention while the action are certain action finished, and you can passed into another activity .

Related

Set spinner title when activity is created

I have a spinner whose elements set by an arrayList.this arraylist gets data by calling an api. I have set title at 0th position of arrayList .the problem is i want to show this title as activity is created. but this will set only when api calling is successful .it takes some time to load data. can anyone give me better idea to set title to spinner.
If data comes from api then you can add dialog on screen and hide title until api call success.. when you get response true then show titleview and add title from api. and api response is false then show error.. so user will get better view..
Thanks
Do like this
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
R.layout.spinner_item, CatagoryName);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
// put code which recognize a selected element
}
Try this way
ArrayList<String> list = new ArrayList<>();
list.add(0,"Please Select");
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, CatagoryName);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(stringArrayAdapter);
// now call the API for data
//After data received, you have to make temp list to store response
ArrayList<String> Templist = new ArrayList<>();
//Templist = Server response
//now add templist in main List
list.addAll(Templist);
// and call this
stringArrayAdapter.notifyDataSetChanged();

How to work With multiple Spinners?

I need to work with 3 Spinners, meaning that if one item from the Spinner number 1 is selected, then the spinner number 2 will become visible, and if another item from the Spinner number 1 is selected, then the Spinner number 3 will become visible, and the number 2 invisible, in order to do this, do i need to work with 3 ArrayAdapter, one per each?? or can i work with one ArrayAdapter? Any help will be very appreciated
ArrayAdapter means 'An adapter for an array of elements`. Not 'An array of adapters'. If you want to work with 3 spinners, you will have to use 3 adapters.
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner1); // invisible by default
final Spinner spinner3 = (Spinner) findViewById(R.id.spinner1); // invisible by default
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter3);
spinner1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
spinner2.setVisibility(View.VISIBLE);
} else if(position == 1) {
spinner3.setVisibility(View.VISIBLE);
}
}
}); //like this for all the three
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view.
If you want to show same data to all of the three Spinners, Then you can use one adapter for the spinners else you need to create separate adapter for each.

Spinner, not set value if i dont click(android)

I have a spinner:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int childposition, long id) {
textView.setText(spinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
textView.setText("");
}
});
Above you'll see that textView - is my text object. I'm displaying a text item spinner in the textView when I click it. If I dont click the spinner then my textView must be textView.setText("");
But the spinner is always set text in my textView, even if I do not choose spinner.
Question
How can I accomplish this?:
If I dont choose item spinner, textView is empty: textView.setText("");
If I do choose the item spinner, textView gets: textView.setText(spinner.getSelectedItem().toString());
String item = parent.getItemAtPosition(childposition).toString(); //Get selected item
if(item.equals("spinner")){ // Check if it equals spinner
textView.setText(item); // Set text to item
}else{
textView.setText(""); // If it doesn't equal spinner set text to ""
}
If I understood the question right, putting this instead of textView.setText(spinner.getSelectedItem().toString()); and deleting content of onNothingSelected should do the trick.
UPDATE
I finally understood what you mean. To do this create your spinner like this and add "" as first choice in your string-array resource :
String[] newArray = getResources().getStringArray(R.array.yourArray);
List<String> myResArrayList = Arrays.asList(newArray);
ArrayList<String> spinnerItems = new ArrayList<String>(myResArrayList);
//Making adapter with ArrayList instead of String[] allows us to add/remove items later in the code
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, spinnerItems);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinnerItems.remove(0);
adapter.notifyDataSetChanged(); // Here we remove the first choice which is "" so the user won't be able to select empty.

Spinner does not get selected after setting adapter

I have two spinner in dialog. Second one is dependent on first one. I want to bind it and then select it in edit mode. All works fine but second spinner does not get selected. However it get selected when I open my dialog next time.
Here is a part of my code.
ArrayAdapter<String> myAdap1 = (ArrayAdapter<String>) spnForeignKeyTable
.getAdapter();
int spinnerPosition1 = myAdap1.getPosition(objcolumn_schema
.getForeignKeyTable());
spnForeignKeyTable.setSelection(spinnerPosition1);
// Bind Column Spinner.Second spinner
dblist = DBAdapter.getColumns(pf.getString("dbid", ""),String.valueOf(objcolumn_schema.getForeignKeyTableID()));
ArrayAdapter<String> adpf = new ArrayAdapter<String>(
column.this, android.R.layout.simple_spinner_item,
dblist);
adpf.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnForeignKeyColumn.setAdapter(adpf);
int spinnerPosition2 = adpf.getPosition(objcolumn_schema.getForeignKey());
spnForeignKeyColumn.setSelection(spinnerPosition2);
for changing selected item in second spinner when first spinner selection change you will need to set setOnItemSelectedListener for first spinner as:
spinnerPosition1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v,
int position, long id)
{
// change second Spinner selection here
}
public void onNothingSelected(AdapterView<?> arg0)
{
//
}
});

how to change data already data in spinner

I have four Spinner ,First spinner display data when user select on spinner item data then other spinner data is displayed .First time,i have loaded all data in spinner when user select first spinner then data should be changed to refresh to second Spinner
How data is changed in second spinner
First you would need to get a reference of your spinner like -
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myArray, android.R.layout.mySpinnerItem);
adapter.setDropDownViewResource(android.R.layout.myDropdownItem);
spinner.setAdapter(adapter);
To change the values you would do -
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//change content
}
}
public void onNothingSelected(AdapterView parent) {
//do nothing
}
}
Set a listener on the setOnItemSelectedListener of your first Spinner, with the appropriate code to populate your second Spinner. This way when you change the value of the first Spinner, the second Spinner will be updated.

Categories

Resources