【发布时间】:2018-03-14 07:50:28
【问题描述】:
我正在开发一个购物车应用程序,我需要一些关于适配器的帮助。我试图回答一个类似的问题here,但这与我的情况有点不同。我有 3 个课程:MakeSale.java、DetailsActivity.java 和 ShoppingCartListAdapter.java。所以,这是流程。
在MakeSale.java 中,我声明了两个数组列表,第一个是cartItemArrayList 商店要由客户购买的商品。它们是生产者名称、产品名称、数量、unitCost 和第二个,cartCostItemsList 保存购物车中物品的总成本。
内部MakeSale.java
public static List<CartItem> cartItemArrayList = new ArrayList<>();
public static List<Double> cartCostItemsList = new ArrayList<>();
然后我有一个扩展ArrayAdapter 的适配器类。此类链接到一个 XML,list_item,它显示在列表视图上。现在,这个list_item 只显示生产者名称、产品名称、总数量、添加到购物车的每件商品的总成本。当用户想要对列表视图中的项目进行更改(增加或减少要购买的项目数量)时,list_item 已变为可点击。
内部ShoppingCartListAdapter.java
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.zynle.fisp_dealer.Dashboard;
import com.zynle.fisp_dealer.DetailsActivity;
import com.zynle.fisp_dealer.MakeSale;
import com.zynle.fisp_dealer.R;
import java.util.List;
import entities.CartItem;
public class ShoppingCartListAdapter extends ArrayAdapter<CartItem> {
private Context context;
private List<CartItem> cartItems;
public ShoppingCartListAdapter(Context context, List<CartItem> cartItems) {
super(context, R.layout.list_item, cartItems);
this.context = context;
this.cartItems = cartItems;
}
public int getCount() {
return cartItems.size();
}
public CartItem getItem(int position) {
return cartItems.get(position);
}
public long getItemId(int position) {
return cartItems.get(position).getId();
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CartItem currentProduct = getItem(position);
View view = layoutInflater.inflate(R.layout.list_item, parent, false);
TextView productName_txtv = (TextView) view.findViewById(R.id.nameTextView);
TextView producerName_txtv = (TextView) view.findViewById(R.id.producerTextView);
TextView productQuantity_txtv = (TextView) view.findViewById(R.id.qtyTextView);
TextView productCost_txtv = (TextView) view.findViewById(R.id.priceTextView);
productName_txtv.setText(cartItems.get(position).getProduct_txt());
producerName_txtv.setText(cartItems.get(position).getProducer_txt());
productQuantity_txtv.setText(String.valueOf(cartItems.get(position).getQuantity()));
productCost_txtv.setText(String.valueOf(cartItems.get(position).getCost_txt()));
productName_txtv.setText(currentProduct.getProduct_txt());
int perItem = currentProduct.getCost_txt();
int quantitee = currentProduct.getQuantity();
final int total = perItem * quantitee;
productCost_txtv.setText("Total: K" + total);
productQuantity_txtv.setText(currentProduct.getQuantity() + " Selected");
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent detailsIntent = new Intent(context, DetailsActivity.class);
detailsIntent.putExtra("name", currentProduct.getProduct_txt());
detailsIntent.putExtra("quantity", currentProduct.getQuantity());
detailsIntent.putExtra("total", total);
context.startActivity(detailsIntent);
}
});
return view;
}
public void makeNewSale() {
if (getCount() == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.Theme_AppCompat_Light_Dialog_Alert);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Cart is Empty!")
.setCancelable(false)
.setPositiveButton("Add new items", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getContext(), MakeSale.class);
getContext().startActivity(intent);
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getContext(), Dashboard.class);
getContext().startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
从意图来看,我的代码在一个名为DetailsActivity.java 的类中处理有关增加和减少按钮点击量的所有逻辑。当然DetailsActivity.java 链接到一些xml文件。
内部DetailsActivity.java
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import database.FISP_SQLiteDB;
import entities.CartItem;
import entities.Products;
public class DetailsActivity extends AppCompatActivity {
ImageView imageView;
TextView nameTextView, priceTextView, qtyTextView, available;
Button increaseQtyButton, decreaseQtyButton, contactSupplierButton, deleteButton, confirmButton;
private List<CartItem> cartItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// Get any data passed in from Fragment
Intent detailsIntent = getIntent();
final String name = detailsIntent.getStringExtra("name");
imageView = (ImageView) findViewById(R.id.imageView);
nameTextView = (TextView) findViewById(R.id.nameTextView);
available = (TextView) findViewById(R.id.availableQTY);
priceTextView = (TextView) findViewById(R.id.priceTextView);
qtyTextView = (TextView) findViewById(R.id.qtyText);
increaseQtyButton = (Button) findViewById(R.id.increaseQtyButton);
decreaseQtyButton = (Button) findViewById(R.id.decreaseQtyButton);
contactSupplierButton = (Button) findViewById(R.id.contactSupplierButton);
deleteButton = (Button) findViewById(R.id.deleteProductButton);
confirmButton = (Button) findViewById(R.id.confirm);
nameTextView.setText(name);
int quantityPicker = Integer.parseInt(MakeSale.quantityPicker_Npkr.getText().toString());
qtyTextView.setText("" + quantityPicker);
final FISP_SQLiteDB db = new FISP_SQLiteDB(DetailsActivity.this);
final Products product = db.getProduct(name);
if (product != null) {
final double productPrice = (product.getPrice() * quantityPicker);
final int subQuantity = (product.getQuantity() - quantityPicker);
priceTextView.setText("K" + productPrice);
available.setText("Available Quantity is " + subQuantity);
final int[] counter = {quantityPicker};
final int[] counter1 = {quantityPicker};
final int[] minteger = {1};
increaseQtyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
qtyTextView.setText(String.valueOf(counter[0]++));
int reducingQty = (subQuantity - counter1[0]++);
double totalingPrice = productPrice + (product.getPrice()* minteger[0]++);
available.setText(String.valueOf("Available Quantity is " + reducingQty));
priceTextView.setText("K" + totalingPrice);
decreaseQtyButton.setEnabled(true);
if(reducingQty==0){
increaseQtyButton.setEnabled(false);
decreaseQtyButton.setEnabled(true);
}
}
});
decreaseQtyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
qtyTextView.setText(String.valueOf(counter[0]--));
int increasingQty = (subQuantity - counter1[0]--);
double totalingPrice = productPrice - (product.getPrice()* minteger[0]--);
available.setText(String.valueOf("Available Quantity is " + increasingQty));
priceTextView.setText("K" + totalingPrice);
increaseQtyButton.setEnabled(true);
if (increasingQty==product.getQuantity()){
increaseQtyButton.setEnabled(true);
decreaseQtyButton.setEnabled(false);
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//db.deleteProduct(name);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder ab = new AlertDialog.Builder(DetailsActivity.this, R.style.MyDialogTheme);
ab.setMessage("Delete " + name + " ?").setPositiveButton("DELETE", dialogClickListener)
.setNegativeButton("CANCEL", dialogClickListener).show();
}
});
contactSupplierButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
// Creating alert Dialog with two Buttons
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DetailsActivity.this, R.style.MyDialogTheme);
// Setting Dialog Title
alertDialog.setTitle("Do you want to call?");
// Setting Dialog Message
alertDialog.setMessage("" + product.getSupplierName());
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.warning);
// Setting Negative "NO" Button
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to execute after dialog
Intent callIntent = new Intent(Intent.ACTION_CALL);
//callIntent.setData(Uri.parse("" + product.getSupplierPhone().trim()));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:" + product.getSupplierPhone()));
if (ActivityCompat.checkSelfPermission(DetailsActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
DetailsActivity.this.startActivity(callIntent);
}
});
// Showing Alert Message
alertDialog.show();
}
});
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//CartItem cartItem = new CartItem(producer, product, quantity, unitCost);
//cartItemArrayList.add(cartItem);
Intent intent = new Intent(DetailsActivity.this, ShoppingCart.class);
startActivity(intent);
}
});
}
}
}
现在,当用户单击confirmChangesBtn 时,如何将这些新值(新数量、新总成本)设置/替换到列表视图上的list_item 中?通过这样做更改数组列表中的产品详细信息(cartItemArrayList 和 cartCostItemsList)。将 DetailsActivity.java 中的 textview 值传递给适配器进行显示?我该怎么办?任何人?
【问题讨论】:
-
只需将值传递给适配器构造函数并创建逻辑!
-
像在 DetailsActivity 中可能在您的减少/增加按钮单击侦听器中,只需将适配器中使用的列表访问到您的活动 List
在适配器类中创建一个方法 public List getDataList( ) { return cartItems ;// 您在适配器的主要数据列表。现在在活动中调用 List carttemsList = adapter.getDataList();然后 CartItems cartItems = cartItemsList.get(position); // 将受到影响的视图位置。 cartItems.setValue("YourValue"); cartItemsList.add(cartItems, 位置); adapter.notifyDataSetChanged(); -
您可以使用界面来更改。
-
您在哪里使用
ShoppingCartListAdapter?显示活动代码 -
@phpdroid 谢谢,让我看看你的回答太直接了
标签: java android listview arraylist android-adapter