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
- 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
- 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) - 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).
- 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 ?
- Also how to code it to send info of multiple selected items to cart?
- 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 .
- Also I need help to write code for Clear cart .
My project is open source at github there you can see my full code