How to submit spinner selecteditem text via btnup and why selectedReport on btnup cases is declared not used?I have search hard enough to solve this but i still blankon what i need to do in order to get text on spinner to suit the coding.
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
Button bLogout;
ImageView viewImage;
Button b,btnup;
private String selectedReport = null;
} private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Array list of report to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Crime");
list.add("Bribery");
list.add("Schools problem");
list.add("Homeless");
list.add("Rural Problems");
list.add("Public Transport");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedItem = parent.getItemAtPosition(pos).toString();
switch (parent.getId()) {
case R.id.spinner:
if (selectedReport != null) {
Toast.makeText(parent.getContext(), "Report you select is " + selectedItem,
Toast.LENGTH_LONG).show();
}
selectedReport = selectedItem;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String selectedReport = spinner.getSelectedItem().toString();
break;
}
}
Declare your views before onCreate in your class:
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
ImageView viewImage;
Button b, bLogout, btnup;
Spinner spinner;
String selectedReport;
In your onCreate, add the following:
String[] list = {"Crime", "Bribery", "Schools Problem", "Homeless", "Rural Problems", "Public Transport"}
spinner = (Spinner) findViewById(R.id.spinner);
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);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(this, "Report selected: " + list[position], Toast.LENGTH_LONG).show();
//You can remove this switch statement, if you don't need it.
switch (position) {
case 0:
//Do something
break;
case 1:
//Do something
break;
case 2:
//Do something
break;
case 3:
//Do something
break;
case 4:
//Do something
break;
case 5:
//Do something
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
I am assuming here that you have already set an onClickListener to your buttons in onCreate.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
selectedReport = spinner.getSelectedItem().toString();
break;
}
}
Related
First of all, I am fairly new at this, so bear with me.
I am trying to make a spinner where if you select an item, the layout and the activity will change, but I cant even implement the simplest of spinners.
so far the only code I've got is the one below, but no matter what I put in it doesn't do anything, so I must just not understand it, please be very specific in your answers. thank you. the first answer got me some of the way, but the "spinner spin..... spin.setAdapter(aa)" part is not accepted under OnCreate
String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Intent intent2 = new Intent(this, gen2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(this, Gen3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(this, gen4.class);
startActivity(intent4);
break;
case 4:
Intent intent5 = new Intent(this, gen5.class);
startActivity(intent5);
break;
case 5:
Intent intent6 = new Intent(this, MainActivity.class);
startActivity(intent6);
break;
case 6:
Intent intent7 = new Intent(this, gen7.class);
startActivity(intent7);
break;
}
}
Here is a example, hope it helps you understand it better:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
switch (position){
// Check what position was selected
case 1:
//start activity depending on position
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(this, ActivityThree.class);
startActivity(intent);
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I have 4 activity with spinner inside every activity and this spinner include 3
string data (drop-down selection), when I pass from activity to another one I must pass this selected data inside spinner like if I have chosen data x from the list in the spinner and click in button the selected data must be in second activity spinner as x too.
I read several solutions without any solving. I hope to solve it here and this is my code for spinner and where to put the intent code
public class Page1 extends AppCompatActivity {
Spinner spinner;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.film_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
switch (position)
{case 0:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
double thick = Double.valueOf(editText1.getText().toString());
double width = Double.valueOf(editText2.getText().toString());
}
});
break;
case 1:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
} catch (NumberFormatException e) {
//not a double
}
}
});
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
First save the position of selected data from spinner in a String variable,
int positionOfSelectedDataFromSpinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
positionOfSelectedDataFromSpinner= position;
}
Then on button click send intent to Another activity with putExtra
Intent i = new Intent (this, activity2.class);
i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);
get int from getIntent in another activity
Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");
then set the position to spinner
spinner.setSelection(positionToShowToSpinner);
I think this my solve your problem.
this is the most painless way i can think of:
make a new class or a static member of an existing class but the second solution makes the code less understandable.
public class SpinnerPosHolder{
public static int poition;
}
then in all 4 of them:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SpinnerPosHolder.position = spinner.getSelectedItemPosition();
For using it:
spinner.setSelection(SpinnerPosHolder.position)
You can do it in this way
String selectedItem=spinner1.getSelectedItem().toString();
And that String you can pass using Intent
I created some Activities on my app,so i created a listView with the name of those activities , so i can click on them and open the activity that i want (with setOnClickListener method and switch.) and a i created a searhView, so i can filter them by its name (with setOnQueryTextListener).
Let's use 3 names as examples: Alfred,Beth,Bill
In the "normal" listView Alfred is 0, Beth is 1, Bill is 2. So i made this:
switch( position )
{
case 0: Intent newActivity = new Intent(telaPesquisa.this, alfred.class);
startActivity(newActivity);
break;
case 1: Intent newActivityy = new Intent(telaPesquisa.this, beth.class);
startActivity(newActivityy);
break;
case 2: Intent newActivity2 = new Intent(telaPesquisa.this, bill.class);
startActivity(newActivity2);
break;
When i search in the searchView for "B" just Beth and Bill appears, and that is right, but now Beth is case 0, and Bill is case 1, so it will not open the activity that i want.
How can i set absolute values to the itens? like Alfred is always 0, Beth always 1 and Bill awalys 2?
Here is my entire code.
public class telaPesquisa extends Activity {
ListView lv;
SearchView sv;
String[] teams={
"Alfred",
"Beth",
"Bill",
};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_pesquisa);
lv=(ListView) findViewById(R.id.listView);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//nt firstPosition = lv.getFirstVisiblePosition()+3;
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( position )
{
case 0: Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case 1: Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case 2: Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});
sv.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
}
sorry for my english
Modify the listener as this:
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( itemValue )
{
case "alfred": Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case "beth": Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case "bill": Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});
I'm creating converter (distance) 4 Android. My idea was do it like:
EditText - user put value f.e. 40
two Spinners - first to set started unit and second to set destination unit f.e. first [cm] second [meters]
TextView with result
Button to calculating
I did functions with calculating values, the problem is how to choose units
this is my code which didn't work
spinnerP = (Spinner) findViewById(R.id.spinnerPocz);
ArrayAdapter<CharSequence> adapterP = ArrayAdapter.createFromResource(this, R.array.odlegloscArray, R.layout.support_simple_spinner_dropdown_item);
adapterP.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinnerP.setAdapter(adapterP);
spinnerP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getBaseContext(), parent.getItemIdAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerD = (Spinner) findViewById(R.id.spinnerDoc);
ArrayAdapter<CharSequence> adapterD = ArrayAdapter.createFromResource(this, R.array.odlegloscArray, R.layout.support_simple_spinner_dropdown_item);
adapterD.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinnerD.setAdapter(adapterD);
spinnerD.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinnerP.getItemAtPosition(position) == 0) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "zmień wartość docelową", Toast.LENGTH_SHORT).show(); //units the same
break;
case 1:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaKm(); //meters to km
}
});
break;
case 2:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaCm(); //meters to cm
}
});
break;
case 3:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaCale(); //meters to inch
}
});
break;
//etc.
//then there is
else if (spinnerP.getItemAtPosition(position) == 1) {
switch (position) {
case 0:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kilometryNaM(); //km to meters
//etc
I guess that doing it like this is horrible, but I'mma not advanced, I am open to suggestions
What you should do is to remove all your OnItemselectedListeners, and use only one OnClickListener for your button. Then use getSelectedItemPosition.
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedP = spinnerP.getSelectedItemPosition();
int selectedD = spinnerD.getSelectedItemPosition();
switch(selectedP){
case 0:
switch(selectedD){
case 1:
convertFrom0to1();
break;
case 2:
convertFrom0to2();
....
}
break;
....
}
}
});
So I have a Spinner and I wan't user to select one item from it and then display his selection in another activity.. but Button won't even do anything.. I'm guessing error is in intent.putExtra of int position == 0.. can somebody help me out?
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
This is the second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
Thank you!!
Here's the modified code of first activity:
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Toast.makeText(PrehranaInputMain.this, R.string.cilj_prehrana_nista_odabrano, Toast.LENGTH_SHORT).show();
} else if (position == 1){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getSelectedItem().toString());
startActivity(intent);
} else if (position == 2){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJeMrsavljenje", cilj.getSelectedItem().toString());
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
and second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
String prehrana2 = intent.getStringExtra("ciljJeMrsavljenje");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
// SEEMS LIKE SOME IF STATEMENT NEEDED HERE?
//ciljPrehranaRezultat.setText(prehrana2);
your listener to the drop down is not set until the user clicks the button, which means until you click the button at least once, selecting an item from the drop down doesn't do anything.
have you tried to select an item from the spinner after you click the button?
if you want to go to the other activity when the user clicks the button, keep only the startActivity(intent) in the onClick() method and move the onItemSelected() method up a level, move the intent as a global variable.
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
}
});
also, even in that case, it appears to me that you are only jumping to the activity when the user selects item at position "0". can we assume this is not a blank space and your spinner only has 2 entries in it?
Change the following lines:
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", (CharSequence)cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
To:
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemAtPosition(position));
startActivity(intent);