How to get the number of Items in multiple Spinner View and calculate the total price
android, android-listview, android-spinner
Solution
I just update your code without creating new. First I will answer the three questions.
- The spinners are resetting after scrolled because the adapter call `getView()` method and they are initializing to default value again. To avoid this and get back your previously selected values, what you have to do is save them in a map and assign back. In following code "`selectedItem`" is the map I used to store selected amounts for each spinner.
- To get total amount you can use another map which is used to store total price of each spinner. In the following code I used "`totalPrices`" for it. Inside `onItemSelected()` we can put total amount for specific spinner. Inside the onclick() method of "`BUY`" button you can access this map by calling `getTotalPrice()` method is adapter class.
- This is quiet easy. We don't have to get separate array. We can get this information using "`selectedItem`" map which used to store selected values of spinners. Same as the total price, you can get this map using `getTotalItems()`.
Also in your price calculation inside `onItemSelected()` method you used "`i`" to multiply the item price. It should be `(i+1)`. Because "i" is the position of the list in spinner. It starts from `0`.
This is the Base_Adapter class
public class Base_Adapter extends BaseAdapter{
ImageView image;
TextView name, price;
private HashMap<Integer,Integer> selectedItem=new HashMap<Integer, Integer>();
private HashMap<Integer, String> totalPrices=new HashMap<Integer, String>();
Context context ;
ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
RelativeLayout R_Layout;
Activity activit;
public Base_Adapter(Context context , ArrayList<ItemDetails> li) {
// TODO Auto-generated constructor stub
this.context = context;
IDetails = li;
}
public void setLayout(Activity activity, RelativeLayout layout){
R_Layout = layout;
this.activit = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return IDetails.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public HashMap<Integer,Integer> getTotalItems(){
return selectedItem;
}
public HashMap<Integer, String> getTotalPrice(){
return totalPrices;
}
//////// Get View For Spinner////
@Override
public View getView(final int position, View CV, ViewGroup parent) {
// TODO Auto-generated method stub
final ItemDetails item = IDetails.get(position);
int min =1;
int max = Integer.parseInt(item.stock());
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.base_adapter, parent,false);
ArrayList<String> A_list= new ArrayList<String>();
for(int i=1;i<=max;i++)
{
A_list.add("Number of Items :"+i);
}
ImageView image=(ImageView) rowView.findViewById(R.id.item_image);
TextView nameTextView=(TextView) rowView.findViewById(R.id.item_name);
nameTextView.setText(item.title());
final TextView price=(TextView) rowView.findViewById(R.id.item_price);
Spinner quantity=(Spinner) rowView.findViewById(R.id.items);
ArrayAdapter<String > quatity=new ArrayAdapter<String>(context, R.layout.spinner_textview, R.id.item_list, A_list);
quantity.setAdapter(quatity);
if(selectedItem.get(position) != null){
//This should call after setAdapter
quantity.setSelection(selectedItem.get(position));
}
quantity.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
{
if(i>0){
selectedItem.put(position, i);
i=i+1;
float cal=Float.parseFloat(item.price());
float cal3=cal*i;
price.setText(""+String.format("%.2f", cal3).replace(".", ","));
item.Totalprice= cal3;
}
else{
price.setText(""+String.format("%.2f", Float.parseFloat(item.price())).replace(".", ","));
}
totalPrices.put(position, price.getText().toString());
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
return rowView;
}
}
So inside your activity class implement `onclick` method for the "`BUY`" button.
public void getTotal(View view){
HashMap<Integer, Integer>totalItems=adapter.getTotalItems();
HashMap<Integer, String> totalPrices=adapter.getTotalPrice();
for(int i=0;i<totalItems.size();i++){
if(totalItems.get(i) != null){
System.out.println("Spinner No"+(i+1)+"Items :"+(totalItems.get(i)+1));
System.out.println("Spinner No "+(i+1)+"total price "+totalPrices.get(i));
}else{
System.out.println("Spinner No"+(i+1)+"Items : 1");
}
}
Problem
I am implementing spinner in a list view (Multiple Spinners) for different products with their images on left and prices on right. User has choice to select number (quantity) of each product. This work is going in a Class that extends from BaseAdapter according to my needs. In getView of the spinner I set the spinner view. Now I want: 1) when the user select an Item in a Spinner, the price of that item is calculated as total and the TextView text on the right is set to that total price. Now this is working well, but when I scroll up the list, the Spinner changes its value to the old one (i.e., the value at position 0) not the new one total. 2) The other thing i want to do is to keep all these values that came from different spinners in an array, so that at the end all different spinners values are further calculated as total (at first i was calculating the single product values, say the price of that product is 50$ and the user selected that he want 20 pieces of that product so totall=20x50 ). 3) And one other thing i want is to get the number of items selected in one spinner. And in the same way to keep these numbers of each spinner in another array, so that at the end these all are calculated as total number of all products. Below is the image and sorry as my question gone too long, but i really want to solve this out. And if you want more things from me to post please do tell me. When I select Items When I scroll the screen all values in spinners and the prices in the text views reset to initial position Here is my code ``` public class Base_Adapter extends BaseAdapter { ImageView image; TextView name, price; Context context ; ArrayList<ItemDetails> IDetails; //The item class which have methods and fields RelativeLayout R_Layout; Activity activit; public Base_Adapter(Context context , ArrayList<ItemDetails> li) { this.context = context; IDetails = li; } public void setLayout(Activity activity, RelativeLayout layout){ R_Layout = layout; this.activit = activity; } @Override public int getCount() { // TODO Auto-generated method stub return IDetails.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } //////// Get View For Spinner//// @Override public View getView(final int position, View CV, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater infleter = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(CV == null) { CV = infleter.inflate(R.layout.base_adapter, null); } final ItemDetails item = IDetails.get(position); int min =1; int max = Integer.parseInt(item.totall_Available()); ArrayList<String> A_list= new ArrayList<String>(); for(int i=1;i<=max;i++) { A_list.add("Number of Items :"+i); } image = (ImageView) CV.findViewById(R.id.Item_image); name = (TextView) CV.findViewById(R.id.item_name); price = (TextView) CV.findViewById(R.id.item_price); final Spinner quantity = (Spinner) CV.findViewById(R.id.items); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_textview, A_list); quantity.setAdapter(adapter); //String selectedItem = (String) quantity.getSelectedItem(); name.setText(item.name()); /// ItemClick///// quantity.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3) { if(i>0){ float cal=Float.parseFloat(item.Fisrtprise()); float cal3=cal*i; price.setText(""+String.format("%.2f", cal3).replace(".", ",")); String s = Float.toString(cal3); item.Totalprice=s; } else{ price.setText(""+String.format("%.2f", Float.parseFloat(item.Fisrtprise())).replace(".", ",")); } } public void onNothingSelected(AdapterView<?> arg0){ } }); return CV; } ``` And this is the IDetails Class ``` @SuppressWarnings("serial") public class IDetails implements Serializable { ContentValues colmnValues; private int no_of_items; public float Totalprice; public IDetails(ContentValues values ) { colmnValues = values; } public String title() { return getValue(colmnValues.get("title")); } public void setNo_of_items(int no_of_items) { this.no_of_items = no_of_items; } public int getNo_of_items() { return no_of_items; } public void setTotalprice(float Totalprice) { this.Totalprice = Totalprice; } public float getTotalprice() { return Totalprice; } public String imageUrl() { return getValue(colmnValues.get("imageUrl")); } public String pprice() { return getValue(colmnValues.get("Realprice")); } public String stock() { return getValue(colmnValues.get("stock")); } private String getValue(Object obj){ if(obj == null){ return ""; } return (String) obj; } } ```