2

我试图实现一个使用ExpandableListView的活动,我得到了很多,但现在我发现了一些奇怪的行为。Expandablelistview中的奇怪行为 - Android

我的活动是为了记录用户指定的食物摄入量。他们可以选择一些菜单(早餐,午餐和晚餐 - 外部团队),这些菜单可以展开以显示其内容。 当用户点击内部菜单项时,出现一个对话框询问他们的数量。一旦他们输入一个数字并关闭对话框,菜单项上的文本将发生变化,以反映已消耗的物品的数量。 fig 1

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

下面是打开午餐菜单后点击“土豆片”并显示数量为1的列表。如您所见,'土豆'itemtext现已更改为反映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(); 
    } 
    }); 

上面的代码打开一个对话框,接受数量值,附加列表元素来反映这一点,也保存到数据库,并设置一个文本视图与选定的项目和数量。

对不起,只是转储一大堆代码,但这让我难住,希望有人可以帮忙。

感谢

凯文

回答

0

我不认为问题出现在对话框中。我在我的项目中遇到了同样的问题,无法解决问题。我认为ExpandableListView打开孩子时有一个错误。我每个小组只有一个孩子,当我扩大一个小组时,小孩移动到另一个小组。经过测试,我发现当我展开时,孩子们重新加载。

1

的Android重用对话框。所以你看到的行为可能是这个结果。您可以使用活动托管对话框并使用onPrepareDialog()更新对话框内容。

+0

当你说android重用对话框,你的意思是列表元素?或者你的意思是用于输入物品数量的实际对话框? 谢谢 – 2010-09-03 08:43:39