0

Added Spinner data

 ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, brandWiseProductArrayList.get(position).getProductDetailsArrayList());
 holder.spinner_product_details.setAdapter(productSpinnerAdapter);

When change to this

ProductDetails selectOneDetails = new  ProductDetails("", "Select One", "", "0.00", "", "", "");
    ArrayList productDetails = brandWiseProductArrayList.get(position).getProductDetailsArrayList();
    productDetails.add(0, selectOneDetails);


    ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, productDetails);
    holder.spinner_product_details.setAdapter(productSpinnerAdapter);

*When i update my code i am getting selec one but in the every selection of the spinner Select one is added *

Taps
  • 27
  • 6
  • I have added spinner data in the spinner but want to add Some hint in the spinner as select one in the first position – Taps Oct 22 '19 at 06:17
  • refer to this https://stackoverflow.com/questions/37019941/how-to-add-a-hint-in-spinner-in-xml – L2_Paver Oct 22 '19 at 06:21
  • 1
    Possible duplicate of [How to make an Android Spinner with initial text "Select One"?](https://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – Jakir Hossain Oct 22 '19 at 06:22

5 Answers5

1

Use the add(index, element) interface method , i.e, list.add(index, element)

 brandWiseProductArrayList.get(position).getProductDetailsArrayList().add(0, "Select One");
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • Wrong 2nd argument type. Found: 'java.lang.String', required: 'com.argosupplier.Bean.ProductDetails' less... Inspection info: add (int, com.argosupplier.Bean.ProductDetails) in ArrayList cannot be applied to (int, java.lang.String) – Taps Oct 22 '19 at 06:42
  • you need to construct the `ProductDetails` element ! with the string "Select One" and then use it – Santanu Sur Oct 22 '19 at 06:45
  • Can't get it. Can you please elaborate. – Taps Oct 22 '19 at 07:32
0

Just add Select One as first child of productDetails items

ProductDetails selectOneDetails = ProductDetails("", "Select One", "", "", "", "", "") 
ArrayList productDetails = brandWiseProductArrayList.get(position).getProductDetailsArrayList();
productDetails.add(0, selectOneDetails);
ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, productDetails);
holder.spinner_product_details.setAdapter(productSpinnerAdapter);

You can modified selectOneDetails object by passing appropriate data to ProductDetails constructor.

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • in the adapter section of load spinner. java.lang.ClassCastException: java.lang.String cannot be cast to com.argosupplier.Bean.ProductDetails at com.argosupplier.Adapter.ProductSpinnerAdapter.initView(ProductSpinnerAdapter.java:71) at com.argosupplier.Adapter.ProductSpinnerAdapter.getView(ProductSpinnerAdapter.java:52) at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) – Taps Oct 22 '19 at 06:41
  • Then add your **ProductDetails** model, I will update my code. Or You have to create a **ProductDetails** object with **Select One** – Md. Asaduzzaman Oct 22 '19 at 06:43
  • public ProductDetails(String id, String name, String pn, String comRate, String nettRate, String rateWithGST, String pkgNo) { this.id = id; this.name = name; this.pn = pn; this.comRate = comRate; this.nettRate = nettRate; this.rateWithGST = rateWithGST; this.pkgNo = pkgNo; } – Taps Oct 22 '19 at 06:47
  • Thats the Constractor of my ProductDetails model class – Taps Oct 22 '19 at 06:48
  • but why to avoid selectOneDetails multiple times, Its getting multiple – Taps Oct 22 '19 at 07:04
  • You have to add more code to check that. How can I know from where you call this? If you call this multiple time then it adds multiple time. You have to check that – Md. Asaduzzaman Oct 22 '19 at 07:06
  • NO No when i am selecting any spinner item from spinner then automatically select one is incresasing – Taps Oct 22 '19 at 07:08
  • only first position of the arraylist is increasing rather than that its fine. – Taps Oct 22 '19 at 07:27
  • That is another issue. Its depends on your implementation. Accept this answer and post another with the problem mentioning some code. Thanks – Md. Asaduzzaman Oct 22 '19 at 07:34
  • My code was running successfully before add "Select One". Again when I omit this "Select One" then its work fine – Taps Oct 22 '19 at 08:46
  • Hi sir is there any way to validate the spinner – Taps Oct 22 '19 at 12:01
0

Use This

String[] newArrayList = getResources().getStringArray(R.array.yourArrayName);
List<String> listNew = new ArrayList<String>();
listNew.add("Select One");
for (int j = 0; j < newArrayList.length; j++) {
    listNew.add(newArrayList[j]);
}

Than pass new List to spinner.

mehul chauhan
  • 1,792
  • 11
  • 26
0

Two ways to do this

  1. Add a element on first position if user selects first do nothing
  2. Use Library Spinners 'https://github.com/srodrigo/Android-Hint-Spinner' like this will provides additional tag of hint.
Aravind V
  • 358
  • 2
  • 10
0

this is the way you can add.

ArrayList<String> newBrandList = new ArrayList()<>;
    newBrandList.add("Select One");
    ArrayList<brandModel> brandWiseProductArrayList = brandWiseProductArrayList.get(position).getProductDetailsArrayList()
    for(int i=0 i<brandWiseProductArrayList.size();i++){
    newBrandList.add(brandWiseProductArrayList.get(i).value);
    }

    ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, brandWiseProductArrayList);
     holder.spinner_product_details.setAdapter(productSpinnerAdapter);

or you can add on index 0

brandWiseProductArrayList = brandWiseProductArrayList.add(0,"Select One");