开发者

Strange behaviour in Expandablelistview - Android

Im trying to implement an activity that uses ExpandableListView and I have gotten so far but now I have found some strange behavior.

My activity is meant to record food intake as specified by the user. they have a choice of menus (breakfast, lunch and dinner - the outer group) which expand to show their contents. when a user clicks on an inner menu item a dialog appears asking them for the qty. once they enter a qty and dismiss the dialog the text on the menu item changes to reflect the quantity of that item that has been consumed

Strange behaviour in Expandablelistview - Android

The above image shows the list in a closed state.

below is the list after I have opened the lunch menu and clicked on 'Potato Chips' and indicating a Quantity of 1. As you can see the 'Potato' itemtext has now been changed to reflect the Qty of 1.

Strange behaviour in Expandablelistview - Android

The strange part happens now. if I click on 'Lunch' and close the list and then click on it again re-opening it, the 'Qty X 1' text has jumped to another item (Milk)

Strange behaviour in Expandablelistview - Android

each time I open and close the list it jumps back and forth between the two items. Also if I open up other items, such as breakfast, I find that they too have now gotten items with 'Qty X 1' even though I havent clicked them.

The bits of code that are relevant are as such:

The XML for a child element:

<?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>

The code thats triggered on clicking a child element:

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.findVi开发者_高级运维ewById(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();
   }
     });

The above code opens a dialog, accepts a value for quantity, appends the list element to reflect this, also saves to database and sets a textview with the selected item and quantity.

Sorry to just dump a whole load of code, but this has me stumped and hopefully someone can help.

Thanks

Kevin


Android reuses dialogs. So the behavior you are seeing could be a result of that. You could use a activity managed dialog and use onPrepareDialog() to update dialog contents.


I don't think the problem is with the dialogs. I had the same problem in my project and couldn't fix it. I think the ExpandableListView has a bug when opening children. I had only one child per group and when I expand a group, the child moves to another group. After testing, I found out that when I expand the children are reloaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜