循环数量值的总和?

时间:2015-11-04 08:54:45

标签: android for-loop

我有这个代码..

qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
        qtySum += qtyNum;

我想将数量textTotalitems.setText(qtySum); 相加并设置为我的textview ..

我试图创建一个

JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");

cartarraylist = new ArrayList<HashMap<String, String>>();
int qtySum=0;
int qtyNum;
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
    HashMap<String, String> lmap = new HashMap<String, String>();
    JSONObject p = jsonarrayCartProducts.getJSONObject(i);
    // Retrive JSON Objects
    lmap.put("products_id", p.getString("products_id"));
    lmap.put("products_name", p.getString("products_name"));
    lmap.put("products_price", p.getString("products_price"));
    lmap.put("products_image", p.getString("products_image"));
    lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
    lmap.put("products_price_total", p.getString("products_price_total"));
    lmap.put("pcustomersid", customersid);
    lmap.put("pcountryid", countryid);
    lmap.put("customers_basket_id", p.getString("customers_basket_id"));
    // Set the JSON Objects into the array
    qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
    qtySum += qtyNum;
    cartarraylist.add(lmap);
}
textTotalitems.setText(qtySum);

并在循环中执行此操作..

    $parent.find('.js-opps-aside').stickAside();



    var thisScroll = $(window).on('scroll', enqStick);

    ...

    $(window).off('scroll', thisScroll);

并将qtySum设置为我的textview

Request Method: POST
Request URL: http://127.0.0.1:8000/

Django Version: 1.8.3
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'main')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/eisamazrouei/Desktop/django/imhere/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/eisamazrouei/Desktop/django/imhere/imhere/main/views.py" in landing_page
  36.   return render(request,'main/landing_page.html',{'loginform':loginform,'registerform':registerform})

Exception Type: UnboundLocalError at /
Exception Value: local variable 'loginform' referenced before assignment

但我收到错误,应用程序崩溃了..

这是更新的代码,我试过了..

def landing_page(request):
if request.method == "POST":
    if 'Login' in request.POST:
        loginform = LoginForm(request.POST)
        if loginform.is_valid():
            username = form.cleaned_data['l_username']
            password = form.cleaned_data['l_password']
            user = authenticate(username=username,password=password)
            login(request,user)
            return redirect(reverse('main_page',args=[request.user.username]))
        registerform = RegisterForm()
    elif 'Register' in request.POST :
        registerform = RegisterForm(request.POST)
        if registerform.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            email = form.cleaned_data['email']
            user = User.objects.create_user(username,email,password)
            login(request,user)
            return redirect(reverse('edit_page',args=[request.user.username]))
        loginform = LoginForm()
else :
    loginform = LoginForm()
    registerform=RegisterForm()
return render(request,'main/landing_page.html',{'loginform':loginform,'registerform':register form})

1 个答案:

答案 0 :(得分:3)

使用

class ViewController: UIViewController,UITextFieldDelegate  //set delegate to class 

@IBOutlet var txtValue: UITextField             //create a textfile variable 

override func viewDidLoad() {
    super.viewDidLoad()
    txtValue.delegate = self 
   YourButton.hidden=false;

            //set delegate to textfile
}


func textFieldDidBeginEditing(textField: UITextField!)
 {    //delegate method

   YourButton.hidden=true;
}

func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //delegate method
    return false

}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
  textField.resignFirstResponder()

    return true
}

而不是

textTotalitems.setText(String.valueOf(qtySum));

使用您当前的实现,您尝试将资源ID设置为TextView,因为TextView有一个重载的setText(int resId)-method。