Spinner OnItemSelectedListener Method in Android issue - android

I have 3 spinners in my xml..
1. one is Product Name
2. one is Product Quantity.
3. and 3rd one is Discount
Now, i calling separate Listener for each one.. the problem is i want to use item quantity to calculate final price and i called spinner in all in same sequence which is i wrote Name,Quantity and Discount..
The listener of Quantity is called at last even though i wrote above Discount Spinner..i don't know what's happening..can anybody ever face this problem...Please help me..
This is my Spinner Listener:-
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
strItem=spnSelectItem.getItemAtPosition(arg2).toString(); }
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called last..
spnQty.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
strQty=spnQty.getItemAtPosition(arg2).toString();
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called before spnQty ..
spnItemDiscount.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
disc=round(Double.parseDouble(spnItemDiscount.getItemAtPosition(arg2).toString().replace("%", "")));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Why not create a separate method to perform all calculation like this:
private void calculatePrice() {
String strItem = spnSelectItem.getSelectedItem().toString();
String qty = spnQty.getSelectedItem().toString();
String discount = spnItemDiscount.getSelectedItem().toString().replace("%", "");
//perform calculate and update UI as you like
}
and then either make a single listener for all of three spinners or call them in all of them, e.g.:
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//do some work, update UI or reset qty and discount spinner or whatever
strItem = spnSelectItem.getItemAtPosition(arg2).toString();
//call calulcate method
calculatePrice();
}
});

Related

Spinner OnItemSelectedListener is not working in fragment

Hi all I can get load data's and I can view but cant select item from that spinner listthis is my output page
langeBtn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selected = langeBtn.getSelectedItem().toString();
// editor.putString("selected", selected);
// editor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

how to take the listview item from addTextChangedListener and display it in the corresponding edittext box

//Listview adapter
adapter_q = new ArrayAdapter<String>(Authors.this, R.layout.list_item_author,R.id.product_name, Quotes);
lv.setAdapter(adapter_q);
//textViewq=(MultiAutoCompleteTextView)findViewById(R.layout.list_item_author);
System.out.println("before printing");
// filtering text
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Authors.this.adapter_q.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
taking the selected item from the list and seeting it to the edittext box
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
valc= arg01.getItemAtPosition(arg21).toString();
System.out.println(valc);
inputSearch.setText(valc);
}
});
This is my search edittext box,i am getting values in it and it is filtering correctly,
but my problem is that when i select a value from the list view, the listview still remains there,
how to dismiss it as i take value from that list view?
Assuming that Quotes is your arraylist of string that you passed into the adapter, you need to do the following:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
System.out.println(Quotes.get(arg21));
inputSearch.setText(Quotes.get(arg21));
}
});
Because the data you want to display is stored in the array list instead.

same item change listener for multiple spinners

I have 8 spinners:
pl1sp = (Spinner) offersdialoglayout.findViewById(R.id.pl1offer);
pl2sp = (Spinner) offersdialoglayout.findViewById(R.id.pl2offer);
pl3sp = (Spinner) offersdialoglayout.findViewById(R.id.pl3offer);
pl4sp = (Spinner) offersdialoglayout.findViewById(R.id.pl4offer);
and
pl1scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl1scores);
pl2scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl2scores);
pl3scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl3scores);
pl4scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl4scores);
I need to set them 2 listeners. One for first 4 spinner and one for last 4. I don't want to repeat code like this:
pl1sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum(); // same method for first 4 spinners. for last 4 spinners is checkScoresSum()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
pl2sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
and so on..
So I need to write listener only twice, first for first 4 spinners and second for last 4 spinners. Thanks
define one listener for first 4 spinner
Spinner[] offerspinners = {R.id.pl1offer, R.id.pl2offer, R.id.pl3offer, R.id.pl4offer};
for(Spinner offer : offerspinners){
offersdialoglayout.findViewById(offer).
setOnItemSelectedListener(offerListener);
}
AdapterView.OnItemSelectedListener offerListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1offer:
// offer 1 related code
break;
case R.id.pl2offer:
// offer 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
than define 2nd listener with same code here
Spinner[] scorespinners = {R.id.pl1scores, R.id.pl2scores, R.id.pl3scores, R.id.pl4scores};
for(Spinner score : scorespinners){
scoresdialoglayout.findViewById(score).
setOnItemSelectedListener(scoreListener);
}
AdapterView.OnItemSelectedListener scoreListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1scores:
// score 1 related code
break;
case R.id.pl2scores:
// score 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
You can manage multiple spinners using a single listener. Please check this answer. It deals with the same issue. Hope it helps.
Follow these steps:
Implement OnItemSelectedListener in your class and override the unimplemented method onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3).
use pl1sp.setOnItemSelectedListener(this) and do this for all spinners.
Use the overridenmethod onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) like this:
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
int id = view.getId();
if(id==R.id.pl1offer || id==R.id.pl2offer || id==R.id.pl3offer ||id==R.id.pl4offer){
// do this
}
else{
//do this
}
}
What you can do is create an array containing all your spinners then use a for loop to attach each individual spinner to setItemSelected() method ie
Spinner[] spinner = {pl1sp, pl2sp, pl3sp, pl4sp};
for(Spinner individualSpinner : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
Spinner[] spinners = {pl1scores, pl2scores, pl3scores, pl4scores};
for(Spinner individualSpinner2 : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});

Android - Get Selected Item from Dynamically created Spinner

I create a Spinner depending on some rules of my app. So, its created dynamically. How can I retrieve which item was selected?
Spinner mySpinner = (Spinner)linearMoreInfo.getChildAt(1);
Using this code to retrieve the created dynamically spinner. But got no idea of how I can handle its click/select item.
Any helps?
I hope it works for you:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object obj = parent.getItemAtPosition(pos);
...
}
public void onNothingSelected(AdapterView<?> parent) { }
});
Try this Code...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
selected_item_position = pos;
selected_item = spinner.getSelectedItem().toString();
// You can insert your code...
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Adding an onclicklistener to listview (android)

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o
Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources