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
Related
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I set out to create a project of an Android app consisting of 3 activities.
The first activity has a layout with 3 elements, 2 spinners and a button.
I set out with an intention to make them work in the following manner:
MainActivity.java:
1. Let's just say that I wanted the first spinner to load a set of categories on app startup. e.g., Income, Expenditure.
2. After I select a category from the first spinner, the second spinner will load up its respective sub-categories in the second spinner. e.g., If I choose Income in the first spinner, the second spinner with load up Commission, Salary, Bonus, etc. and if Expenditure is chosen, Monthly Expenditure, Conveyance, Bills, etc are loaded in the second spinner.
3. Now, on the basis of which sub-category I chose, when I click the button, it will open a new activity and set the title of the action bar for that particular category and sub-category. e.g., If I chose Income and Salary, the next activity will show Income, Salary as title after I click the button in the main activity.
I have created a class named Utils.java to store the set of category and sub-category names as string arrays.
public class Utils{
public static String[] mainCategoryList =
{
"Choose One",
"Income",
"Expenditure"
};
//
public static String[] chooseOne_list=
{
"Choose One"
};
public static String[] income_subCategoryList =
{
"Bonus",
"Commision",
"Salary"
};
public static String[] expenditure_subCategoryList=
{
"Bills",
"Conveyance",
"Monthly Expenditure"
};
public static String[] income=
{
"Income, Bonus",
"Income, Commission",
"Income, Salary"
};
public static String[] expenditure=
{
"Expenditure, Bills",
"Expenditure, Conveyance",
"Expenditure, Monthly Expenditure"
};
}
The coding I have done for MainActivity.java is given below,
public class MainActivity extends AppCompatActivity {
Spinner spinner1_mainCategory, spinner2_subCategory;
Button buttonChoose;
String spinner1Item, spinner2Item;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Spinner element
spinner1_mainCategory = (Spinner)findViewById(R.id.spinner1_mainCategory);
spinner2_subCategory = (Spinner)findViewById(R.id.spinner2_subCategory);
//Button element
buttonChoose = (Button)findViewById(R.id.buttonChoose);
//To keep onCreate() clean of multiple adapter setting
adapterMethod(spinner1_mainCategory,mainCategoryList);
// Spinner click listener
spinner1_mainCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//String.valueOf(array_name.getSelectedItem()) captures the value of the selected option of a specific string array onClick event
spinner1Item = String.valueOf(spinner1_mainCategory.getSelectedItem());
switch (position){
case 0:
adapterMethod(spinner2_subCategory,chooseOne_list);
spinner2_subCategory.setVisibility(View.GONE);
//Button click listener
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (spinner1Item.contentEquals(Utils.mainCategoryList[0])){
Toast toast = Toast.makeText(getApplicationContext(), "Choose a Category First", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
toast.show();
}
}
});
break;
case 1:
adapterMethod(spinner2_subCategory,income_subCategoryList);
spinner2_subCategory.setVisibility(View.VISIBLE);
break;
case 2:
adapterMethod(spinner2_subCategory,expenditure_subCategoryList);
spinner2_subCategory.setVisibility(View.VISIBLE);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2_subCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, final int position, long id) {
spinner2Item = String.valueOf(spinner2_subCategory.getSelectedItem());
switch (position){
case 0:
//Income Category, Bonus Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[0])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[0]);
}
});
}
//Expenditure Category, Bills Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[0])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[0]);
}
});
}
break;
case 1:
//Income Category, Commission Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[1])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[1]);
}
});
}
//Expenditure Category, Conveyance Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[1])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[1]);
}
});
}
break;
case 2:
//Income Category, Salary Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[2])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[2]);
}
});
}
//Expenditure Category, Monthly Expenditure Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[2])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[2]);
}
});
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void adapterMethod(View v, String[] list){
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, list);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
// attaching adapter to spinner for district
if (v.getId()==R.id.spinner1_mainCategory){
spinner1_mainCategory.setAdapter(dataAdapter);
spinner1_mainCategory.setSelection(0, false);
}
if (v.getId()==R.id.spinner2_subCategory){
spinner2_subCategory.setAdapter(dataAdapter);
spinner2_subCategory.setSelection(0, false);
}
}
public void changeTitleInNextActivity(String title){
intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("Title", title);
startActivity(intent);
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String title = intent.getStringExtra("Title");
getSupportActionBar().setTitle(title);
}
}
I don't understand why, but I can't get the coding to work for me the way I wanted it to. Selection from the first and second spinner works perfectly, but its the button that I can't get to work to set the title in the next activity no matter how many approaches I try.
Tell me, guys, what did I do wrong? How to better code to avoid this kind of pitfall.
Can't make the Button "Choose":
to go to the SecondActivity
In your second activity,
get your passed string
String title=getIntent().getStringExtra("Title");
Then set your title
getSupportActionBar().setTitle(title);
Hello to the community,
got a problem with the implementation of my project, which I hope someone can give me because some clarity.
Short description of my project:
I want to change from the Activity_A means of a button onclick events the default position of a spinner in the Activity_B. In my sample code I've tried using an Intent, Value Activity_A to the spinner in the Activity_B in an if - to give statement which then starts the spinner in the corresponding position.
Here is my code:
Activity_A:
public class Start extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
Button button = (Button) findViewById(R.id.button_1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Start.this, Spinnerwert.class);
intent.putExtra("position", "3");
startActivity(intent);
}
});
}
}
Activity_B:
public class Spinnerwert extends Activity {
private TextView beschreibung_1;
private TextView beschreibung_2;
private Spinner s1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert);
// TextView issue of Spinner
beschreibung_1 =(TextView)findViewById(R.id.tV_spinner_1);
// TextView output Intent worth
beschreibung_2 =(TextView)findViewById(R.id.tV_spinner_2);
// Intent data receiving
Intent i = getIntent();
String selected = i.getStringExtra("position");
beschreibung_2.setText(selected);
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Spinner position - Response to Intent worth handover --------
s1.setSelection(position);
if (position == 0) {
s1.setSelection(0);
} else if (position == 1) {
s1.setSelection(1);
} else if (position == 2) {
s1.setSelection(2);
}
beschreibung_1.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
It seems as though your intent is passing a String extra, and only changing a text view within the second activity. If I am correct at understanding your question, you are wanting to place the spinner in a certain position based on the intent passed from the first activity.
Using the calls similar to those in your IF statement, simply parse the selected String to an int and call s1.setSelection on it.
It should look something like this:
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setSelection(Integer.parseInt(selection));
I hope this helped!
It looks like you're never setting the spinner to the selected index. Your if statements in onItemSelected aren't doing anything since "position" is a value that gets passed in from the spinner and not the previous intent.
Try putting this
s1.setSelection(Integer.parseInt(selected));
after you set your adapter.
Spinner variant with equals value
public class Spinnerwert_2 extends Activity {
private TextView beschreibung_3;
private TextView beschreibung_4;
private Spinner s2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert_2);
beschreibung_3 =(TextView)findViewById(R.id.tV_spinner_3);
beschreibung_4 =(TextView)findViewById(R.id.tV_spinner_4);
Intent intent_spinner_2 = getIntent();
String spinner_2_auswahl = intent_spinner_2.getStringExtra("position_spinner_2");
beschreibung_4.setText(spinner_2_auswahl);
// #2 Spinner + Funktion(Auswahl aus Resource values/array.xml)
s2 = (Spinner) findViewById(R.id.spinner_2);
ArrayAdapter<CharSequence> adapter_2 = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter_2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter_2);
// #2 SPINNER-ITEM POSITIONSEINSTELLUNG / ZUWEISUNG
s2.setSelection(adapter_2.getPosition(spinner_2_auswahl));
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (s2.getSelectedItem().toString().equals("Mercury")){
Toast.makeText(getApplicationContext(), "Auswahl Mercury", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Venus")){
Toast.makeText(getApplicationContext(), "Auswahl Venus", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Earth")){
Toast.makeText(getApplicationContext(), "Auswahl Earth", Toast.LENGTH_SHORT).show();
}
beschreibung_3.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity.java is:
public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", "animalbites.html");
startActivity(intent);
}
}
});
}
}
code of CustomOnItemSelectedListener.java is:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and the display.java is:
public class display extends Activity implements OnClickListener {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent=getIntent();
String mUrl=intent.getStringExtra("urlpath");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/"+mUrl);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
i am not exactly getting your problem, but i ll answer according to what i got:
Why are you concerned of adding an onClickListener to the spinner?
Just add onClickListener to the button and on the click of that button do:
String selecteditemName=(String)spinner1.getSelectedItem();
//spinner1 having items : "file1" , "file2"
Intent i=new Intent(this, yournewActivity.class);
i.putExtra("selected_item", selecteditemName);
startActivity(i);
Now in that activity (yournewActivity) containing the webview use this code in onCreate() method:
String item;
Intent i=getIntent();
item = i.getStringExtra("selected_item");
Now you have your spinner selection in the String 'item', you can now do whatever you want:
My point is that : You should use your spinner selection as ID for populating the webview.
If you still want to use any IDs other than the ones present in the spinner then
make an extra java class Idgetter,
class idgetter
{
public static String getID(String name)
{
if(name.equals("file1"))
return "requiredfile1.html";
else if(name.equals("file2"))
return "requiredfile2.html";
}
}
Now, Do exactly as the above code says and after you have the spinner selection in the variable item , use:
String id=idgetter.getID(item); //in younewActivity
You are finally done.
By using Intent's putExtra method.
Intent mIntent = new Intent(this, display.class);
mIntent.putExtra("urlpath", "put value in here");
startActivity(mIntent);
Populate the spinner with number of html pages and Use spinner.getSelectedItem().toString() inside the addListenerOnButton() method. By doing do, you will get the selected html page and pass it using intent to the next layout.
CODE:
`public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
int final websiteA = 1;
int final websiteB = 2;
int final websiteC = 3;
String selectedHtmlPage = "";
.........
.........
.........
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
switch(arg2)
{
case websiteA :
selectedHtmlPage = "websiteA.html";
break;
case websiteB :
selectedHtmlPage = "websiteB.html";
break;
case websiteC :
selectedHtmlPage = "websiteC.html";
break;
}
}
public void addListenerOnButton()
{
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", selectedHtmlPage);
startActivity(intent);
}
}
});
}
}`
Solved it, its simple. Here is what I did:
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
}
- - -
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", mLink);
startActivity(intent);
}
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
mLink="Beef html/BBQ_Meatballs_Recipes.html";
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
i am new in Android development.
Im trying to transfer selected data from multiple spinners (Search activity) to another activity (JSON Search result activity)
and at the end i have button that open the search result
Search activity:
i have the java spinner
ArrayAdapter<CharSequence> whatlist = ArrayAdapter.createFromResource(this,
R.array.whatlist, android.R.layout.simple_spinner_item);
whatlist.setDropDownViewResource(R.layout.spinner_style);
spwhat = (Spinner) findViewById(R.id.spWhat);
spwhat.setAdapter(whatlist);
spwhat.setOnItemSelectedListener(new MyOnItemSelectedListener());
and the MyOnItemSelectedListener
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strs = new Bundle();
Intent i = new Intent(SearchActivity.this, SearchResult.class);
strs.putString("setwhat", parent.getItemAtPosition(pos).toString());
i.putExtras(strs);
}
public void onNothingSelected(AdapterView<?> arg0) {}
}
This is the button
btnsearch = (Button)findViewById(R.id.btnSearch);
btnsearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ia = new Intent(SearchActivity.this, SearchResult.class);
SearchActivity.this.startActivity(ia);
}
});
This is in the search result
Bundle extras = getIntent().getExtras();
if(extras!=null){
Integer item = extras.getInt("setwhat");
//Use a switch(item) here to switch to different links based on selection
TextView tv = (TextView) findViewById(R.id.tvtv);
tv.setText("Another Activity, Item is :" + item.toString());
the text wont change.
i have tried any tutorial on the web and searching here for a solution for hours..
anyone can help?
You don't even need a listener for your spinner. Just change your button's onclick to this:
btnsearch = (Button)findViewById(R.id.btnSearch);
btnsearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner spwhat = (Spinner) findViewById(R.id.spWhat);
Intent ia = new Intent(SearchActivity.this, SearchResult.class);
ia.putExtra("setwhat", spwhat.getSelectedItem().toString());
startActivity(ia);
}
});