1

I am trying to make a basic shopping cart android application

Here is how the code looks

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. The issue at the moment is I am not able to send data to CartDisplay Activity also I need help with implementing few other things
  2. In Items Activity, how do I make EditText between - and + button to show the number based on the clicks on + and - button it should not go below zero (ie like no negative figures its like x1 or x2 or x3 etc)
  3. When pressed on itemAddToCart button (its the cart button inside the item list ) , it must add item to cart and not move to CartDisplay activity , it must also send the no of items added (because its needed there ie in CartDisplay layout needs to show this in EditText there).
  4. When there is an update in Cart (i.e like once we have Added an item to cart) need to show update on cart icon on Action bar with little +1 or where there is two items in cart +2 or anything similar , the cart icon is an menu_item if pressed here it will take you to CartDisplay activity how to code this ?
  5. Also how to code it to send info of multiple selected items to cart?
  6. when pressed on cart icon it must show the updates and the last activity must be paused ? now i have menu item in CartDisplay Activity which will take me back to Items Activity , so i can add more items to the cart , the process continues until i checkout or Clear cart .
  7. Also I need help to write code for Clear cart .

My project is open source at github there you can see my full code

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sooraj S
  • 291
  • 5
  • 13
  • 1
    My project is open source at github there you can see my full code https://github.com/sooorajjj/KOT I need help , if anyone is interested to join the project here is my mail sooorajjj@gmail.com – Sooraj S Aug 29 '16 at 12:56
  • 1
    You aren't really doing anything with the `intent` object you create (in which you put extra) – Shaishav Aug 29 '16 at 12:59
  • 1
    @Shaishav ik , i just left it there i need more than that just one item count, i think it wouldn't help ? , i guess that needs a whole lot of new code to do what i really want, i believe , which iam trying to figure out . Though sorry for leaving it there like that – Sooraj S Aug 29 '16 at 13:03
  • 1
    Alright, heres one thing you can do: You need to attach a click listener to your `ListView`, from there you'll get the position of click. Using that position you'll get the item from your `result` list object which brings us to the fact that you need to save that object now too (instead of simply passing it to the adapter). Once you get your list item, you can update (or call) your intent appropriately. Or, you can use java interfaces. – Shaishav Aug 29 '16 at 13:09
  • @Shaishav i guess you mean attach click listener to my buttons that would be - or + or addtocart button and yea i can get a few results like item name sales rate how would i go about + and - buttons and sending these values to next activity too ? can you code that for me ? – Sooraj S Aug 29 '16 at 16:18
  • I mean calling `setOnItemClickListener` on your `ListView` – Shaishav Aug 29 '16 at 16:40
  • @Shaishav Okay, so something like [this](http://stackoverflow.com/questions/4709870/setonitemclicklistener-on-custom-listview) ? what and about coding + and - buttons to display values in edit text or take values of edit text ? – Sooraj S Aug 29 '16 at 17:14

0 Answers0