ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Expandablelistview中的奇怪行为 – Android

2019-10-08 11:02:13  阅读:193  来源: 互联网

标签:settext expandablelistadapter android java expandablelistview


我试图实现一个使用ExpandableListView的活动,我已经到目前为止,但现在我发现了一些奇怪的行为.

我的活动是为了记录用户指定的食物摄入量.他们可以选择菜单(早餐,午餐和晚餐 – 外部团体),以扩展其内容.
当用户点击内部菜单项时,会出现一个对话框,要求他们输入数量.一旦他们输入数量并关闭对话框,菜单项上的文本就会发生变化,以反映已消耗的项目数量

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

下面是我打开午餐菜单并单击“薯片”并指示数量为1后的列表.您可以看到“马铃薯”项目文本现已更改为反映数量为1.

奇怪的部分现在发生了.如果我点击’午餐’并关闭列表,然后再次点击它重新打开它,’Qty X 1’文字已跳转到另一个项目(牛奶)

每次打开和关闭列表时,它都会在两个项目之间来回跳转.此外,如果我打开其他项目,如早餐,我发现他们现在也得到了’Qty 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();
   }
     });

上面的代码打开一个对话框,接受数量值,附加列表元素以反映这一点,同时保存到数据库并设置带有所选项目和数量的文本视图.

很抱歉只是转储了大量的代码,但这让我很难过,希望有人可以提供帮助.

谢谢

凯文

解决方法:

我认为问题不在于对话框.我在我的项目中遇到了同样的问题而且无法修复它.我认为ExpandableListView在打开子项时有一个bug.我每组只有一个孩子,当我扩展一个小组时,孩子会移动到另一组.经过测试,我发现当我扩展时,孩子们会重新加载.

标签:settext,expandablelistadapter,android,java,expandablelistview
来源: https://codeday.me/bug/20191008/1871405.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有