user13657604
user13657604

Reputation:

for loop not giving me proper result in android

Hi in the below code I am getting the productvalue,productid .Now if I am selected the autocomplete textview I am getting the string that string I am comparing and set the text.

But everytime it is giving me else condition means 1000.

can any one help me where I did the mistake

java1 method:

   private ArrayList<SynFields> productList=new ArrayList<>();

               for (SynFields synFields1 : synFields) {

                            String name = synFields1.getName();
                            values = synFields1.getValue();

                            if (name.equals("productid")) {
                                try {
                                    Gson gson = new Gson();
                                    String strJson = gson.toJson(values);
                                    JSONObject jsonObject = new JSONObject(strJson);
                                    String v = jsonObject.getString("label");
                                    String id = jsonObject.getString("value");
                                    productValue = v;
                                    productID = id;

                                    SynFields synFields2 = new SynFields(name, productID, productValue);
                                    productList.add(synFields2);
                                } catch (Exception ex) {
                                    Log.e("SalesStageFragment", "Exception is : " + ex.toString());
                                }

                            }

                        }

java2 method:

 autoproduct_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               productnames=adapter.getItem(position);

               for (SynFields synFields2 : productList) {
                   if (synFields2.getLabel().equals(productnames)) {
                       textunitprice.setText("2000");
                   }else {
                       textunitprice.setText("1000");
                   }
               }




           }
       });

Upvotes: 0

Views: 66

Answers (2)

user13657604
user13657604

Reputation:

Yes I resolved My issue First I added break and check with boolean .Now it is working perfect.

   autoproduct_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               productnames=adapter.getItem(position);

               boolean isSuccess = false;
               for (SynFields synFields2 : productList) {
                   Log.d("productList", String.valueOf(productList.get(position).getLabel()));
                   if (synFields2.getLabel().equalsIgnoreCase(productnames)) {
                       textunitprice.setText(productnames);
                       isSuccess = true;
                       break;
                   }else {
                       textunitprice.setText("");
                   }
               }




           }
       });

Upvotes: 1

ALearner
ALearner

Reputation: 522

you can check whether there is a problem with the Caps or small fonts for the strings from 'adapter' and the 'productlist'. So to handle that you can use 'equalsIgnoreCase' in place of 'equals' for the below logic,

if (synFields2.getLabel().equalsIgnoreCase(productnames)) {
    textunitprice.setText("2000");
 }

Upvotes: 0

Related Questions