how to populate spinner options programmatically(not using xml) - android

I wish to set spinner items using program code and not using android:entries in xml layout.
But i am failing to do so.
I want to set spinner items according to a condition so cannot set it using xml statically so kindly help me to set dynamically.
here's the code:
public class Converter extends AppCompatActivity {
EditText et2;
TextView tv;
Spinner spr2,spr3;
ArrayAdapter adap2,adap3;
String []spr_2;
String []spr_3;
Button btn2;
String s1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
et2=(EditText) findViewById(R.id.et2);
tv=(TextView) findViewById(R.id.tv);
spr2=(Spinner) findViewById(R.id.spr2);
spr3=(Spinner) findViewById(R.id.spr3);
btn2=(Button) findViewById(R.id.btn2);
Bundle b=getIntent().getExtras();
et2.setText(b.getString("Value1"));
s1=b.getString("SpinnerValue");
adap2=adap3=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line);
if(s1 == "height") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "weight") {
spr_2 = spr_3 = getResources().getStringArray(R.array.weight);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "distance") {
spr_2 = spr_3 = getResources().getStringArray(R.array.distance);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "currency") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
}
}

Looks like code in if-blocks does not invoked. You shouldn't check objects equality like this. Operator '==' checks identity, not equality. Not s1 == s2, but s1.equals(s2), or even better Objects.equals(s1, s2).
Your code should look like this:
String[] items;
if ("height".equals(s))
items = getResources().getStringArray(R.array.height);
else if ("width".equals(s))
items = getResources().getStringArray(R.array.width);
....
else
items = getResources().getStringArray(R.array.width); //just for example
spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, items));

Related

how to set spinner textview I want a separate textview to show if the text in the spinner is clicked?

There are three buttons namely spinner,spinner2,spinner3 and next button there is sumbit button.
sumbit button is clicked then show should be displayed in the TEXTVIEW.
spinner:DD
Spinner:mm
spinner:yyy
I want it in calendar format
TextView textView = (TextView) findViewById(R.id.textView2);
**
String [] values =
{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String [] values2 =
{"1","2","3","4","5","6","7","8","9","10","11","12"};
String [] values3 =
{"2023"};
Spinner dd = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
dd.setAdapter(adapter);
Spinner mm = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values2);
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mm.setAdapter(adapter2);
Spinner yyyy = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values3);
adapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
yyyy.setAdapter(adapter3);
Button sumbit= (Button) findViewById(R.id.sumbit);
TextView textView = (TextView) findViewById(R.id.textView2);
**
A) using array
Spinner spinner = findViewById(R.id.spinner); //day spinner.
ArrayList<String> arrayListdays = new ArrayList<>();
arrayListdays.add("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayListdays);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
similarly for month and year,
then button click,
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tvw.setText(spinner.getSelectedItem().toString()+"-"+spinner1.getSelectedItem().toString()+"-"+spinner2.getSelectedItem().toString());
}
});
B) using spinner alternatively, try like below,
1). for spinner like date for day,month, year use below xml, in datepicker use property android:datePickerMode="spinner" like below.
<DatePicker
android:id="#+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"/>
2). on button click you need value in textview for this,
public class MainActivity extends AppCompatActivity {
DatePicker picker;
Button btnGet;
TextView tvw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvw=(TextView)findViewById(R.id.textView1);
picker=(DatePicker)findViewById(R.id.datePicker1);
btnGet=(Button)findViewById(R.id.button1);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tvw.setText("Selected Date: "+ picker.getDayOfMonth()+"/"+ (picker.getMonth() + 1)+"/"+picker.getYear());
}
});
}
}
if still have issue, please comment.

Change the string names in spinner dynamically from previous spinner chosen item

I want to change the string names in spinner dynamically android from previous spinner chosen item.
This is my Activity class:
public class SelectionActivity extends AppCompatActivity implements View.OnClickListener {
final String LOG = "Selection";
private Spinner spBranch;
private Spinner spSection;
private Spinner spSemester,spSubject;
private Button btnSend;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spBranch = (Spinner) findViewById(R.id.spBranch1);
spSection = (Spinner) findViewById(R.id.spSection1);
spSemester = (Spinner) findViewById(R.id.spSemester1);
spSubject=(Spinner) findViewById(R.id.spSubject1);
String[] items1 = new String[]{"CSE", "EEE", "EE", "ECE", "MECH", "CIVIL"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items1);
spBranch.setAdapter(adapter1);
String[] items2 = new String[]{"A", "B", "C", "D", "E", "F"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items2);
spSection.setAdapter(adapter2);
String[] items3 = new String[]{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"};
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items3);
spSemester.setAdapter(adapter3);
String[] items4 = new String[]{"Math 1", "Programming in C", "Thermodynamics", "Communication English", "Physics", "Basic Electronics"};
String[] items5 = new String[]{"Chemistry", "Data Structure", "Mechanics", "Buiseness English", "Basic Electrical Engineering l ", "Math 2"};
if(spSemester.getSelectedItem().toString().equals("1st"))
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}
btnSend = (Button) findViewById(R.id.btnSend);
}
#Override
public void onClick(View v) {
Intent in = new Intent(SelectionActivity.this, ListActivity.class);
startActivity(in);
HashMap postData = new HashMap();
postData.put("txtBranch", spBranch.getSelectedItem().toString());
postData.put("txtSection", spSection.getSelectedItem().toString());
postData.put("txtSemester", spSemester.getSelectedItem().toString());
}
}
I am getting Error at r this line spSubject.setAdapter(adapter4);
New to andorid, so is there any other way to do this
Note that all of the code you have posted will run immediately when the Activity is created. This is the whole point of the onCreate() method. Nothing will happen when the user selects a new item in any of the Spinners. If you want the Spinners to change more dynamically, you need to add appropriate event listeners. In particular, you need to implement AdapterView.OnItemSelectedListener. An example is shown here. I suggest creating separate classes which implement the listener interface rather than implementing it on your activity class.
Try this code to know if first index in spSemester spinner is selected or not
if(spSemester.getSelectedItemPosition() == 1)
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}

Dynamic Spinner not working correctly

I have a spinner2 which will have day and month, when user select day, s1 and s2 will display hours and mins respectively.
On startup of the activity, I have the daily selected.
The problem is I keep on getting toast of daily. Which suggested that it gets selecteditem endlessly. Also, I cant select values in s1 and s2, the spinner keep on refreshing among themselves. How to solve this?
public class try_spinner extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner1, spinner2, s1, s2, s3;
String app_name, room_name;
TextView app, room;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.try_spinner);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .9), (int) (height * .7));
Bundle extras = getIntent().getExtras();
final String app_string = extras.getString("APPLIANCE_NAME");
final String room_string = extras.getString("APPLIANCE_ROOM");
app = (TextView) findViewById(R.id.app_name);
app.setText(app_string);
room = (TextView) findViewById(R.id.room_name);
room.setText(room_string);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
s1 = (Spinner) findViewById(R.id.s1);
s2 = (Spinner) findViewById(R.id.s2);
s3 = (Spinner) findViewById(R.id.s3);
spinner2.setOnItemSelectedListener(this);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);
if (app_string.equals("Lighting")) {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lighting));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
} else if (app_string.equals("Shower Heater")) {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.shower_heater));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
} else {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
}
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String txt_spinner2 = String.valueOf(spinner2.getSelectedItem());
Toast.makeText(this, txt_spinner2, Toast.LENGTH_SHORT).show();
if (txt_spinner2.contentEquals("Daily")) {
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.days));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s1.setAdapter(dataAdapter);
String txt_s1 = String.valueOf(s1.getSelectedItem());
//set for daily hours usage
List<String> list3 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s2.setAdapter(dataAdapter3);
//set for daily minutes usage
List<String> list4 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s2.setAdapter(dataAdapter4);
//set for daily empty thrd space
List<String> list5 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter5.notifyDataSetChanged();
s3.setAdapter(dataAdapter5);
}
else if (txt_spinner2.contentEquals("Monthly")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
list.add("the end");
ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter6.notifyDataSetChanged();
s3.setAdapter(dataAdapter6);
}
}
First of all set adapter first to spinner and then call setOnItemSelectedListener() for it. b'z when spinner value gets initialized it even call setOnItemSelectedListener method. you can put debugger and check.
Secondly always check for which spinner or widget you are getting event, in your case for spinner you can do like this
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Spinner clickedSpinner = (Spinner) parent;
switch (clickedSpinner.getId()) {
case R.id.spinner1:
// your code for spinner1
String str = spinner1.getSelectedItem().toString();
break;
}
}
so change your code and see its working or not. hope it will help you...

How to use onclicklistener

I am trying to use an onclick listener on radio buttons to update a spinner I have. The default numbers in the spinner will be 15, 25, 35, and 45. When you select the KG radio button I want it to change to 8, 15, and 20. I have the following:
public class PlateCalc_Tab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.platecalc_tab);
RadioButton weightSettingLB = (RadioButton)findViewById(R.id.weightSettingLB);
RadioButton weightSettingKG = (RadioButton)findViewById(R.id.weightSettingKG);
weightSettingLB.setChecked(true);
weightSettingLB.setOnClickListener(updateToLB);
weightSettingKG.setOnClickListener(updateToKG);
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("15");
weightOfBarArray.add("25");
weightOfBarArray.add("35");
weightOfBarArray.add("45");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
}
};
This code currently works. When I enter the following code into my onclick listener I get an error.
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("8");
weightOfBarArray.add("15");
weightOfBarArray.add("20");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
};
I get "Cannot Resolve Constructor" # this line:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
Replace the array adapter initialization like below.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlateCalc_Tab.this, android.R.layout.simple_spinner_item, weightOfBarArray);
use
ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_spinner_item, weightOfBarArray);

android Spinner set Text give error in setText

I am working on EditText and Spinner . When I set text for EditText it were fine but when I try to set text for Spinner it give error.
this is code for set text EditText
New_Name.setText(NewName);
here New_Name is EditText
this is code for set text Spinner
New_Quantity.setText(NewQuantity);
here New_Quantity is Spinner
this is the full code.
public class UpdateFoodActivity extends Activity {
EditText Name_Search,New_Name,New_Calorie,New_Fat,New_Protein,New_Sugar,New_carbohydrates;
Spinner New_Quantity;
TextView title_text;
String SearchName,NewName,NewQuantity,NewCalorie,NewFat,NewProtein,NewSugar,Newcarbohydrates;
FoodDbHelper foodDbHelper;
SQLiteDatabase sqLiteDatabase;
Button UpdateButton;
ArrayAdapter<CharSequence> adapter;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.update_food_activity);
Name_Search = (EditText)findViewById(R.id.name_search);
New_Name = (EditText)findViewById(R.id.new_name);
New_Quantity = (Spinner)findViewById(R.id.new_quantity);
adapter = ArrayAdapter.createFromResource(this, R.array.quant, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
New_Quantity.setAdapter(adapter);
New_Quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
New_Calorie = (EditText)findViewById(R.id.new_calorie);
New_Fat = (EditText)findViewById(R.id.new_fat);
New_Protein = (EditText)findViewById(R.id.new_protein);
New_Sugar = (EditText)findViewById(R.id.new_sugar);
New_carbohydrates = (EditText)findViewById(R.id.new_vitamins);
UpdateButton = (Button)findViewById(R.id.update_button);
title_text = (TextView)findViewById(R.id.title_text);
New_Name.setVisibility(View.GONE);
New_Quantity.setVisibility(View.GONE);
New_Calorie.setVisibility(View.GONE);
New_Fat.setVisibility(View.GONE);
New_Protein.setVisibility(View.GONE);
New_Sugar.setVisibility(View.GONE);
New_carbohydrates.setVisibility(View.GONE);
UpdateButton.setVisibility(View.GONE);
title_text.setVisibility(View.GONE);
}
public void searchFood(View view){
SearchName = Name_Search.getText().toString();
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
Cursor cursor = foodDbHelper.getFood(SearchName,sqLiteDatabase);
if (cursor.moveToFirst()) {
NewQuantity = cursor.getString(0);
NewCalorie = cursor.getString(1);
NewFat = cursor.getString(2);
NewProtein = cursor.getString(3);
NewSugar = cursor.getString(4);
Newcarbohydrates = cursor.getString(5);
NewName = SearchName;
New_Name.setText(NewName);
New_Quantity.setText(NewQuantity);
New_Calorie.setText(NewCalorie);
New_Fat.setText(NewFat);
New_Protein.setText(NewProtein);
New_Sugar.setText(NewSugar);
New_carbohydrates.setText(Newcarbohydrates);
New_Name.setVisibility(View.VISIBLE);
New_Quantity.setVisibility(View.VISIBLE);
New_Calorie.setVisibility(View.VISIBLE);
New_Fat.setVisibility(View.VISIBLE);
New_Protein.setVisibility(View.VISIBLE);
New_Sugar.setVisibility(View.VISIBLE);
New_carbohydrates.setVisibility(View.VISIBLE);
UpdateButton.setVisibility(View.VISIBLE);
title_text.setVisibility(View.VISIBLE);
}
else
{
Toast.makeText(getApplicationContext(),
"No Dish Found " ,
Toast.LENGTH_SHORT).show();
}
}
public void updatebutton(View view)
{
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getWritableDatabase();
String name,quantity,calorie,fat,protein,sugar,carbohydrates;
name = New_Name.getText().toString();
quantity = New_Quantity.getSelectedItem().toString();
calorie = New_Calorie.getText().toString();
fat = New_Fat.getText().toString();
protein = New_Protein.getText().toString();
sugar = New_Sugar.getText().toString();
carbohydrates = New_carbohydrates.getText().toString();
int count = foodDbHelper.updateInformation(SearchName,name,quantity,calorie,fat,protein,sugar,carbohydrates,sqLiteDatabase);
Toast.makeText(getApplicationContext(),count+" dish updated",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,UpdateFoodActivity.class);
startActivity(intent);
}
}
In order to add entries to the spinner you need to define an Adapter i give you an example:
spinner = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.spinner_item, list);
spinner.setAdapter(dataAdapter);
Spinner likes ListView, need an adapter for rendering data. Adapter, in simple term, is a collection of items. (As you see ListView always contains list of item and also Spinner). Nevertheless, Adapter of spinner often a list of string. (because spinner just represents user choice)
In your example, You can simply create ArrayAdapter and assign again to Spinner.
private void setTextForSpinner(Spinner spinner) {
String[] items = {"Stack", "Over", "Flow"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
If apply above code to your example, you can change again to:
private void setTextForSpinner(Spinner spinner, String NewName) {
String[] items = {NewName};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
Hope this help :)

Categories

Resources