android 购物车项目计数增加/减少&添加到购物车

l0oc07j2  于 2023-02-27  发布在  Android
关注(0)|答案(3)|浏览(196)

大家好,我正在使用自定义列表视图从服务器获取数据,并在列表视图中显示。我能够获取数据,并在列表视图中显示它,但我不知道如何实现listitem中按钮的click事件。有两个按钮可以增加和减少数量。我的clicklistener可以工作,但工作方式不正确。当我单击增加或减少一个产品时,它应该被添加到购物车中。请帮我纠正这个问题。我确实在里面搜索了太多的帖子,但是看不懂...
下面是我的适配器类代码:

public class ProductsAdapter extends BaseAdapter {
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

private Context context;
private ArrayList<HashMap<String, String>> postItems;
public SharedPreferences settings;
public final String PREFS_NAME = "Products";

DisplayImageOptions options;
ImageLoaderConfiguration imgconfig;

private static int _counter = 0;
private String _stringVal;

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist){
    this.context = context;

    File cacheDir = StorageUtils.getCacheDirectory(context);
    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.loading)
    .showImageForEmptyUri(R.drawable.loading)
    .showImageOnFail(R.drawable.loading)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .displayer(new SimpleBitmapDisplayer())
    .imageScaleType(ImageScaleType.EXACTLY)
    .build();

    imgconfig = new ImageLoaderConfiguration.Builder(context)
    .build();
    ImageLoader.getInstance().init(imgconfig);

    postItems = arraylist;
    settings = context.getSharedPreferences(PREFS_NAME, 0);     

}

@Override
public int getCount() {
    return postItems.size();
}
@Override
public Object getItem(int position) {       
    return postItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
            context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_products, null);   
        }
        final HashMap<String, String> map = postItems.get(position);

        ImageView imgProduct = (ImageView)convertView.findViewById(R.id.proimage);
        ImageLoader.getInstance().displayImage(ConstValue.PRO_IMAGE_BIG_PATH+map.get("image"), imgProduct, options, animateFirstListener);

        TextView txtTitle = (TextView)convertView.findViewById(R.id.proTitle);
        txtTitle.setText(map.get("title"));

        TextView txtPrice = (TextView)convertView.findViewById(R.id.txtprice);
        txtPrice.setText(map.get("price"));

        TextView textDiscount = (TextView)convertView.findViewById(R.id.textDiscount);
        textDiscount.setVisibility(View.GONE);

        TextView txtDiscountFlag = (TextView)convertView.findViewById(R.id.textDiscountFlag);
        txtDiscountFlag.setVisibility(View.GONE);

        TextView textCurrency = (TextView)convertView.findViewById(R.id.textCurrency);
        textCurrency.setText(map.get("currency"));

        final TextView value = (TextView)convertView.findViewById(R.id.textView3);

        Button txtminus = (Button) convertView.findViewById(R.id.minus);

        txtminus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("src", "Decreasing value...");
            _counter--;
            _stringVal = Integer.toString(_counter);
            value.setText(_stringVal);

            if (_counter < 0)
            {
                value.setText("0");
            }

        }
        });

        Button txtplus  = (Button) convertView.findViewById(R.id.plus);
        txtplus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("src", "Increasing value...");
            _counter++;
            _stringVal = Integer.toString(_counter);
            value.setText(_stringVal);
        }
        });

        if(!map.get("discount").equalsIgnoreCase("") && !map.get("discount").equalsIgnoreCase("0")){
            Double discount = Double.parseDouble(map.get("discount"));
            Double price = Double.parseDouble(map.get("price"));
            Double discount_amount =  discount * price / 100;

            Double effected_price = price - discount_amount ; 
            //txtPrice.setBackgroundResource(R.drawable.strike_trough);
            txtPrice.setPaintFlags(txtPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            textDiscount.setVisibility(View.VISIBLE);
            textDiscount.setText(effected_price.toString());

            txtDiscountFlag.setVisibility(View.VISIBLE);
            txtDiscountFlag.setText(discount+"% off");
        }

        TextView txtUnit = (TextView)convertView.findViewById(R.id.txtunit);
        txtUnit.setText(map.get("gmqty"));

        TextView txtgm = (TextView)convertView.findViewById(R.id.txtgm);
        txtgm.setText(map.get("unit"));

        int a = Integer.parseInt(map.get("total_qty_stock").toString());
        int b = Integer.parseInt(map.get("consume_qty_stock").toString());

        int result = a - b;

        TextView txtstock = (TextView)convertView.findViewById(R.id.stock);
        txtstock.setText(String.valueOf(result)+" "+map.get("type")+" In Stock");
        //txtstock.setText(map.get("stock")+" "+map.get("type")+" In Stock");

    return convertView;
}

}

这里是我的产品活动代码
公共类ProductsActivity扩展了ActionBarActivity {

public SharedPreferences settings;
public ConnectionDetector cd;
static ArrayList<HashMap<String, String>> products_array;
ProductsAdapter adapter;
ListView products_listview;
DisplayImageOptions options;
ImageLoaderConfiguration imgconfig;
ProgressDialog dialog;

TextView txtcount;
Button btn;

private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
HashMap<String, String>  catMap;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0);
    cd = new ConnectionDetector(getApplicationContext());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_products);

/*  ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(true);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
    mTitleTextView.setText("My Own Title");

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true); */

    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0);
    cd=new ConnectionDetector(this);

    File cacheDir = StorageUtils.getCacheDirectory(this);
    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.loading)
    .showImageForEmptyUri(R.drawable.loading)
    .showImageOnFail(R.drawable.loading)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .displayer(new SimpleBitmapDisplayer())
    .imageScaleType(ImageScaleType.NONE)
    .build();

    imgconfig = new ImageLoaderConfiguration.Builder(this)
    .build();
    ImageLoader.getInstance().init(imgconfig);

    ArrayList<HashMap<String,String>> categoryArray = new ArrayList<HashMap<String,String>>();
    try {
        categoryArray = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("categoryname", ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>())));
    }catch (IOException e) {
            e.printStackTrace();
    }

    catMap = new HashMap<String, String>();
    catMap = categoryArray.get(getIntent().getExtras().getInt("position"));

    products_array = new ArrayList<HashMap<String,String>>();
    try {
        products_array = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("products_"+catMap.get("id"), ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>())));
    }catch (IOException e) {
            e.printStackTrace();
    }

    products_listview = (ListView)findViewById(R.id.listView1);
    adapter = new ProductsAdapter(getApplicationContext(), products_array);
    products_listview.setAdapter(adapter);

    TextView txtTitle = (TextView)findViewById(R.id.catname);
    txtTitle.setText(catMap.get("name"));

    txtcount = (TextView)findViewById(R.id.textcount);

    btn = (Button) findViewById(R.id.button);

    products_listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, final int position,
                                long id) {
            // TODO Auto-generated method stub

            try {

                settings.edit().putString("selected_product", ObjectSerializer.serialize(products_array.get(position))).commit();


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent intent = new Intent(ProductsActivity.this, ProductdetailActivity.class);
            intent.putExtra("position", position);
            startActivity(intent);

        }
    });

    new loadProductsTask().execute(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



public class loadProductsTask extends AsyncTask<Boolean, Void, ArrayList<HashMap<String, String>>> {

    JSONParser jParser;
    JSONObject json;
    String count;
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        if (result!=null) {
            //adapter.notifyDataSetChanged();
        }   
        try {
            settings.edit().putString("products_"+catMap.get("id"), ObjectSerializer.serialize(products_array)).commit();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        txtcount.setText(count);
        adapter.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        super.onCancelled(result);
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Boolean... params) {
        // TODO Auto-generated method stub

        try {
            jParser = new JSONParser();

            if(cd.isConnectingToInternet())
            {
                String urlstring = ConstValue.JSON_PRODUCTS+"&id="+catMap.get("id");
                json = jParser.getJSONFromUrl(urlstring);
                count = json.getString("count");
                if (json.has("data")) {
                    if(json.get("data") instanceof JSONArray){
                        JSONArray jsonDrList = json.getJSONArray("data");
                        products_array.clear();
                        for (int i = 0; i < jsonDrList.length(); i++) {
                            JSONObject obj = jsonDrList.getJSONObject(i);
                            put_object(obj);
                        }
                    }else if(json.get("data") instanceof JSONObject){
                        put_object(json.getJSONObject("data"));
                    }
                }
            }else
            {
                Toast.makeText(ProductsActivity.this, "Please connect mobile with working Internet", Toast.LENGTH_LONG).show();
            }

            jParser = null;
            json = null;

            } catch (Exception e) {
                // TODO: handle exception

                return null;
            }
        return null;
    }

    public void put_object(JSONObject obj){
        HashMap<String, String> map = new HashMap<String, String>();

        try {
        map.put("id", obj.getString("id"));
        map.put("title", obj.getString("title"));
        map.put("slug", obj.getString("slug"));
        map.put("description", obj.getString("description"));       
        map.put("image", obj.getString("image"));

        map.put("price", obj.getString("price"));
        map.put("currency", obj.getString("currency"));
        map.put("discount", obj.getString("discount"));
        map.put("cod", obj.getString("cod"));       
        map.put("emi", obj.getString("emi"));       
        map.put("status", obj.getString("status"));     

        map.put("gmqty", obj.getString("gmqty"));       
        map.put("unit", obj.getString("unit"));     
        map.put("deliverycharge", obj.getString("deliverycharge"));     
        map.put("tax", obj.getString("tax"));       
        map.put("category_id", obj.getString("category_id"));

        map.put("on_date", obj.getString("on_date"));

        map.put("stock", obj.getString("stock"));
        map.put("type", obj.getString("type"));
        map.put("total_qty_stock", obj.getString("total_qty_stock"));
        map.put("consume_qty_stock", obj.getString("consume_qty_stock"));

        products_array.add(map);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

mm9b1k5b

mm9b1k5b1#

这条线

private static int _counter = 0;

因此,当您单击列表中的 any 项时,您只跟踪适配器中所有项的一个编号,而不是每个 * 单独 * 项。
我建议你定义一些面向对象的设计来获得CartProduct类,这将允许你以比将HashmapMap到ListView更干净的方式跟踪列表中的项目及其细节和数量。
有了Product类之后,我建议您研究一下如何实现ArrayAdapter<Product>

mrfwxfqh

mrfwxfqh2#

你可以在你的活动类中用接口覆盖方法实现一个接口,并将该接口作为参数传递给适配器构造函数。2让我们创建一个名为increment的接口。

public interface Increment {
  void addCount(boolean ADD_FLAG);
}

在你的活动中实现这一点。

Class CartActivity implements Increment {

@Override
public void addCount(boolean ADD_FLAG) {
if (ADD_FLAG) {
//If true increment..
} else{
//If false decrement..
}
}

将接口作为参数传递给适配器构造函数,如下所示,使用另一个参数。

Increment increment = null;

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, Increment increment) {
  this.increment = increment;
}

最后是点击监听器。

//Add this line to increment..
increment.addCount(true);

//Add this line to decrement..
    increment.addCount(false);

希望这有帮助:)

f87krz0w

f87krz0w3#

创建一个接口,例如:

interface QuantityChangeListener {
    void onQuantityChanged(HashMap<String, String> map);
}

实现这个接口,你创建适配器像上面.

ProductsAdapter adapter = new ProductAdapter(this, "your list",
    new QuantityChangeListener() {
         @Override
         public void onQuantityChanged(HashMap<String, String> map) {
              // add item to cart
         }
     }
);

将此接口传递给ProductsAdapter类的构造函数。

private QuantityChangeListener listener;
public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, QuantityChangeListener listener){
    this.listener = listener;
    // do other stuff
}

txtplus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("src", "Increasing value...");
        _counter++;
        _stringVal = Integer.toString(_counter);
        value.setText(_stringVal);
        listener.onQuantityChanged(map);
    }
});

相关问题