如何从android中的ListItems获取所有editext的值并将它们传递给api

时间:2014-04-29 07:40:30

标签: android android-listview android-edittext listitem

我已经在一个ListView中做了一个活动,在ListView中,每个listItem都有一个名为“qty”的editText,可以编辑,一个textView显示“价格”,我需要的是当我编辑edittext,如果输入的值超过某些限制,则textView值将更改,之后我必须将它们作为参数传递给api,如下所示:

http://yehki.epagestore.in/app_api/updateCart.php?customer_id=41&product_id=30&quantity=90&product_id=23&quantity=90

从那里我将获得eact项目的小计,并且必须将它们设置为列表中的每个项目,有人可以帮我吗?我的代码如下。请帮助我拯救我的生命......谢谢

main.java

   package com.epe.yehki.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epe.yehki.adapter.CartAdapter;
import com.epe.yehki.backend.BackendAPIService;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.example.yehki.R;

public class CartListActivity extends Activity {
    private ProgressDialog pDialog;
    Intent in = null;
    ListView lv;
    JSONObject jsonObj;
    ArrayList<HashMap<String, String>> cartList;
    Bitmap bitmap;;
    private CartAdapter cartContent;
    JSONArray carts = null;
    ImageView back;
    TextView tv_place_order, tv_home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_cart_list);
        lv = (ListView) findViewById(R.id.cart_list);
        back = (ImageView) findViewById(R.id.iv_bak);
        tv_place_order = (TextView) findViewById(R.id.tv_place_order);
        tv_home = (TextView) findViewById(R.id.tv_home);
        cartList = new ArrayList<HashMap<String, String>>();
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // execute the cartList api()...........!!!!
        new GetCartList().execute();
        // listView ClickEvent
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                lv.removeViewAt(position);
                cartContent.notifyDataSetChanged();
            }
        });
        tv_home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                in = new Intent(CartListActivity.this, HomeActivity.class);
                startActivity(in);
            }
        });
        tv_place_order.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }

    /*
     * CART LIST PRODUCT LIST...............!!!!!!!!!
     */
    private class GetCartList extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(CartListActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            String cartUrl = Const.API_CART_LIST + "?customer_id=" + Pref.getValue(CartListActivity.this, Const.PREF_CUSTOMER_ID, "");
            BackendAPIService sh = new BackendAPIService();
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(cartUrl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    if (jsonObj.has(Const.TAG_PRO_LIST)) {

                        carts = jsonObj.getJSONArray(Const.TAG_PRO_LIST);
                        if (carts != null && carts.length() != 0) {
                            // looping through All Contacts

                            for (int i = 0; i < carts.length(); i++) {
                                JSONObject c = carts.getJSONObject(i);

                                String proId = c.getString(Const.TAG_PRODUCT_ID);
                                String proName = c.getString(Const.TAG_PRODUCT_NAME);
                                String wPrice = c.getString(Const.TAG_WHOLESALE_PRICE);
                                String rPrice = c.getString(Const.TAG_RETAIL_PRICE);
                                String qty = c.getString(Const.TAG_QUANTITY);
                                String proimg = Const.API_HOST + "/" + c.getString(Const.TAG_PRODUCT_IMG);
                                HashMap<String, String> cartProduct = new HashMap<String, String>();

                                cartProduct.put(Const.TAG_PRODUCT_ID, proId);
                                cartProduct.put(Const.TAG_PRODUCT_NAME, proName);
                                cartProduct.put(Const.TAG_PRODUCT_IMG, proimg);
                                cartProduct.put(Const.TAG_WHOLESALE_PRICE, wPrice);
                                cartProduct.put(Const.TAG_RETAIL_PRICE, rPrice);
                                cartProduct.put(Const.TAG_QUANTITY, qty);

                                cartList.add(cartProduct);

                            }
                        }
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * 
             * */

            cartContent = new CartAdapter(CartListActivity.this, cartList);
            lv.setAdapter(cartContent);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

在ListAdapter中,getItem()应该返回一个用于填充Views

的项目
@Override
public HashMap<String, String> getItem(int paramInt) {
    return cartArray.get(paramInt);
}

获取所有值,

final CartAdapter adapter = (CardAdapter) lv.getAdapter();
for (int i = 0; i < adapter.getCount(); i++) {
    final HashMap<String, String> item = adapter.getItem(i);
    final String quantity = item.get(Const.TAG_QUANTITY); // value of EditText of one row
}