gpt4 book ai didi

java - Expandablelistview 中的奇怪行为 - Android

转载 作者:太空狗 更新时间:2023-10-29 15:24:14 25 4
gpt4 key购买 nike

我正在尝试实现一个使用 ExpandableListView 的 Activity ,到目前为止我已经完成了,但现在我发现了一些奇怪的行为。

我的 Activity 旨在记录用户指定的食物摄入量。他们可以选择菜单(早餐、午餐和晚餐 - 外层),这些菜单会展开以显示其内容。当用户单击内部菜单项时,会出现一个对话框,询问他们的数量。一旦他们输入数量并关闭对话框,菜单项上的文本就会更改以反射(reflect)已消耗的该项目的数量 fig 1

上图显示了处于关闭状态的列表。

下面是我打开午餐菜单并单击“薯片”并指示数量为 1 后的列表。如您所见,“马铃薯”项目文本现已更改为反射(reflect)数量 1。

fig 2

奇怪的部分现在发生了。如果我单击“午餐”并关闭列表,然后再次单击它重新打开它,“数量 X 1”文本已跳转到另一个项目(牛奶)

alt text

每次我打开和关闭列表时,它都会在两个项目之间来回跳转。此外,如果我打开其他项目,例如早餐,我会发现即使我没有点击它们,它们现在也已经获得了“数量 X 1”的项目。

相关的代码位如下:

子元素的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView android:id="@+id/childname"
android:paddingLeft="50dip"

android:textSize="14dip"
android:textStyle="italic"
android:layout_width="200dip"
android:textColor="@color/black"
android:layout_height="40dip"/>

<TextView android:id="@+id/qty_display"
android:text="-"
android:textSize="14dip"
android:textStyle="italic"
android:layout_width="50dip"
android:textColor="@color/black"
android:layout_height="wrap_content"/>
</LinearLayout>

点击子元素时触发的代码:

public boolean onChildClick(
ExpandableListView parent,
View v,
int groupPosition,
int childPosition,
long id) {

// open the dialog and inflate the buttons
myDialog = new Dialog(this);
myDialog.setTitle("Food Item Qty");
myDialog.setContentView(R.layout.food_intake_dialog);
final Button ok = (Button)myDialog.findViewById(R.id.fi_ok_but);
Button cancel = (Button)myDialog.findViewById(R.id.fi_cancel_but);

//the id for this item is stored as a hash key in a map (say, item_id01)
String key = "item_id"+groupPosition+""+childPosition;
current_selected_food_item_id = Integer.parseInt(itemMap.get(key));

// inflate the textview that shows the qty for this item on the expandablelist
barQty = (TextView) v.findViewById(R.id.qty_display);

// set the ok button to record teh quantity on press
ok.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {

//inflate the input box that receives quantity from user
EditText fiQty = (EditText) myDialog.findViewById(R.id.fiQty);
// get the quantity and append the text on hte list item
String qty = fiQty.getText().toString();
barQty.setText("Qty X "+qty);

//open the database and save state
FoodIntake.this.application.getFoodIntakeHelper().open();
FoodIntake.this.application.getFoodIntakeHelper().storeFoodIntakeLog(current_selected_food_item_id,qty,visit_id,remote_visit_id);
String log = FoodIntake.this.application.getFoodIntakeHelper().getFoodIntakeLog(visit_id);
FoodIntake.this.application.getFoodIntakeHelper().close();

// append the main food intake list and close the dialog
list.setText("Food Intake Log:\n\n"+log);
myDialog.cancel();
}
});

上面的代码打开一个对话框,接受一个数量值,附加列表元素来反射(reflect)这一点,还保存到数据库并设置一个带有所选项目和数量的 TextView 。

很抱歉只是转储了一大堆代码,但这让我很困惑,希望有人能提供帮助。

谢谢

凯文

最佳答案

Android 重复使用对话框。所以你看到的行为可能是它的结果。您可以使用 Activity 管理对话框并使用 onPrepareDialog()更新对话框内容。

关于java - Expandablelistview 中的奇怪行为 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3633823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com