我正在尝试制作一个购物清单的ListView,在该清单中单击任何按钮都会更改特定商品的数量。这是XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/descriptionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Description" />
<TextView
android:id="@+id/priceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="$14.99" />
<Button
android:id="@+id/lessButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lessButton"
android:text="-" />
<TextView
android:id="@+id/quantityText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/moreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="moreButton"
android:text="+" />
<TextView
android:id="@+id/itemTotalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="$0.00" />
</LinearLayout>
这是自定义适配器的Java代码,受到TheNewBoston的视频(https://www.youtube.com/watch?v=nOdSARCVYic)的严重影响:
public class ListCustomAdapter extends ArrayAdapter<String>
{
public String description = "";
public double price = 0;
public Integer quantity = 0;
public ListCustomAdapter(@NonNull Context context, String[] items) {
super(context, R.layout.custom_list_row, items);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_list_row, parent, false);
DecimalFormat df = new DecimalFormat("#,###,##0.00");
String entry = getItem(position);
TextView descriptionText = (TextView) customView.findViewById(R.id.descriptionText);
TextView priceText = (TextView) customView.findViewById(R.id.priceText);
TextView quantityText = (TextView) customView.findViewById(R.id.quantityText);
Button lessButton = (Button) customView.findViewById(R.id.lessButton);
Button moreButton = (Button) customView.findViewById(R.id.moreButton);
final Integer ENTRIES = 100;
String c = "";
String tempString = "";
Integer field = 0;
//Decoding the data.
System.out.println("String entry at position "+ position+ ": "+ entry);
for (int a = 0; a <= entry.length()-1; a++)
{
c = entry.substring(a,a+1);
System.out.println("Character line "+a+" : "+ c);
if (!c.equals("*"))
{
tempString += c;
}
else
{
System.out.println("Tempstring: "+tempString);
if (field == 0)
{
description = tempString;
}
else if (field == 1)
{
price = Float.valueOf(tempString);
}
else if (field == 2)
{
quantity = Integer.valueOf(tempString);
}
field++;
tempString = "";
if (field > 2)
{
field = 0;
}
}
}
descriptionText.setText(description);
priceText.setText("$"+df.format(price));
quantityText.setText(quantity.toString());
lessButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quantity > 0)
{
quantity--;
}
}
});
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quantity < ENTRIES)
{
quantity++;
}
}
});
return customView;
}
}
“解码数据”部分的解释:每个字符串项目都是一个巨大的字符串,其中包含描述,价格和数量,例如“牛肉* 3.33 * 0 *”。可能有更好的方法,但这就是我现在所知道的。
无论如何,在按钮OnClickListeners上,我需要做什么以确保更改该特定项目的数量并反映在ListView中?我听说过notifyDataSetChanged(),但不确定如何使用它。
答案 0 :(得分:0)
在这种情况下,您可以在更改数量后在setText中设置setText。
lessButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quantity > 0)
{
quantity--;
//put this value in quantityText
quantityText.setText(quantity.toString());
}
}
});
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quantity < ENTRIES)
{
quantity++;
//put this value in quantityText
quantityText.setText(quantity.toString());
}
}
});
但是随后您必须更改“一个巨型字符串”以具有新的数量((在某些可能的情况下,您需要将“一个巨型字符串”与新值一起使用)
更好的方法是使用必需的字段制作GroceryProduct类并创建对象。 (以防您熟悉OOP)。在这种情况下,notifyDataSetChanged()将特别有用。