Expandablelistview 中的奇怪行为 - Android

2023-12-01

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

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 fig 1

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

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

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

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

Thanks

Kevin


Android 重用对话框。因此,您所看到的行为可能就是由此造成的。您可以使用活动管理对话框并使用onPrepareDialog()更新对话框内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Expandablelistview 中的奇怪行为 - Android 的相关文章

  • 对象映射器 - YAMLFactory - 由于缺少 _createContentReference 方法而出现异常

    我正在使用最新的 2 13 0 版本的 jackson 当我尝试解析 YAML 文件时 出现此异常 java lang NoSuchMethodError com fasterxml jackson core io ContentRefer
  • 如何组合 3 个或更多 CompletionStages?

    如果有 2 个 CompletionStages 我可以将它们与thenCombine method CompletionStage a aCompletionStage getA CompletionStage b bCompletion
  • Netty Nio java 中的通信

    我想在 Netty nio 中创建一个具有两个客户端和一个服务器的通信系统 更具体地说 首先 我希望当两个客户端与服务器连接时从服务器发送消息 然后能够在两个客户端之间交换数据 我正在使用本示例提供的代码 https github com
  • Firebase API 初始化失败,java.lang.reflect.InitationTargetException

    我在我的应用程序中使用 firebase 身份验证 数据库和存储服务 之前运行良好 我已经添加了 firebase 云消息传递设置 如文档中所述 但应用程序在运行时崩溃了 我调查了这个问题大约 4 个小时并尝试了不同的解决方案 就像保持所有
  • 覆盖Java中的属性[重复]

    这个问题在这里已经有答案了 在 Java 中 我最近有几个项目 我使用了这样的设计模式 public abstract class A public abstract int getProperty public class B exten
  • SQLiteConstraintException:错误代码19:约束失败——Android错误

    我已经看到了一些与此相关的其他问题 但没有一个答案似乎真正适用于我的代码 当我尝试插入数据库时 出现 SQLiteConstraintException 错误代码 19 约束失败 错误 这是插入操作的代码 db insert 现在返回 1
  • 如何修补更新 Android Studio (0.80 -> 0.81)?

    我安装了 Android Studio Beta v0 8 0 并下载了 v0 8 1 因为 IDE 不会自动下载 v0 80 并使用新版本修补 Android Studio 的预览系列自动做到了这一点 从他们的网页 http tools
  • Tomcat下的Spring CXF Soap Web服务:找不到服务

    我正在尝试使用 CXF 和 Spring 设置一个在 Tomcat 上运行的简单 CXF Web 服务 我有一个 Web 应用程序初始化程序来引导 CXF servlet public class WebAppInitializer ext
  • 为什么 CompletableFuture 的 thenAccept() 不在主线程上运行

    我在 CompletableFuture 的 SupplyAsync 中处理长时间运行的操作 并将结果放入 thenAccept 中 有时 thenAccept 在主线程上执行 但有时它在工作线程上运行 但我只想在主线程上运行 thenAc
  • 是否可以在本机代码中读取/编辑共享首选项?

    我有一个 Android 应用程序 其中包含一个使用 NDK 执行一些代码的 C 库 在 C 库中 我想更新应用程序共享首选项 我的问题 是否可以在本机代码中读取 编辑共享首选项 您可以在本机代码中做任何您想做的事情 这只是很麻烦 您需要
  • 是否可以用 C# 为 Android 编写应用程序?

    我们都知道Android运行Dalvik VM程序 通常开发人员用 Java 编写程序并将其编译为 Dalvik 字节码 我想知道是否有可能创建一个可以接受 C 代码并将其编译为 Dalvik 字节码的编译器 嗯 这是一种选择 或者您可以在
  • 无法获取 android.permission.CLEAR_APP_USER_DATA

    我正在开发需要特殊权限的系统应用程序 由于某种原因 我无法获得 CLEAR APP USER DATA 权限 但我可以使用 INSTALL PACKAGES DELETE PACKAGES 等 什么可能导致这种情况 显现 uses perm
  • 如何使用 javascript 迭代文件系统目录和文件?

    我正在使用 Javascript 编写一个应用程序 该应用程序将与 Phonegap 一起使用来制作 Android 应用程序 我正在使用 Phonegap File API 来读取目录和文件 相关代码如下所示 document addEv
  • 使用 Retrofit 获取原始 HTTP 响应

    我想从我的 API REST 获取原始 http 响应 我尝试过这个界面 POST login FormUrlEncoded Call
  • 如何获取在代码中 attrs.xml 中创建的枚举

    我创建了一个自定义视图 找到它here https bitbucket org informatic0re awesome font iconview 具有枚举类型的可声明样式属性 在 xml 中 我现在可以为我的自定义属性选择枚举条目之一
  • 可空日期列合并问题

    我在 Geronimo 应用程序服务器上使用 JPA 和下面的 openjpa 实现 我也在使用MySQL数据库 我在更新具有可为空 Date 属性的对象时遇到问题 当我尝试合并 Date 属性设置为 null 的实体时 不会生成 sql
  • Bipush 在 JVM 中如何工作?

    我知道 iload 接受整数 1 到 5 但是如何使用 bipush 指令扩展到更高的数字 特定整数如何与字节码一起存储 有几种不同的指令可用于推送整数常量 最小的是iconst 指令 这些只是一个字节 因为该值是在操作码本身中编码的 ic
  • 在edittext android中插入imageview

    我想将 imageview 放在 edittext 中 可能吗 我检查了 evernote 应用程序 它能够将照片放在编辑文本部分 我想让我的应用程序完全相同 我如何才能将从图库中选择的图像视图放入编辑文本中 我首先尝试将 imagevie
  • ImageIO.read(...) - 非常慢,有更好的方法吗?

    我正在加载大量将在我的应用程序中使用的图标 我计划在服务器启动时从 jar 中加载所有这些 然而 由于数百张图像加起来刚刚超过 9MB 执行此任务仍然需要 30 秒多的时间 我现在正在一个单独的线程中执行此操作 但这让我想知道我是否在代码中
  • 在应用程序的所有活动中重用操作栏

    我创建了一个 MenuActivity 它有一个操作栏和一个拆分操作栏 我想将此操作栏和 splitactionbar 视图用于我的应用程序中的所有活动 我是 android 的新手 所以有人可以逐步指导我 另外 我试图将搜索图标放在操作栏

随机推荐