我正在尝试制作一个基本的购物车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
EditText
根据点击+和 - 按钮显示数字不应该
低于零(即没有像x1或x2或x3那样的负数
等)我的项目是github的开源,您可以看到我的完整代码