Java 如何在Arraylist中计算价格并在另一个页面中显示选中的项目(Android Studio)
我的应用程序有三个xml文件和两个java文件。我几乎完成了这项工作,但我坚持让应用程序计算用户检查的餐费,并在发票页面上显示所选餐费的名称。我将向您展示代码,我想知道的是,当用户单击count total按钮并将其显示在invoic_页面的tv_result上时,如何让应用程序计算价格并显示总价。我还想知道如何在发票页面中显示已签入(checkedItems)的膳食名称 活动\u main.xmlJava 如何在Arraylist中计算价格并在另一个页面中显示选中的项目(Android Studio),java,android,xml,Java,Android,Xml,我的应用程序有三个xml文件和两个java文件。我几乎完成了这项工作,但我坚持让应用程序计算用户检查的餐费,并在发票页面上显示所选餐费的名称。我将向您展示代码,我想知道的是,当用户单击count total按钮并将其显示在invoic_页面的tv_result上时,如何让应用程序计算价格并显示总价。我还想知道如何在发票页面中显示已签入(checkedItems)的膳食名称 活动\u main.xml <?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sahora.al_baik.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1"/>
<Button
android:text="Count Total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/findSelected"
android:layout_alignBottom="@+id/listView1"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox"
android:layout_alignBottom="@+id/checkBox"
android:layout_toRightOf="@+id/checkBox"
android:text="TextView" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Checked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkedItems"
android:layout_margin="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv_result"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
很抱歉,我是编码初学者^ ^请在复选框中添加一个setOnCheckedChangeListener
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.food_info, null);
holder = new ViewHolder();
holder.price = (TextView) convertView.findViewById(R.id.price);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Food food = foodList.get(position);
holder.price.setText(" (" + food.getPrice() + ")");
holder.name.setText(food.getName());
holder.name.setChecked(food.isSelected());
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
food.setSelected(b);
}
});
holder.name.setTag(food);
return convertView;
}
}
使食物列表成为全球性的。点击按钮
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.invoic_page);
int totalPrice = 0;
for(Food f : foodList){
if(f.isSelected){
totalPrice += f.getPrice();
}
//totalPrice variable now has the total of all selected items
}
});
}
添加新代码后,我发现一些错误。。。请问我该怎么办?以下是错误:[1]:[2]:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.food_info, null);
holder = new ViewHolder();
holder.price = (TextView) convertView.findViewById(R.id.price);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Food food = foodList.get(position);
holder.price.setText(" (" + food.getPrice() + ")");
holder.name.setText(food.getName());
holder.name.setChecked(food.isSelected());
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
food.setSelected(b);
}
});
holder.name.setTag(food);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.invoic_page);
int totalPrice = 0;
for(Food f : foodList){
if(f.isSelected){
totalPrice += f.getPrice();
}
//totalPrice variable now has the total of all selected items
}
});
}