我是编程新手,如果您可以帮助我,那将是很棒的。我有一个带有按钮和列表视图的活动。列表视图上的值是名称,价格,类别和数量,并通过适配器定义。我想知道如何将每行的数量值与价格值相乘并求和所有这些值,并在单击按钮时在烤面包中显示结果。 Here's对其进行图形处理以更好地理解它。
代码如下:
CartListActivity.java
public class CartListActivity extends AppCompatActivity {
ListView userList;
UserCustomAdapter userAdapter;
ArrayList<User> userArray = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_list);
Button payBtn = (Button)findViewById(R.id.payBtn);
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Total: x€", Toast.LENGTH_SHORT).show();
}
});
/**
* add item in arraylist
*/
userArray.add(new User("Olives verdes", "1,20€", "Aperitius", "0"));
userArray.add(new User("Olives negres", "1,30€", "Aperitius", "0"));
userArray.add(new User("Cogombrets", "1,80€", "Aperitius","0"));
userArray.add(new User("Patates Lay's", "1,19€", "Aperitius","0"));
userArray.add(new User("Potet", "0,95€", "Bebè","0"));
userArray.add(new User("Biberó", "3,85€", "Bebè","0"));
userArray.add(new User("Tovalloletes", "1,10€", "Bebè","0"));
userArray.add(new User("Perxes de nadó", "1,95€", "Bebè","0"));
/**
* set item into adapter
*/
userAdapter = new UserCustomAdapter(CartListActivity.this, R.layout.view_row,
userArray);
userList = (ListView) findViewById(R.id.listView);
userList.setItemsCanFocus(false);
userList.setAdapter(userAdapter);
/**
* get on item click listener
*/
userList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
Log.i("List View Clicked", "**********");
Toast.makeText(CartListActivity.this,
"List View Clicked:" + position, Toast.LENGTH_LONG)
.show();
}
});
}
}
UserCustomAdapter.java
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.textPrice = (TextView) row.findViewById(R.id.textView2);
holder.textCategory = (TextView) row.findViewById(R.id.textView3);
holder.textQuantity = (TextView) row.findViewById(R.id.quantityTxt);
holder.btnEdit = (Button) row.findViewById(R.id.button1);
holder.btnDelete = (Button) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
final User user = data.get(position);
holder.textName.setText(user.getName());
holder.textPrice.setText(user.getPrice());
holder.textCategory.setText(user.getCategory());
holder.textQuantity.setText(user.getQuantity());
final UserHolder finalHolder = holder;
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String value1 = finalHolder.textQuantity.getText().toString();
int a = Integer.parseInt(value1);
if(a <= 99) {
int sum = a + 1;
Log.i("Edit Button Clicked", "**********");
user.setQuantity(Integer.toString(sum));
finalHolder.textQuantity.setText(Integer.toString(sum));
}
}
});
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String value2 = finalHolder.textQuantity.getText().toString();
int a = Integer.parseInt(value2);
if(a > 0) {
int substract = a - 1;
Log.i("Delete Button Clicked", "**********");
user.setQuantity(Integer.toString(substract));
finalHolder.textQuantity.setText(Integer.toString(substract));
}
}
});
return row;
}
static class UserHolder {
TextView textName;
TextView textPrice;
TextView textCategory;
TextView textQuantity;
Button btnEdit;
Button btnDelete;
}
}
User.java
public class User {
String name;
String price;
String category;
String quantity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public User(String name, String price, String category, String quantity) {
super();
this.name = name;
this.price = price;
this.category = category;
this.quantity = quantity;
}
}
答案 0 :(得分:0)
希望我没有解决您的问题,如果您遍历payBtn onClick方法内的userArray列表并获得总和,该怎么办? 像这样的东西:
Button payBtn = (Button)findViewById(R.id.payBtn);
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total = 0;
for(User u: userArray){
total += (u.getPrice()*u.getQuantity());
}
Toast.makeText(getApplicationContext(), "Total: +total+"€", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
因此,对于您的设计,正如我在您的代码中看到的那样,您有一个arrayList,其中包含所有创建的项目。 我会做这样的事情(在您的活动类中创建此函数):
public class CartListActivity extends AppCompatActivity {
ListView userList;
UserCustomAdapter userAdapter;
ArrayList<User> userArray = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_list);
Button payBtn = (Button)findViewById(R.id.payBtn);
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Total: x€", Toast.LENGTH_SHORT).show();
}
});
/**
* add item in arraylist
*/
userArray.add(new User("Olives verdes", "1,20€", "Aperitius", "0"));
userArray.add(new User("Olives negres", "1,30€", "Aperitius", "0"));
userArray.add(new User("Cogombrets", "1,80€", "Aperitius","0"));
userArray.add(new User("Patates Lay's", "1,19€", "Aperitius","0"));
userArray.add(new User("Potet", "0,95€", "Bebè","0"));
userArray.add(new User("Biberó", "3,85€", "Bebè","0"));
userArray.add(new User("Tovalloletes", "1,10€", "Bebè","0"));
userArray.add(new User("Perxes de nadó", "1,95€", "Bebè","0"));
/**
* set item into adapter
*/
userAdapter = new UserCustomAdapter(CartListActivity.this, R.layout.view_row,
userArray);
userList = (ListView) findViewById(R.id.listView);
userList.setItemsCanFocus(false);
userList.setAdapter(userAdapter);
/**
* get on item click listener
*/
userList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
Log.i("List View Clicked", "**********");
Toast.makeText(CartListActivity.this,
"List View Clicked:" + position, Toast.LENGTH_LONG)
.show();
}
});
}
private double getTotalPrice(){
double total = 0;
if (userArray.size() == 0){ return total;}
else {
for (User user : userArray ) {
double price = Double.parseDouble(user.getPrice()); // assuming you price is a string , and that you have getters and setters for all of the user's fields
int quantity = Integer.parseInt(user.getQuantity());
total = price*quantity;
}
return total;
}
}
(如果语法方面有问题,我将编辑答案) 然后在您的onClick中:
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double totalPrice = getTotalPrice();
String msg = String.format("Total: %f€" , totalPrice ) // %f because the function returns double
Toast.makeText(getApplicationContext(), msg , Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
yourButton.setOnClickListener(new View.OnClickListener() {
long total=0;
for(User u:userArray){
String priceAsString=u.getPrice();
String priceWithOutCamma=priceAsString.replaceAll(",","");
String priceWithOutSign=priceWithOutCamma.replaceAll("€","");
int price=Integer.valueOf("priceWithOutSign");
int quantity=Integer.valueOf(u.getQuantity());
total+=price*quantity;
}
DecimalFormat format=new DecimalFormat("#,###");
String totalFormatted=format.format(total);
Toast.makeText(getApplicationContext(), "Total: " +totalFormatted+"€", Toast.LENGTH_SHORT).show();
}