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
}
});
Related
I am building a simple grocery ordering App with firebase, where I want two spinners and a button.
One Spinner for the item name and item price tag. (For example - 'Chips -5rs-')
Second spinner for the quantity. (for Example - '5 packet')
A button for taking the values from the spinners and putting it to the TextView below. (One item after the other.)
For Example, after adding several items from the spinners, the textview below should look like this. -
Chips -5rs- 5 packet
Biscuits -20rs- 20 packet
Noodles -15rs- 2 packet
and so on..
How do I actually implement it? and if anyone could help me with actual java codes, I would be very thankful. I've also attached a picture below of how I am trying to Implement it. I am new to Android Development and I have keen interest in it.. any help would be deeply appreciated..
:) Thankyou ..
This is the Screenshot of what I am trying to approach.
Define your string global
String priceTag,quantityTag;
then get value from spinner like this
yourSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
same as getting value from your second spinner
yourSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
and show data on button click like this
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
yourtextViewId.setText(priceTag " - " quantityTag);
}
});
You can do it this way,
Firstly grab the values from both the spinners
//From spinner 1
String s1_val = spinner1.getSelectedItemsAsString();
//From spinner 2
String s2_val = spinner2.getSelectedItemsAsString();
Concat the values and set it to the text view.
//concat and set to textView
textView.setText(s1_val.concat(s2.val()));
Perform this on the onClick of Add Items button.
My problem is : I want to have 3 different spinners, that displays the same type of object, and I want to be able to identify from which spinner I get the data, for example :
In spinner 1, the user selected "potato"
In spinner 2, the user selected "tomato"
In spinner 3, the user selected "fries"
But I can only get "the user selected [...]", since I don't know how to tell from which spinner I got the data.
I was wondering if there was a way to do that on the onItemSelected(AdapterView parent, View view, int position, long id) method ?
The View view is most likely the spinner that made the selection. If you set the .tag = 1 of each spinner to different numbers at the start of your program, then you will be able to look at what tag value is passed to the onItemSelected method:
if (view.tag == 1)...
Probably you have set a common listener for all the spinners, so you can distinguish which spinner was selected by checking parent.getId():
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.spinner1:
//your code here
break;
case R.id.spinner2:
//your code here
break;
case R.id.spinner3:
//your code here
break;
}
}
As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});
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()
How would you go about making a spinner populate another spinner based on the first spinners selection?
So for example:
Spinner1 items are vegetarian or meat eater.
<string-array name="spinnerarray_veg_meat">
<item >Vegetarian</item>
<item >Meat eater</item>
</string-array>
Spinner2 would then need to display either vegetarian meal names or meat eater ones depending on spinner1's selection.
To do this you'll have to set a OnItemSelectedListener on your first Spinner to populate the second Spinner programmatically.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// Populate the Spinner2 with different values
} else {
// Populate the Spinner2 with different values
}
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
There are a number of ways to do it. One being, create an Array of meat items and one of vegetable items. In onItemSelected() of spinner1 set the adapter for spinner2 according to the position
Spinner Docs
This link has many useful functions and properties available to Spinners