存储数据并传递到下一个活动暂停和恢复活动

时间:2016-08-29 12:56:24

标签: java android shopping-cart

我正在尝试制作一个基本的购物车Android应用程序

以下是代码的外观

Items.java

public class Items extends AppCompatActivity {

private ListView lvUsers;
private ProgressDialog dialog;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub);

    Intent intent = getIntent();
    int subCategoryId = intent.getIntExtra("parameter_name", 1);
    dialog = new ProgressDialog(this);
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.setMessage("Loading, please wait.....");

    lvUsers = (ListView) findViewById(R.id.lvUsers);
    String url = "http://146.185.178.83/resttest/subCategories/" + subCategoryId  +"/items/";
    new JSONTask().execute(url);

}

public class JSONTask extends AsyncTask<String, String, List<ItemModel> > {

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        dialog.show();
    }

    @Override
    protected List<ItemModel> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line ="";
            while ((line=reader.readLine()) !=null){
                buffer.append(line);
            }

            String finalJson = buffer.toString();
            JSONArray parentArray = new JSONArray(finalJson);


            List<ItemModel> itemModelList = new ArrayList<>();

            Gson gson = new Gson();
            for (int i = 0; i < parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                ItemModel itemModel = gson.fromJson(finalObject.toString(), ItemModel.class);
                itemModelList.add(itemModel);
            }
            return itemModelList;

        }catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if(connection !=null) {
                connection.disconnect();
            }
            try {
                if (reader !=null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(List<ItemModel> result) {
        super.onPostExecute(result);
        dialog.dismiss();
        ItemAdapter adapter = new ItemAdapter(getApplicationContext(), R.layout.row_item, result);
        lvUsers.setAdapter(adapter);
    }
}
public class ItemAdapter extends ArrayAdapter {

    public List<ItemModel> itemModelList;
    private int resource;
    private LayoutInflater inflater;
    public ItemAdapter(Context context, int resource, List<ItemModel> objects) {
        super(context, resource, objects);
        itemModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

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

        ViewHolder holder = null;

        if(convertView == null){
            holder = new ViewHolder();
            convertView=inflater.inflate(resource, null);
            holder.tvItemName = (TextView) convertView.findViewById(R.id.tvItemName);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvItemName.setText(itemModelList.get(position).getItemName());
        holder.tvPrice.setText("Rs " + itemModelList.get(position).getSalesRate());
        final String itemName = itemModelList.get(position).getItemName();
        final String salesRate = itemModelList.get(position).getSalesRate();

        Intent intent = new Intent(Items.this, CartDisplay.class);
        intent.putExtra("itemName", itemName);
        intent.putExtra("salesRate", salesRate);

        return convertView;
    }

    class ViewHolder{
        private TextView tvItemName;
        private TextView tvPrice;
    }
}

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem add_item = menu.findItem(R.id.action_add_item);
    add_item.setVisible(false);
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.testmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
        case R.id.action_show_cart:
            cartMenuItem();
            break;
    }
    return true;
}

private void cartMenuItem() {
    Intent intent = new Intent(Items.this, CartDisplay.class);
    startActivity(intent);
    finish();
}
}

CartDisplay.java

public class CartDisplay extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.row_cart_display);
    TextView tvItemName = (TextView) findViewById(R.id.tvItemName);
    TextView tvSalesRate = (TextView) findViewById(R.id.tvSalesRate);
    TextView tvTotalAmount = (TextView) findViewById(R.id.tvTotalAmount);

    Intent intent = getIntent();

    String itemName = intent.getStringExtra("itemName");
    tvItemName.setText(itemName);
    String salesRate = intent.getStringExtra("salesRate");
    tvSalesRate.setText("RS"+salesRate);
    tvTotalAmount.setText("Total: RS"+salesRate);
}

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem show_cart = menu.findItem(R.id.action_show_cart);
    show_cart.setVisible(false);
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.testmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
        case R.id.action_add_item:
            addMenuItem();
            break;
    }
    return true;
}

private void addMenuItem() {
    Intent intent = new Intent(this, Categories.class);
    startActivity(intent);
    finish();
}
}

Here is how the item layout looks

Here is how the CartDisplay layout looks

  1. 目前的问题是我无法将数据发送到CartDisplay 活动我还需要帮助实施其他一些事情
  2. 在项目活动中,如何在 - 和+按钮之间设置EditText 根据点击+和 - 按钮显示数字不应该 低于零(即没有像x1或x2或x3那样的负数 等)
  3. 当按下itemAddToCart按钮时(其中的推车按钮) 项目列表),它必须将项目添加到购物车,而不是移动到CartDisplay 活动,它还必须发送没有添加的项目(因为它的 需要在那里,即CartDisplay布局需要在EditText中显示 有)。
  4. 当购物车有更新时(例如我们添加了一件商品 购物车)需要在操作栏上显示购物车图标上的更新 +1或购物车中有两个商品+2或类似的东西,购物车图标是一个menu_item,如果按此处它会带你到 CartDisplay活动如何编码?
  5. 另外如何编码将多个选定项目的信息发送到购物车?
  6. 按下购物车图标时,必须显示更新和最后一次 活动必须暂停?现在我在CartDisplay中有菜单项 活动将带我回到项目活动,所以我可以添加 更多物品到购物车,过程一直持续到我结帐或 清除购物车。
  7. 我还需要帮助为Clear cart编写代码。
  8. 我的项目是github的开源,您可以看到我的完整代码

0 个答案:

没有答案