i am new to android studio and I am stuck in a scenario, I have a recylerview where all data visible and i want to show data only base on selected city from downlist. any suggestion that how i can achieve that. also guide me that how can I use spinner for filtering
In Spinner to call a method when a item is selected you can use setOnItemSelectedListener()
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// code which needs to be executed when item selected
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
For more details you can visit : https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener
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.
I have a OnItemSelectedListener for my Spinner, but it is not called when the selected item is the same as the previous one.I also implemented OnClickListener but it doesn't worked. I have found some solution. But I don't know how can I use it. I need to catch every-time a user click on an item.
JAVA CODE:
spn_filter_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
XML CODE:
<Spinner
android:id="#+id/spn_filter_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
If you select an different item onItemSelected gets called.
If you don't select a different one it means you have not selected an item, then onNothingSelected gets called.
spn_filter_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
CharSequence message = "called when the selected item is the same as the previous one";
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
}
});
This question already has answers here:
Android - How to create clickable listview?
(5 answers)
Closed 8 years ago.
I currently have a ListView id=listview, which I am populating with an ArrayList id=myarraylist. Here is how I do it:
final ListView listview = (ListView) findViewById(android.R.id.list);
myarraylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
myarraylist.add(...And then some other stuff I add...);
//This is the part I want to focus on
myarraylist.add("Reset High Scores");
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Now, I want the entry Reset High Scores in my ListView to be clickable, and when it is clicked, I want it to do Some Stuff. I know for buttons all you do is set an onClickListener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
..Some Stuff..
}
});
My question is, how can I make Some Stuff happen when the element Reset High Scores is clicked in the ListView
Edit: This is what I have so far
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Clear High Scores")) {
//Stuff
}
}
});
ListView, just like Button, has the ability to set a click listener, but for ListView, the name of the method is setOnItemClickListener(). Instead of using OnClickListener, though, it uses a class called OnItemClickListener, which is more advantageous for using with ListView. The method looks as follows:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
// some stuff
}
}
)
Now the advantage of this is that you can check what's contained at the position you've pressed, so the way you could tackle your problem could look something like this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Reset High Scores"))
{
// Do what you want
}
}
});
/**
* Creates a new instance
*/
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
super.onListItemClick(l, v, position, id);
//Do stuff
}
that should get ya started... Andrew is right though.
I have a spinner that opens programaticly, and when the user chose an option from the spinner, it closes... is there a way to be notified, or a listener that tells you, when the user chose his choice?
the onItemSelected gets the default item that is chosen automaticly when the spinner is open.
set setOnItemSelectedListener to your spinner...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object obj = (Object) parent.getSelectedItem();
//get clicked position from position
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//this method is call when nothing choosed by you
}
});
Add an OnItemSelectedListener to your Spinner.
I am not sure if I understand your question correctly but let me try to answer it. The normal and straight forward way would be to add an OnItemSelectedListener to the spinner i.e
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Do whatever you want here
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
but this is such a basic thing that I feel like a fool for pointing it out. Anyway here is a tutorial on spinner from Android Developer resources. It creates the listener in step 5 and adds it to spinner on step 6.
in an android website, I found an article about how to create a text entry widget that provides auto-complete suggestions. (Following is the link to the site; and it shows all the codes).
http://developer.android.com/resources/tutorials/views/hello-autocomplete.html
Can anyone please tell me how I can capture the input entered by the user? For example, if the user chooses “Canada”, is there a way I can know the result in the “HelloAutoComplete.java” activity? Any help would be greatly appreciated.
public class HelloAutoComplete extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
String[] countries = getResources().getStringArray(R.array.countries_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getItemAtPosition(i).toString().equals("Canada")) {
Toast.makeText(getApplicationContext(), "Result Canada", Toast.LENGTH_SHORT).show();
} //Does not get an out put when I select Canada.
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
} }
textView.getText();
textView.getText().toString(); // If you need an actual String
Currently your code does not hook up the listener to the text view.
You need to either (a) use an immediate listener:
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getItemAtPosition(i).toString().equals("Canada")) {
Toast.makeText(getApplicationContext(), "Result Canada", Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Or (b) if you want to use the activity as the listener (I wouldn't) have it implement the interface and set the itemSelectedListener to this. But yuck.
To set the selected text into another text element, a few changes must be made. First, the layout must now include the autocomplete "component", and the additional text view. We set the "parent" layout to vertical orientation, create a new horizontal layout for the autoselect stuff, and add a new text view.
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:padding="5dp">
<LinearLayout a:orientation="horizontal"
a:layout_width="fill_parent"
a:layout_height="wrap_content">
<TextView a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:text="Country"/>
<AutoCompleteTextView a:id="#+id/autocomplete_country"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView a:id="#+id/selected_country"
a:layout_height="wrap_content"
a:layout_width="fill_parent"/>
</LinearLayout>
The activity gets a new TextView instance property, which I'm calling selectedCountry. I'm not showing its declaration. The onCreate method looks it up by ID, and the select listener just updates it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Text view for selected country.
selectedCountry = (TextView) findViewById(R.id.selected_country);
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedCountry.setText(adapterView.getItemAtPosition(i).toString());
}
public void onNothingSelected(AdapterView<?> adapterView) { }
});
}
The best way would be to set an "item selected listener" which will notify you when the user has picked something from the available entries. See the docs. With this you can get the item from the adapter that they picked (and, conversely, whatever you defined for that item's text).
Example:
textView.setOnItemSelectedListener( new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View selectedView,
int selectedPosition, long selectedId) {
//do stuff here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
Try this:
textView.getText().toString();
Then display it through Toast.
Use :
textView.getText().toString()