String Resources - android

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).

Related

how to pass spinner selected value to onCreate() section in the same activity?

I want to make an app which collect data based on some Edittexts and two spinners. Then, by clicking a button show all String data in a TextView. I know it may be simple but I wonder how to get the selected Item in spinners and show them near the rest of values.
'''
Spinner shift, permit;
EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;
TextView BigText;
Button SaveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shift=(Spinner)findViewById(R.id.shift_spinner);
permit=(Spinner)findViewById(R.id.permission_spinner);
TextHere=(EditText)findViewById(R.id.text_here);
TrainNumber=(EditText)findViewById(R.id.train_number);
SignalPerson=(EditText)findViewById(R.id.signal_person);
NavganPerson=(EditText)findViewById(R.id.navgan_person);
DriverPerson=(EditText)findViewById(R.id.driver_person);
SaveButton=(Button) findViewById(R.id.save_btn);
BigText=(TextView)findViewById(R.id.text_text);
ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
this,
R.array.shifts,
R.layout.support_simple_spinner_dropdown_item
);
shift.setAdapter(ShiftSelect);
shift.setOnItemSelectedListener(this);
ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
this,
R.array.permissions,
R.layout.support_simple_spinner_dropdown_item
);
permit.setAdapter(PermissionSelect);
permit.setOnItemSelectedListener(this);
final String TN = TrainNumber.getText().toString();
final String TH = TextHere.getText().toString();
final String SP = SignalPerson.getText().toString();
final String NP = NavganPerson.getText().toString();
final String DP = DriverPerson.getText().toString();
SaveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if (spinner.getId()==R.id.shift_spinner){
String SHIFT = parent.getSelectedItem().toString();
}
if (spinner.getId()==R.id.permission_spinner){
String PER = parent.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
'''
I want to pass Item that is selected from public void onItemSelected to onCreate method section to show it in a txetview.
Thanks you.
Try this.
TextView textView;
Spinner spinner;
String SEL_VALUE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.shift_spinner);
textView=(TextView)findViewById(R.id.textView);
SEL_VALUE="";
// Create adapter here
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sel_val = spinner.getSelectedItem().toString();
// Setting selected value to textView.
SEL_VALUE = sel_val;
textView.setText(sel_val);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
I hope this is what you asked for
Spinner shift, permit;
EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;
TextView BigText;
Button SaveButton;
String SHIFT,PER;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shift=(Spinner)findViewById(R.id.shift_spinner);
permit=(Spinner)findViewById(R.id.permission_spinner);
TextHere=(EditText)findViewById(R.id.text_here);
TrainNumber=(EditText)findViewById(R.id.train_number);
SignalPerson=(EditText)findViewById(R.id.signal_person);
NavganPerson=(EditText)findViewById(R.id.navgan_person);
DriverPerson=(EditText)findViewById(R.id.driver_person);
SaveButton=(Button) findViewById(R.id.save_btn);
BigText=(TextView)findViewById(R.id.text_text);
ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
this,
R.array.shifts,
R.layout.support_simple_spinner_dropdown_item
);
shift.setAdapter(ShiftSelect);
shift.setOnItemSelectedListener(this);
ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
this,
R.array.permissions,
R.layout.support_simple_spinner_dropdown_item
);
permit.setAdapter(PermissionSelect);
permit.setOnItemSelectedListener(this);
final String TN = TrainNumber.getText().toString();
final String TH = TextHere.getText().toString();
final String SP = SignalPerson.getText().toString();
final String NP = NavganPerson.getText().toString();
final String DP = DriverPerson.getText().toString();
SaveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BigText.setText(SHIFT+" "+PER);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if (spinner.getId()==R.id.shift_spinner){
SHIFT = parent.getSelectedItem().toString();
}
if (spinner.getId()==R.id.permission_spinner){
PER = parent.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
Try this Out.
TextView textView;
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.shift_spinner);
textView=(TextView)findViewById(R.id.textView);
// Create adapter here
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sel_val = spinner.getSelectedItem().toString();
// Setting selected value to textView.
textView.setText(sel_val);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}

How do I use the inputs of 2 separate spinners

I'm trying to build an app that takes 2 inputted redox pairs from 2 spinners to display information about the cell that could be made from these chemicals such as what would be used as the anode/cathode, the reduction potential of each half-reaction and the cell etc.
My issue is I haven't been able to find any way to take the input of both the spinners to be able to make the calculations, only have both spinners do the same thing.
My current code is this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1, spinner2;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(MainActivity.this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(MainActivity.this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice1 = parent.getItemAtPosition(position).toString();
double first = 0;
double second = 0;
double anode;
double cathode;
if (choice1 == getResources().getString(R.string.MnO4)){
first = 0.60;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
} else if (choice1 == getResources().getString(R.string.Cl)) {
first = 1.36;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
}
if (first > second) {
cathode = first;
anode = second;
} else {
cathode = second;
anode = first;
}
double cellValue = anode - cathode;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I'm very new to android studio so any help is greatly appreciated.
Thanks
I suggest that you use anonymous classes to handle the onItemSelected. That way you can handle the selection of each Spinner independently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner1 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner2 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Now you can remove the Override method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
and you can remove implements :
implements AdapterView.OnItemSelectedListener

Using HashMaps in spinners android studio

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());

Spinner in android studio

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);
}

Application crash when try to set text selected from spinner to textview

I want to set text from selected item from spinner to textview, But when i tried to do application craches. Can any one help me .
public class MainActivity extends AppCompatActivity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
String result = sp.getSelectedItem().toString();
txt.setText(result);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
}
sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
This may help Frnd.
spinner has no "getSelectedItem". https://developer.android.com/reference/android/widget/Spinner.html https://developer.android.com/guide/topics/ui/controls/spinner.html
and here's how you get the text-value of the selected row: https://stackoverflow.com/a/15545909/3022387
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Implement spinner.setOnItemClickListener and then find text inside that method like this.
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
You can't get selected Item before setting adapter.Try this below code
public class MainActivity extends AppCompatActivity {
final TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

Categories

Resources