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