I am using dynamic and static spinner.
This is my code, how can I implement a HashMap to show what the user selects (values)?
public class DatosAutoActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner sp_marca, sp_modelo, sp_colores;
private TextView tvMarca, tvModelo, tvColor, tvPlacas;
private Button btn_enviarDatos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_auto);
sp_marca = (Spinner)findViewById(R.id.sp_marca);
sp_modelo = (Spinner)findViewById(R.id.sp_modelo);
sp_colores = (Spinner)findViewById(R.id.sp_colores);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.array_marcas,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_marca.setAdapter(adapter);
sp_marca.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapterColor = ArrayAdapter.createFromResource(
this,
R.array.array_colores,
android.R.layout.simple_spinner_item);
adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_colores.setAdapter(adapterColor);
sp_colores.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
int[] marcas = {R.array.array_chevrolet, R.array.array_ford, R.array.array_honda,
R.array.array_nissan,R.array.array_renault, R.array.array_toyota,
R.array.array_volkswagen};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
marcas[position],
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_modelo.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
protected void onResume() {
super.onResume();
btn_enviarDatos = (Button)findViewById(R.id.btn_enviarDatos);
btn_enviarDatos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent siguiente = new Intent(DatosAutoActivity.this, VerificacionDatosActivity.class);
startActivity(siguiente);
}
else {
Toast.makeText(getApplicationContext(), "Debes llenar todos los campos", Toast.LENGTH_SHORT).show();
}
});
}
}
Using Hashmap hope this will help you.
Value for spinner
String[] spinnerArray = new String[array.size()];
HashMap<Integer,String> spinnerMap = new HashMap<Integer, String>();
for (int i = 0; i < array.size(); i++)
{
spinnerMap.put(i,array.get(i));
spinnerArray[i] = array.get(i);
}
Set value to spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Get value from spinner
String name = spinner.getSelectedItem().toString();
String id = spinnerMap.get(spinner.getSelectedItemPosition());
Related
I have 3 spinners and depend on each other like that;
Category1
Category2
Category3
When user select Category1, 2.spinner should show that;
Category1-1
Category1-2
Category1-3
And user select Category1-2, 3.spinner should show that;
Category1-1-1
Category1-1-2
Category1-1-3
I implement this way, but it is too long
so i want to any suggestion for more efficient code
public class BookActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner sp1,sp2,sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
List<String> list = new ArrayList<String>();
list.add("Category1");
list.add("Category2");
list.add("Category3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
sp1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
fillSpinnerList(parent.getItemAtPosition(position).toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void fillSpinnerList(String selectedSpinnerName){
if(selectedSpinnerName =="Category1" ){
List<String> list = new ArrayList<String>();
list.add("Category1-1");
list.add("Category1-2");
list.add("Category1-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category2" ){
List<String> list = new ArrayList<String>();
list.add("Category2-1");
list.add("Category2-2");
list.add("Category2-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category3" ){
List<String> list = new ArrayList<String>();
list.add("Category3-1");
list.add("Category3-2");
list.add("Category3-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-1" ){
List<String> list = new ArrayList<String>();
list.add("Category1-1-1");
list.add("Category1-1-2");
list.add("Category1-1-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-2" ){
List<String> list = new ArrayList<String>();
list.add("Category1-2-1");
list.add("Category1-2-2");
list.add("Category1-2-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-3" ){
List<String> list = new ArrayList<String>();
list.add("Category1-3-1");
list.add("Category1-3-2");
list.add("Category1-3-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
}
}
You can refactor it like that. It's still long as a part of it can be generalised but it's more readable.
BookActivity
public class BookActivity extends AppCompatActivity {
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
initSpinner1();
}
private void initSpinner1() {
final List<String> list = new ArrayList<>();
list.add("Category1");
list.add("Category2");
list.add("Category3");
initSpinner(sp1, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner2(itemName);
}
});
}
private void initSpinner2(String selectedCategory) {
final List<String> list = new ArrayList<>();
switch (selectedCategory) {
case "Category1":
list.add("Category1-1");
list.add("Category1-2");
list.add("Category1-3");
break;
case "Category2":
list.add("Category2-1");
list.add("Category2-2");
list.add("Category2-3");
break;
case "Category3":
list.add("Category3-1");
list.add("Category3-2");
list.add("Category3-3");
break;
default:
break;
}
initSpinner(sp2, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner3(itemName);
}
});
}
public void initSpinner3(String selectedCategory) {
final List<String> list = new ArrayList<>();
switch (selectedCategory) {
case "Category1-1":
list.add("Category1-1-1");
list.add("Category1-1-2");
list.add("Category1-1-3");
break;
case "Category1-2":
list.add("Category1-2-1");
list.add("Category1-2-2");
list.add("Category1-2-3");
break;
case "Category1-3":
list.add("Category1-3-1");
list.add("Category1-3-2");
list.add("Category1-3-3");
break;
default:
break;
}
initSpinner(sp3, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
// TODO
}
});
}
private void initSpinner(Spinner spinner, List<String> list, SpinnerListener.Callback callback) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final AdapterView.OnItemSelectedListener listener = new SpinnerListener(this, callback);
spinner.setOnItemSelectedListener(listener);
}
}
The implementation of the OnItemSelectedListener
public class SpinnerListener implements AdapterView.OnItemSelectedListener {
private final Context mContext;
private final Callback mCallback;
public SpinnerListener(Context context, Callback callback) {
mContext = context;
mCallback = callback;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String itemName = parent.getItemAtPosition(position).toString();
Toast.makeText(mContext, "YOUR SELECTION IS : " + itemName, Toast.LENGTH_SHORT).show();
mCallback.onResult(itemName);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing
}
public interface Callback {
void onResult(String itemName);
}
}
The code can be shorten a little bit if you use lambda function (Java 8). For instance:
initSpinner(sp2, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner3(itemName);
}
});
can be written:
initSpinner(sp2, list, itemName -> initSpinner3(itemName));
or even shorter:
initSpinner(sp2, list, this::initSpinner3);
EDIT
Or you can do something like that by using String arrays instead of lists. You can export the methods to init the array into a separated file so it makes the code in the Activity lighter:
public class BookActivity extends AppCompatActivity {
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
initSpinner1();
}
private void initSpinner1() {
final String[] list = SpinnerArrayInitializer.getSpinner1Array();
initSpinner(sp1, list, itemName -> initSpinner2(itemName));
}
private void initSpinner2(String selectedCategory) {
final String[] list = SpinnerArrayInitializer.getSpinner2Array(selectedCategory);
initSpinner(sp2, list, itemName -> initSpinner3(itemName));
}
public void initSpinner3(String selectedCategory) {
final String[] list = SpinnerArrayInitializer.getSpinner3Array(selectedCategory);
initSpinner(sp3, list, itemName -> {
// TODO
});
}
private void initSpinner(Spinner spinner, String[] list, SpinnerListener.Callback
callback) {
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final AdapterView.OnItemSelectedListener listener = new SpinnerListener(this, callback);
spinner.setOnItemSelectedListener(listener);
}
}
And the to initialise the arrays:
public interface SpinnerArrayInitializer {
static String[] getSpinner1Array() {
return new String[]{"Category1", "Category2", "Category3"};
}
static String[] getSpinner2Array(String selectedCategory) {
switch (selectedCategory) {
case "Category1":
return new String[]{"Category1-1", "Category1-2", "Category1-3"};
case "Category2":
return new String[]{"Category2-1", "Category2-2", "Category2-3"};
case "Category3":
return new String[]{"Category3-1", "Category3-2", "Category3-3"};
default:
return new String[0];
}
}
static String[] getSpinner3Array(String selectedCategory) {
switch (selectedCategory) {
case "Category1-1":
return new String[]{"Category1-1-1", "Category1-1-2", "Category1-1-3"};
case "Category1-2":
return new String[]{"Category1-2-1", "Category1-2-2", "Category1-2-3"};
case "Category1-3":
return new String[]{"Category1-3-1", "Category1-3-2", "Category1-3-3"};
default:
return new String[0];
}
}
}
Try this:
public class BookActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private final static int SP1_NUM_OF_OPTIONS = 3;
private final static int SP2_NUM_OF_OPTIONS = 3;
private final static int SP3_NUM_OF_OPTIONS = 3;
private final static String SP3_HINT = "<SELECT>";
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
// Setup data list and adapter for sp1.
ArrayList<String> list = new ArrayList<>();
for(int i=1; i<=SP1_NUM_OF_OPTIONS; i++){
list.add("Category" + i);
}
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
// Set listeners for all spinners.
sp1.setOnItemSelectedListener(this);
sp2.setOnItemSelectedListener(this);
sp3.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
fillSpinnerList(parent.getId(), parent.getItemAtPosition(position).toString());
// Automatically do something different after last step is selected.
if(parent.getId() == R.id.sp3){
if(!parent.getItemAtPosition(position).toString().equals(SP3_HINT)){
// Do something
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
public void fillSpinnerList(int spinnerId, String selectedSpinnerName){
ArrayAdapter adapter;
if(spinnerId == R.id.sp1){
String[] list1;
list1 = getResources().getStringArray(getResources().getIdentifier(selectedSpinnerName, "array", getPackageName()));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
}else if(spinnerId == R.id.sp2){
ArrayList<String> list = new ArrayList<>();
list.add(SP3_HINT);
String[] list1;
list1 = getResources().getStringArray(getResources().getIdentifier(selectedSpinnerName, "array", getPackageName()));
list.addAll(Arrays.asList(list1));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
}
}
}
Add your Category in strings.xml:
<array name="Category1">
<item>Category1_1</item>
<item>Two</item>
<item>Three</item>
</array>
<array name="Category1_1">
<item>Category1_1_1</item>
<item>Category1_1_2</item>
<item>Category1_1_3</item>
</array>
<array name="Two">
<item>Category1_2_1</item>
<item>Category1_2_2</item>
<item>Category1_2_3</item>
</array>
<array name="Three">
<item>Category1_3_1</item>
<item>Category1_3_2</item>
<item>Category1_3_3</item>
</array>
<array name="Category2">
<item>Category2_1</item>
<item>Four</item>
<item>Five</item>
</array>
<array name="Category2_1">
<item>Category2_1_1</item>
<item>Category2_1_2</item>
<item>Category2_1_3</item>
</array>
<array name="Four">
<item>Category2_2_1</item>
<item>Category2_2_2</item>
<item>Category2_2_3</item>
<item>Category2_2_4</item>
</array>
<array name="Five">
<item>Category2_3_1</item>
<item>Category2_3_2</item>
<item>Category2_3_3</item>
<item>Category2_3_4</item>
<item>Category2_3_5</item>
</array>
<array name="Category3">
<item>Category3_1</item>
<item>Six</item>
<item>Seven</item>
</array>
<array name="Category3_1">
<item>Category3_1_1</item>
<item>Category3_1_2</item>
<item>Category3_1_3</item>
</array>
<array name="Six">
<item>Category3_2_1</item>
<item>Category3_2_2</item>
<item>Category3_2_3</item>
<item>Category3_2_4</item>
<item>Category3_2_5</item>
<item>Category3_2_6</item>
</array>
<array name="Seven">
<item>Category3_3_1</item>
<item>Category3_3_2</item>
<item>Category3_3_3</item>
<item>Category3_3_4</item>
<item>Category3_3_5</item>
<item>Category3_3_6</item>
<item>Category3_3_7</item>
</array>
Hope it helps!!
I have two spinners, the first one shows the transport lines and the second one shows the stations of the line selected on the first spinner, the problem is that I want to set to empty value the second spinner when I click on the first one, but I can't do it.
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
I've tried a few options on the onItemClick() method of the first spinner but it's not working propertly:
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
protected void onPreExecute() {
// I'm trying all the options in this method
}
#Override
protected String[] doInBackground(String... strings) {
}
#Override
protected void onPostExecute(String[] result) {
}
}
});
Option 1:
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
Option 2:
spinner2.setAdapter(null);
Option 3:
ArrayList<String> list = new ArrayList<>();
list.add("");
spinner2.setSelection(list.size()-1);
Option 4:
arrayAdapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setSelection(arrayAdapter2.getCount());
spinner2.setOnItemSelectedListener(this);
arrayAdapter2.notifyDataSetChanged();
To sum up, there is a way to change the value of a spinner in function of the value of other one? Or just a way to change the value of a spinner to empty value?
Any help will be apreciated!
Use setOnItemSelectedListener instead of OnItemClickListener like:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos==0) // you can change condition as per your requirement
{
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
The complete code:
public class FillTransportPlaceActivity extends AppCompatActivity {
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_transport_place);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner = findViewById(R.id.listLines);
spinner.setAdapter(arrayAdapter);
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner2 = findViewById(R.id.listStations);
spinner2.setAdapter(arrayAdapter2);
SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
boolean metro = sp.getBoolean("metro", false);
boolean bus = sp.getBoolean("bus", false);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).toString() != null) {
String choice = parent.getItemAtPosition(position).toString();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
new stationsDB().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, choice);
else
new stationsDB().execute(choice);
}
}
});
}
private class stationsDB extends AsyncTask<String, String[], String[]> {
#Override
protected String[] doInBackground(String... strings) {
return DB_transportPlace.getStations(strings[0]);
}
#Override
protected void onPostExecute(String[] result) {
updateAdapter2(result);
}
}
public void updateAdapter(String[] result) {
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner.setAdapter(arrayAdapter);
}
public void updateAdapter2(String[] result) {
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner2.setAdapter(arrayAdapter2);
}
As I said I've tried with setOnItemSelectedListener instead of OnItemClickListener and it didn't work, only execute one time in the onCreate method, I just want to change the value of spinner2 to empty when I change the value of spinner1.
I am creating a gpa calculator and have used a spinner to populate the grades. how do I multiply the value in spinner2 which is credits to the grade points value in textview1 so it displays the result in textview2 which is the gradevalue.(should be something like: gradevalue=credits*gradepoints)
`public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(points1[position]);
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
enter code here`
you just need to set Onitemselected listener on spinner depending upon selected item's index you can change textview's value.Hope it will help you
You need to set the text on your spinner and you can get it with a method like :
getSelectedValue() and you add +1 on it because it start at 0.
Hey KunjiDaniel this is working at my end.Please check this Spinner spinner;
ArrayAdapter<String> adapter;
String[] spinnerData = {"1", "3", "4", "5", "6"};
TextView text2, resultText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_spinner);
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (TextView) findViewById(R.id.numbr2);
text2.setText(String.valueOf(3));
resultText = (TextView) findViewById(R.id.result);
adapter = new ArrayAdapter<String>(SampleSpinner.this, android.R.layout.simple_dropdown_item_1line, spinnerData);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String selected_value = adapterView.getItemAtPosition(position).toString();
changeResultantValues(selected_value);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
int value = 0;
public void changeResultantValues(String spinnerSelected) {
if (!spinnerSelected.equals("")) {
value = Integer.parseInt(spinnerSelected);
}
String textview2Value = text2.getText().toString();
int value2 = Integer.parseInt(textview2Value);
String resultant = String.valueOf(value * value2);
resultText.setText(resultant);
}
i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:
<string-array name="tabs">
<item>tab</item>
<item>tab1</item>
<item>tab2</item>
<item>tab3</item>
<item>tab4</item>
<item>tab5</item>
</string-array>
and in the java
ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);
dont work.
what's the problem? Thanks
This is the full code, because i have problems whit the app (crash)
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs= getResources().getStringArray(R.array.tabs);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
String[] stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
Edited 1
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).
I have 2 spinners with two different adapter's (adapter and adapter2). Based on my selection of my first adapter (adapter), I am populating my second spinner (adapter2). After changing the data in the first spinner I called for adapter2.notifyDataSetChanged(); but this is not updating my second spinner. Any ideas?
UPDATE: It is updating if I call for a new adapter and set it like this:
adapter2 = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, objectList);
playerSpinner.setAdapter(adapter2);
but I don't think this is the correct way of doing this....
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, sDefaultPositions);
positionSpinner.setAdapter(adapter);
adapter2 = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, objectList);
playerSpinner.setAdapter(adapter2);
positionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3)
{
changeSpinnerView(parent.getItemAtPosition(pos).toString());
Log.i("YourTeam", "ItemSlected"+objectList.toString());
adapter2.notifyDataSetChanged();
}
});
private void changeSpinnerView(String position)
{
objectList = getResults(playerkey);
Collections.sort(objectList);
}
public ArrayList<String> getResults(int key)
{
BLLPlayer playerdatasource = new BLLPlayer(this);
playerdatasource.open();
ArrayList<PlayerBean> resultList = new ArrayList<PlayerBean>();
resultList = (ArrayList<PlayerBean>) playerdatasource.getAllFunctionList(key);
playerdatasource.close();
return getPlayerName(resultList);
}
private ArrayList<String> getPlayerName(ArrayList<PlayerBean> resultList)
{
ArrayList<String> nameList = new ArrayList<String>();
for (int iIndex = 0; iIndex < resultList.size(); iIndex++)
{
String beanName = resultList.get(iIndex).getName();
if (beanName.equals("1"))
{
beanName = "";
}
else
{
nameList.add(beanName);
}
}
return nameList;
}