I'm working on an Activity where I need to start a new Activity at the click of a button based on the selected item in a spinner. I can't quite figure it out. This is what I have after clearing failed attempts.
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.user_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner.equals("My String Here")){
startActivity(new Intent(Register.this, *****.class));
}else{
startActivity(new Intent(Register.this, *****.class));
}}
Am I better off just installing two buttons with their own proper syntax starting new activities separately?
If spinner.equals does not make any sense at all.
You should use position integer value to check your array data and then make a decision based on that value
First things first.
spinner.equals("My String Here")
spinner is of type Spinner whereas "My String Here" is a String. You are always sure that they will never be equated the same.
Next, try to make use of your position and adapter to be able to properly construct your condition.
Related
I am currently learning android and working on a few projects...in this one I have a list view and when I click it I pass the name of the row I click and a value from a database to my NEXT activity and that works 100%.
This is part of my main Activity on create code. where I populate the listview from my database here and other stuff.
lvUsers = (ListView)findViewById(id.lvUsers);
List<String> listUsers = dbHeplper.getAllUsers();
if(listUsers != null){
adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1,listUsers);
// adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);
lvUsers.setAdapter(adapter);
lvUsers.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String product= ((TextView)view).getText().toString();
// String product= String.valueOf((TextView)findViewById(id.movie_title));
// Toast.makeText(getBaseContext(),product,Toast.LENGTH_LONG).show();
Intent x = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
x.putExtra("product", product);
startActivity(x);
}
});
This code works fine, but I have attempted to style my row myself if you look at this line. (which is commented in the main view)
adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);.
Adding this threw the error of relativelayout cannot be cast to android widget textview on the line
String product= ((TextView)view).getText().toString();
So I took that to mean it does not know which textview I am referring to anymore. so I edited that line to
String product= String.valueOf((TextView)findViewById(id.movie_title));
because movie_title is the ID of the textview in the test layout.
The then disappeared but now when I click the row instead of getting that data I expected like row name and data from database. the textview in my second activity is displaying
"android.widget.TextView{176f155.v.Ed (more random numbers) app:id/movie_title}
(Some of the code from second activity
Intent i=getIntent();
String name = i.getStringExtra("product");
txtName.setText(name);
Like I said all these errors are occurring from me trying to implement my owned custom rows. So If anyone could point out where I went wrong while doing this I would be grateful .
Have you tried creating a custom adapter and making it extend ArrayAdapter ? You can do this easily and then inside your getView() of your custom adapter you can inflate your textview with your appropriate text . Check this : https://stackoverflow.com/a/8166802/1497188
Also String product= String.valueOf((TextView)findViewById(id.movie_title)); , this line is getting the ID of the view and not the text inside the view. The appropriate view to do it is that from inside your onItemClick() you would convert/cast your view into a textview and then do the getText().toString() on that view. Just Search online how to create custom adapters and onItemClick Listener for Adapter
EDIT:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String data=(String)adapterView.getItemAtPosition(arg2);
This will atleast help you get the string from inside your onItemClick()
I'm creating application for the spinner. I have two activities in my application which is Spinner in second Activity. I'm trying to pass the string value from one Activity to another Activity and that string value put it into Spinner in another Activity.
I tried but I can't find proper solution.
Here is my spinner code
private String[] models = { " 207"," 308CC"," 308SW"," 508"," 3008"," 5008"," 308", " RCZ R"," 5008", " 207CC"};
Intent intent = getIntent();
modelString = intent.getExtras().getString("MODEL");
spinnerModels = (Spinner)findViewById(R.id.Model_spinnerTestDrive);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, models);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerModels.setAdapter(adapter_state);
int spinnerPosition = adapter_state.getPosition(spinnerModels);
spinnerModels.setSelection(spinnerPosition);
I have already put some value into spinner. If I set this value it should be displayed in spinner.
I'm not sure if you mean you want to make the String the current selection in the 2nd Activity's Spinner or if you want to add the String to the available options in the Spinner. Either way, I think you have to access the ArrayAdapter directly. See this answer and it might help you.
I have three spinners in one Activity , so I want to choose from one spinner then getting some specific values to the second spinner and after choose one of the second then getting some specific values to third spinner .
i have tired with using loops and switch case but i think it takes much time and long code .
is there is simple way to make this three spinner dependent to each other ?
Disable spinner 2 in your layout, and then enable and populate it when spinner 1 item is selected. Then do the same for spinner 3 based on spinner 2.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Enable Spinner 2
// Set spinner 2 adapter
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// Disable spinner 2
// Set spinner 2 adapter to empty list
}
});
i'm trying to solve something that i'm not sure about. I wants to show a parameter call "decription" in the single item's detail when the single listview item is selected but the "decription" should not be displayed on the single listview item. I know where the problem lies but i'm not sure how do i solve it. Please kindly give me your suggestions! Here is my code that the mistake lies:
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC, KEY_COUNTRY_NAME,
KEY_VENUE_NAME, KEY_START_TIME }, new int[] {
R.id.title,R.id.description, R.id.countryName, R.id.venueName,
R.id.startTime });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title))
.getText().toString();
String description = ((TextView) view
.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
As you can see, the KEY_DESC is the one i'm talking about, but in order for me to use the in.putextra(KEY_DESC, description); for me to use the in.getStringExtra from another class. But the description gets from the R.id.description field which its declared in the ListAdapter, which is also displayed in the listview items. I wants to remove from the description from the listview items but still be able to use the in.putExtra(KEY_DESC, ??) method. May i know what should i do?
If you don't want the description to be shown in the main list, just remove the UI element that represents the description from your res/layout/list-item.xml (it probably would be TextView with id=#description).
In that case you won't see the description, but will still be available throught the SimpleAdapter.
I was fooling around with Android and my Java knowledge is limited at best (for instance, I'm perplexed by the fact that it allows inline classes!?).
My question is as follows:
I have a layout where there are three dropdown menus. I initialise all three of them inside onCreate().
The first one takes its values from a string-array. The second one, however, depends on the choice of the first one and the third one depends on the choice of the second one in turn!
I have a few string-arrays for the second Spinner but I was wondering what would be the correct way to implement a list of successively enabled Spinners. I'm tempted to just hack into it and make it work but I don't want to run the risk of it being malformed and unstable.
So for example's sakes, my case is as if I had the Google Spinner tutorial with an extra Spinner for the moons of each planet (and then maybe for craters on each of the moons).
In my resources, I have an arrays.xml such as:
<resources>
<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
<string-array name="earth">
<item>The Moon</item>
</string-array>
<string-array name="mars">
<item>Deimos</item>
<item>Phobos</item>
</string-array>
<string-array name="jupiter">
<item>Metis</item>
<item>Adrasthea</item>
<item>Amalthea</item>
<item>Thebe</item>
<item>Io</item>
<item>Europa</item>
<item>Ganymede</item>
<item>Callisto</item>
[...] etc. etc.
</resources>
In the onCreate method I initialise all three:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.planetSpinner);
Spinner spinner2 = (Spinner) findViewById(R.id.moonSpinner);
Spinner spinner3 = (Spinner) findViewById(R.id.craterSpinner);
spinner2.setEnabled(false);
spinner3.setEnabled(false);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.planets, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnPlanetSelectedListener());
}
My listener class is
public class MyOnPlanetSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So there it is. My question is where and how to implement the listeners for the successive Spinners so that they're enabled once the first one is selected. My first guess is:
public class MyOnPlanetSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
spinner2.setEnabled(true);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
But obviously this is wrong since spinner2 is not encapsulated here.
Then, the successive spinners should pick a string-array for their adapters depending on what the choice of the Spinners before it are. I tried encapsulating the spinners in the activity class, in the constructor, but the application crashes before running.
Thank you very much in advance for your help.
Use the view parameter of onItemSelected to decide which Spinner sent the onItemSelected event. From this doing the logic is simple.
Make spinner1 2 and 3 member of the class (activity)
private Spinner spinner1;
Then simply compare the view param against
if (view==this.spinner1) {
// event came from spinner 1
// create a new adapter, assign the new adapter to spinner 2
} else if (view==this.spinner2) {
// event came from spinner 2
}
Try to use this :
switch(parent.getId())
{
case R.id.spinner1:
Log.d(TAG,"spinner1");
break;
case R.id.spinner2:
Log.d(TAG,"spinner2");
break;
}
good luck !