I have a spinner which displays both options, but when I accept the female option, it still takes the answer as a male, any suggestions?
List<String> SpinnerArray = new ArrayList<String>();
SpinnerArray.add("Male");
SpinnerArray.add("Female");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner Items = (Spinner) findViewById(R.id.genderSpinner);
Items.setAdapter(adapter);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
String selected = Items.getSelectedItem().toString();
if (selected.equals("Male")) {
inputGen = "Male";
}
if (selected.equals("Female")){
inputGen = "Female";
}
Following the official guide:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
You can try a switch of position and then try your same conditions.
Related
my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
I made a listview and a spinner with 3 options. I want that spinner to connect with Listview on other intent.
The spinner is on a Edit page so whenever I click on 1 item on the list I get to the edit page.
I want the first option on the spinner to make the selected item on the list green the second will make it yellow and then third will make it red. How do I do it?
public class EditList extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_list);
final Button button=(Button)findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EditList.this,Vans.class);
startActivity(intent);
}
});
// Spinner element
spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(EditList.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("רכב עובד");
categories.add("יש תקלה");
categories.add("רכב במוסך");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String work = parent.getItemAtPosition(position).toString();
if (position==0){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.GREEN);
}
Vans.listView.setBackgroundColor(position);
spinner.setSelection(position);
String takala = parent.getItemAtPosition(position).toString();
if (position==1){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.YELLOW);
}
String garage = parent.getItemAtPosition(position).toString();
if (position==2){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.RED);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Here is my code below, which is getting item from spinner on click
public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1,spinner2,spinner3;
private static final String[] sports = {
"Hockey","Cricket","Football","Basketball","Badminton","Tennis"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner1 = (Spinner)findViewById(R.id.drop_down);
spinner2 = (Spinner)findViewById(R.id.drop_down2);
spinner3 = (Spinner)findViewById(R.id.drop_down3);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,sports);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
spinner3.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
How do I preserve the selected spinners' item even after exiting the application?
You can use SharedPreference to store the selected value position/id/string.
Simply add this line when you get String item
Editor edit = context.getSharedPreferences("Name_of_sf",Context.MODE_PRIVATE).edit();
edit.putString("selected_item", item);
edit.commit();
And can simply retrieve the value as
context.getSharedPreferences("Name_of_sf",Context.MODE_PRIVATE).getString("selected_item", "");
For more info #AnirudhSohil you could see the official documentation, it has a very detail examples, I hope that it helps you.
http://developer.android.com/training/basics/data-storage/shared-preferences.html
I want to build android app with spinner , I would like to appear in spinner some text , and when selected, it appears in TextView other text than the value of which is a selected.
for example , i want to appear in spinner website name (like= stackoverflow) and when select appear in TextView the website URL (like=https://stackoverflow.com/)
can anyone help me?
thanks in advance
First, you create yourself an Object:
public class LanguageObject {
private String title;
private String url;
public LanguageObject(String title, String url) {
this.title = title;
this.url = url;
}
public String getTitle() {
return title;
}
public Integer getUrl() {
return url;
}
}
Then, in your Activity/Fragment, you create an ArrayList with the objects you need:
ArrayList<SpinnerObject> spinnerObjectList = new ArrayList<SpinnerObject>();
spinnerObjectList.add(new SpinnerObject("Google", "www.google.com"));
spinnerObjectList.add(new SpinnerObject("StackOverflow", "www.stackoverflow.com"));
In addition to that, create a custom adapter that will handle your data:
public class SpinnerAdapter extends ArrayAdapter<SpinnerObject> {
public SpinnerAdapter (Context context, int textViewResourceId, ArrayList<SpinnerObject> spinnerObjects) {
super(context, textViewResourceId, spinnerObjects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
SpinnerObject spinnerObject = getItem(position);
if(view == null) {
LayoutInflater layoutInflater;
layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(android.R.layout.select_dialog_item, null);
if (view != null) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(spinnerObject.getTitle());
}
}
return view;
}
Now you can use this adapter on your Spinner, and get the url when one of the items was clicked.
You can add text to spinner like this:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> texts = new ArrayList<String>();
texts.add("Google");
texts.add("Facebook");
List<String> urls = new ArrayList<String>();
urls.add("www.google.com");
urls.add("www.facebook.com");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, texts);
spinner.setAdapter(adapter);
then you need to listen for item selected on spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String url = urls.get(i);
// do with url whatever you need
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
For adding text in spinner you can use following code in onCreate()
Spinner locale;
ArrayList<String> lList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locale =(Spinner)findViewById(R.id.Cmblocale);
// bind data to Spinner(ComboBox)
lList =new ArrayList<String>();
lList.add("English");
lList.add("Canada");
lList.add("Chinese");
lList.add("French");
lList.add("Germany");
lList.add("Japan");
ArrayAdapter<String> cAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,lList);
locale.setAdapter(cAdapter);
}
For Retrieving value use following code inside your setOnItemSelectedListener on spinner
if(locale.getSelectedItem().equals("English"))
{
txtText.setText(English);
}else if(locale.getSelectedItem().equals("Chinese"))
{
txtText.setText(Chinese);
}else if(locale.getSelectedItem().equals("Canada"))
{
txtText.setText(Canada);
}else if(locale.getSelectedItem().equals("French"))
{
txtText.setText(French);
}else if(locale.getSelectedItem().equals("Germany"))
{
txtText.setText(Germany);
}else if(locale.getSelectedItem().equals("Japan"))
{
txtText.setText(Japan);
}
Spinner spinner = (Spinner) findViewById(R.id.spinner); //create a spinner view in XML
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.your_array, android.R.layout.simple_spinner_item); // create adapter which has second parameter as the array which contains the values you want to show as entries of spinner
spinner.setAdapter(adapter); // set the adapter to your spinner and your spinner will show the values from array that you set in second parameter of adapter
To listen to the click on the options of the spinner
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
Now to show the appropriate link of the website for selected option from spinner you can have a map which has keys as same as the spinner values and values as websites as you want....
HashMap<String, String> nameWebsiteMap = new HashMap<String, String>();
nameWebsite.put("stack overflow","http://stackoverflow.com/"); // for example
So in onItemSelected you can simply do this
selectedSiteName = nameWebsite.get(parent.getItemAtPosition(pos).toString());
textView.setText(selectedSiteName);