2015-04-28 2 views
0

У меня есть ответ json, из этого ответа я получаю unitprice и boxqty, 1500 и 1 в двух разных edittext, теперь я пытаюсь, если пользователь изменит boxqty от 1 до 15, тогда цена единицы товара должна быть изменена с 1500 1470, после мой фрагмент кода,Как сравнить целое поле с полем json?

{"status":"success","clientproduct":[{"pid":"4","name":"kangan pair","unitprice":"1500","boxqty":"1","bulkprice":[{"minqty":"10","price":"1500"},{"minqty":"15","price":"1470"},{"minqty":"20","price":"1460"}]}]} 

MainActivity.java

 @Override 
    protected String doInBackground(String...args) { 
     //Check for success tag 
     //int success; 
     Looper.prepare(); 

     try { 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("cid",letss)); 
      params.add(new BasicNameValuePair("action", "clientproduct")); 

      System.out.println("su gayu server ma????"+params); 

      Log.d("request!", "starting"); 
      // getting product details by making HTTP request 
      JSONObject json = jsonParser.makeHttpRequest (
       FEEDBACK_URL, "POST", params); 
      //check your log for json response 
      Log.d("Login attempt", json.toString()); 
      if (json != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(json.toString()); 
        // Getting JSON Array node 
        clientproduct = jsonObj.getJSONArray(CLIENTPRODUCT_LIST); 
        // looping through All Contacts 
        for (int i = 0; i < clientproduct.length(); i++) { 
         ck = clientproduct.getJSONObject(i); 
         unitp=ck.getString("unitprice"); 
         System.out.println("Unit ni price"+unitp); 
         boxq=ck.getString("boxqty"); 
         System.out.println("Box ni quantity "+boxq); 

         bulkprice = ck.getJSONArray(BULKPRICE_LIST); 

         allqtys=new ArrayList<Integer>(); 
         for (int b=0 ; b < bulkprice.length(); b++) 
         { 
          jo = bulkprice.getJSONObject(b); 

          minimum_qty=jo.getInt("minqty"); 
         allqtys.add(minimum_qty); 
          /* allqtys=new ArrayList<String>(); 
          allqtys.add(minimum_qty.toString()); 
          System.out.println("All MinQuantitiy"+minimum_qty);*/ 
        System.out.println("All MinQuantitiy"+allqtys); 
         System.out.println("MinQuantitiy"+minimum_qty); 

          pricess=jo.getString("price"); 
          System.out.println("Box price "+pricess); 

         } 
         /*conrs=Integer.parseInt(allqtys.toString()); 
         System.out.println("Integer converted arrray"+conrs);*/ 

         /* newList = new ArrayList<Integer>(allqtys.size()) ; 
         for (String myInt : allqtys) 
         { 
          newList.add(Integer.valueOf(myInt)); 
         } 
         System.out.println("allqtys "+newList);*/ 

         System.out.println("Not Null"); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 


     return json.getString(FEEDBACK_SUCCESS); 

    }catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

    // After completing background task Dismiss the progress dialog 

    protected void onPostExecute(String file_url) { 
     //dismiss the dialog once product deleted 
     pDialog.dismiss(); 

     /* ArrayList<Integer> allval=new ArrayList<Integer>(); 
     // allval.add(minimum_qty); 
     System.out.println("Integer converted arraylist"+allval);*/ 


     autoproduct.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       // uprice.setText(unitp); 
        //bxqtyy.setText(boxq); 
      } 
     }); 


     bxqtyy.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
       if(Integer.parseInt(bxqtyy.getText().toString()) > Integer.parseInt(allqtys.get(index))) 
       { 
        if(bxqtyy.getText().equals(null)) 
        { 
         uprice.setText(unitp); 
        } 
        uprice.setText("1470"); 
        //System.out.println("lets check"); 
       } 
       else 
       { 
        uprice.setText("1500"); 
        System.out.println("lets check"); 
       } 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 

}} 
+0

просто скрывает тип данных до целого. и как эта строка работает нормально "if (bxqtyy.getText(). ToString()> minimum_qty.compareTo (BOX_QTY))"? – Shivansh

+0

no его не работает if (bxqtyy.getText(). ToString()> minimum_qty.compareTo (BOX_QTY)) – chris

+0

и почему вы используете minimum_qty.compareTo (BOX_QTY)? – Shivansh

ответ

0

Эта линия if(bxqtyy.getText().toString() > minimum_qty.compareTo(BOX_QTY)) не работает, потому что вы не можете сравнить строку с целым числом, а именно «>» оператор будет бросать компилировать временная ошибка.

Используйте этот

Integer.parseInt(your text here); 

и правильный код для него

if(Integer.parseInt(bxqtyy.getText().toString()) > minimum_qty.compareTo(BOX_QTY)) 
    { 

    } 

надеюсь, что это поможет.

+0

да, но как изменится мой прайс-лист? – chris

+0

, вы должны создать условия, основанные на нем, и от имени на условиях вы будете изменять единицу стоимости – Shivansh

+0

if (Integer.parseInt (bxqtyy.getText(). ToString())> minimum_qty.compareTo (BOX_QTY)) { uprice. setText ("// используем вашу логику // l"); } – Nivedh

Смежные вопросы