当我单击可扩展子元素时,如何获取 EditBox 和 RadioButton 的每个值?

2024-03-14

我有一个可扩展的 ListView,其中每个父视图都有一个子视图。 在子视图中,我有一个子子元素:EditText、TextView 和 RadioButton。

子元素在单选按钮上单击时具有不同的值,并且在输入后编辑文本值

类别活动.java

   package com.restaurant.app;

    public class CategoryActivity extends Activity implements OnClickListener {


    public static final String TAG_TITLE = "title";
    public static final String TAG_PRICE="price";
    private static final String TAG_CATEGORY="category";
    private static final String TAG_DETAIL="detail";
    private static final String TAG_MENU="menu";
    private static final String TAG_ID="id";
    private static final String TAG_NOME="nome";
    private static final String TAG_HOTELID="hotel_id";
    private static final String TAG_DESCRIPTION="description";
    private static final String TAG_SERVICES="services";
    private static final String TAG_URLID="urlid";
    private static final String TAG_RESTAURANTID="restaurant_id";
    private static final String TAG_USERID="user_id";
    private static final String TAG_OFFERTA="offerta";
    private static final String TAG_PREZZO="prezzo";


    ArrayList<String> resultparent = new ArrayList<String>();
    ArrayList<Object> resultchild = new ArrayList<Object>();
    ArrayList<HashMap<String,String>> child = new ArrayList<HashMap<String,String>>();


    // for childlist item
    ArrayList<ArrayList<HashMap<String, String>>> childlist = new ArrayList<ArrayList<HashMap<String,String>>>();

    ImageView imagehome;
    ImageView imagemenu;
    ImageView imagebooking;
    ImageView imageordernow;
    ExpandableListView expandablelistview;
    CategoryList categoryadapter;   
    SimpleExpandableListAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.booking);   

        imagehome=(ImageView)findViewById(R.id.imagehome);
        imagehome.setOnClickListener(this);

        imagemenu=(ImageView)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(this);

        imageordernow=(ImageView)findViewById(R.id.imageordernow);
        imageordernow.setOnClickListener(this);

        expandablelistview = (ExpandableListView)findViewById(R.id.expandablelistview);    


        new CategoryAsynctask().execute("");

    }

    public class CategoryAsynctask extends AsyncTask<String,String,ArrayList<String>> 
    {
        @Override
        protected ArrayList<String> doInBackground(String... arg0)
        {

            JsonParser jparser = new JsonParser();
            String url="http://cssthemeclub.com/demo/saffron/webservices/categorymenu.php?restid=5";
            String data=jparser.getJSONFromUrl(url);


            try
            {
                JSONObject jobject=new JSONObject(data);                
                JSONArray jcategory=jobject.getJSONArray(TAG_CATEGORY);

                Log.e("Category length","--->"+jcategory.length());

                for (int i=0;i<jcategory.length();i++)
                {   

                    JSONObject jdetail=jcategory.getJSONObject(i).getJSONObject(TAG_DETAIL);

                    String categoryid=jdetail.get(TAG_ID).toString();
                    String nome=jdetail.get(TAG_NOME).toString();
                    String restaurantid=jdetail.get(TAG_RESTAURANTID).toString();
                    resultparent.add(nome);

                    JSONArray jmenu = new JSONArray();
                    jmenu=null;
                    jmenu=jcategory.getJSONObject(i).getJSONArray(TAG_MENU);

                    //Log.e("Jmenu","--->"+jmenu);

                    child=new ArrayList<HashMap<String,String>>();
                    child.clear();

                    for(int j=0;j<jmenu.length();j++)
                    {                           
                            String menuid=jmenu.getJSONObject(j).getString(TAG_ID).toString();
                            String offer=jmenu.getJSONObject(j).getString(TAG_OFFERTA).toString();
                            String price=jmenu.getJSONObject(j).getString(TAG_PREZZO).toString();

                            //Log.e("menu","--->"+offer);
                            //Log.e("price","-->"+price);

                            HashMap<String, String> map = new HashMap<String, String>();

                            map.put(TAG_MENU,menuid);
                            map.put(TAG_PREZZO,price);
                            map.put(TAG_OFFERTA,offer);
                            map.put(TAG_RESTAURANTID,restaurantid);

                            child.add(map);
                            childlist.add(child);

                    }
                    resultchild.add(child);

                }
                Log.e("Result Child","--->"+resultchild);

            }
            catch (Exception e) 
            {

            }

            return resultparent;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result)
        {   
            super.onPostExecute(result);

            categoryadapter= new CategoryList(resultparent,resultchild);
            categoryadapter.setInflater((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE),CategoryActivity.this);
            expandablelistview.setAdapter(categoryadapter);

    }   
 }
        @Override
        public void onClick(View v) 
        {
            switch (v.getId())
            {
                case R.id.imagehome     : Intent home = new Intent(CategoryActivity.this,HomeActivity.class);
                                          startActivity(home);
                                          break;

                case R.id.imagemenu     : Intent menu = new Intent(CategoryActivity.this,MenuActivity.class);
                                          startActivity(menu);  
                                          break;

                case R.id.imageordernow : Intent ordernow= new Intent(CategoryActivity.this,OrderDetailActivity.class);
                                          startActivity(ordernow); 
                                          break;

                default                 :   
                                          break;
            }
        }

    }

类别列表.java

public class CategoryList extends BaseExpandableListAdapter
{   
    private static final String TAG_OFFERTA="offerta";
    private static final String TAG_PREZZO="prezzo";
    private static final String TAG_RESTAURANTID="restaurant_id";
    private static final String TAG_MENU="menu";
    private static final String TAG_ID="id";
    public Activity activity;
    public ArrayList<String> resultparent;
    public ArrayList<HashMap<String,String>> tempchild; 
    public ArrayList<Object> resultchild=new ArrayList<Object>();
    ArrayList<HashMap<String,String>> childlist = new ArrayList<HashMap<String,String>>();
    public LayoutInflater inflater = null;


    String id;
    String qty;
    String title;   
    String menuid;  
    String sessionid;
    String restorantid;
    String price;



    public CategoryList(ArrayList<String> grouplist,ArrayList<Object> childItem)
    {   
          resultparent=grouplist;
          this.resultchild = childItem;
    }

    public void setInflater(LayoutInflater mInflater, Activity act)
    {   
          this.inflater = mInflater;
          activity = act;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) 
    {   

        return null;
    }   

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {

        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) 
    {   

          tempchild = (ArrayList<HashMap<String,String>>)resultchild.get(groupPosition); 

          TextView textprice,textmenutitle;
          final EditText editqty;

          RadioGroup radiogroupchilli;
          RadioGroup radiogroupoil;

          final ImageView imagechilli;
          final ImageView imagechillitwo;

          final ImageView imageoil;
          final ImageView imagechiz;


         // RadioButton radiochillion,radiochillioff,radiooil,radiochiz;

          if(convertView == null)
          {  
              convertView = inflater.inflate(R.layout.redlistchild,null);
          }


          radiogroupchilli=(RadioGroup)convertView.findViewById(R.id.radiogroupchilli);
          radiogroupoil=(RadioGroup)convertView.findViewById(R.id.radiogroupoil);

          imagechilli=(ImageView)convertView.findViewById(R.id.imagechilli);
          imagechillitwo=(ImageView)convertView.findViewById(R.id.imagechillitwo);

          imageoil=(ImageView)convertView.findViewById(R.id.imageoil);
          imagechiz=(ImageView)convertView.findViewById(R.id.imagechiz);

          textprice=(TextView)convertView.findViewById(R.id.textprice);
          textprice.setText(tempchild.get(childPosition).get(TAG_PREZZO).toString());

          textmenutitle=(TextView)convertView.findViewById(R.id.textmenutitle);
          textmenutitle.setText(tempchild.get(childPosition).get(TAG_OFFERTA).toString());

          editqty=(EditText)convertView.findViewById(R.id.editqty);       

          convertView.setTag(childPosition);

          radiogroupchilli.setOnCheckedChangeListener(new OnCheckedChangeListener()
          {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId)
                {
                    switch(checkedId)
                    {
                        case R.id.radiochillion  : 
                                                   imagechillitwo.setImageResource(R.drawable.chilitwooff);
                                                   imagechilli.setImageResource(R.drawable.chilion);
                                                   break;

                        case R.id.radiochillioff : 
                                                   imagechillitwo.setImageResource(R.drawable.chilitwoon);
                                                   imagechilli.setImageResource(R.drawable.chilioff);
                                                   break;
                    }
                }

           });

          radiogroupoil.setOnCheckedChangeListener(new OnCheckedChangeListener()
          {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId)
                {
                    switch(checkedId)
                    {
                        case R.id.radiooil  : 
                                              imageoil.setImageResource(R.drawable.oilon);
                                              imagechiz.setImageResource(R.drawable.chiijoff);
                                              break;

                        case R.id.radiochiz : 
                                              imageoil.setImageResource(R.drawable.oiloff);
                                              imagechiz.setImageResource(R.drawable.chijion);   
                                              break; 
                    }
                }

           });

          return convertView;

  }

    @Override
    public int getChildrenCount(int groupPosition)
    {   

        return ((ArrayList<String>)resultchild.get(groupPosition)).size();

    }

    @Override
    public Object getGroup(int groupPosition) 
    {

        return resultparent.get(groupPosition);
    }


    @Override
    public int getGroupCount()
    {

        return resultparent.size();
    }



    @Override
    public long getGroupId(int groupPosition) 
    {

        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {   
         TextView texttitle;
         final LinearLayout nonvegtitlelistlayout;

         if(convertView == null)
         { 
             convertView = inflater.inflate(R.layout.redlistgroup, null);
         }

         nonvegtitlelistlayout=(LinearLayout)convertView.findViewById(R.id.nonvegtitlelistlayout);
         texttitle=(TextView)convertView.findViewById(R.id.texttitle);
         texttitle.setText(resultparent.get(groupPosition));

        return convertView;
    }


    @Override
    public boolean hasStableIds() 
    {   
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {   
        return true;
    }

     @Override
     public void onGroupCollapsed(int groupPosition)
     {
        super.onGroupCollapsed(groupPosition);
     }

     @Override
     public void onGroupExpanded(int groupPosition) 
     {
         super.onGroupExpanded(groupPosition);
     }

}

而不是直接访问视图(通过迭代子视图)ViewGroup,这甚至不会像你期望的那样工作,因为ExpandableListView回收它的视图以及ListView确实如此),您应该将输入的数据存储到某种模型中。

换句话说,当您在列表中显示数据时,它应该有一些信息支持(String[]最简单的情况)。因此,您的任务是使用某些特定的类来支持显示数据,该类将保存您需要的信息。例如,您可以将其命名为Option.

然后,当您编辑数据时EditText,您将输入的文本保存到实例中Option对应于当前位置ExpandableListView。当您需要检索输入的信息时,您只需迭代每个Option你保存的。

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

当我单击可扩展子元素时,如何获取 EditBox 和 RadioButton 的每个值? 的相关文章

随机推荐

  • Objective-C 中面向方面的 HTTP 身份验证示例

    我正在寻找一些示例 技巧 建议以及一些用于实现 或查找其实现 面向方面的 HTTP 身份验证库的一般方向感 作为一些基础工作 我们构建了一个 iOS 库 该库为 HTTP 服务建立各种形式的身份验证 通过 Web 表单或本机模式窗口请求用户
  • Python 3 中 getstatusoutput 的替换

    由于命令模块自 Python 2 6 起已被弃用 我正在寻找替换的最佳方法命令 getstatusoutput http docs python org library commands html highlight commands co
  • Java Stream 在单循环中以声明方式分别按多个字段进行分组

    我用谷歌搜索了它 但我主要发现了按聚合字段分组或改变流响应的情况 但没有找到下面的情况 我有课User有字段category and marketingChannel 我必须以声明式风格编写一个方法 该方法接受用户列表并根据category
  • 删除列表中间的项目时,React 渲染错误的数据

    我有一个嵌套字段数组设置使用react hook form here https codesandbox io s compassionate galileo nkz71 请注意 我的实际代码有点复杂 但问题在这里显示是一样的 我遇到的问题
  • 在 Node.js 中反序列化后重新将对象与其类关联

    我正在为一些特定于应用程序的对象编写一个简单的序列化 反序列化框架 考虑以下 use strict function Dog name this name name Dog prototype constructor Dog Dog pro
  • 为什么用 MSVC 构建的 Python DLL 不能用 mod_wsgi 加载?

    我最近从 Python 2 5 更新到 2 7 在麻烦期间尝试了 2 6 虽然从命令行或 Django runserver 中一切正常 但 mod wsgi 无法加载任何包含使用 MSVC 构建的 DLL pyd 的模块 例如 如果我构建自
  • 删除指定目录中所有大小为零的文件[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 提前致谢 我想删除指定目录中的所有零大小文件 你能告诉我如何在 Ubuntu 操作系统上执行此操作的命令吗 谢谢 穆克蒂亚尔 find size 0c
  • for循环中断内的Javascript切换;冲突

    我在 JavaScript 中使用循环嵌套了 switch 如下所示 for var i 0 i lt checkBoxIds length i if checkBoxIds i prop checked var id checkBoxId
  • 使用knitr和latex在Beamer中代码块字体大小

    我正在尝试获取一些 R 代码以适合我的投影仪幻灯片 似乎无法通过更改字体大小size代码块的参数 就像您对其他knitr类型文档所做的那样 唯一的方法似乎是与 footnotesize在每个代码块之前 这让人沮丧 因为我有很多代码块 在很多
  • 请求的资源 () 不可用。 (下载 JSF 2.0 项目后运行 Tomcat 7.0 时)

    我在 eclipse 3 6 中创建了一个新的动态 Web 项目 我添加了 Tomcat 7 0 服务器并在 jsf 文件上选择 在服务器上运行 工作正常 我将其添加到 SVN 存储库以与其他人共享 我关闭 Eclipse 我创建了一个新的
  • 如何使用 blas 以最佳方式转置矩阵?

    我正在做一些计算 并对不同 BLAS 实现的优势和弱点进行一些分析 但是我遇到了一个问题 我正在测试 cuBlas 在 GPU 上执行 linAlg 似乎是一个好主意 但有一个问题 cuBlas 实现使用列主格式 并且由于这不是我最终需要的
  • 推入数组后变量值发生变化[重复]

    这个问题在这里已经有答案了 所以我正在测试这段代码 我在网络控制台中得到以下输出 value 0 Array Object value 1 Array Object Object 数组中的所有对象都具有完全相同的值 name value 1
  • UdpClient 在广播地址上接收

    在 c 中 我使用 UdpClient Receive 函数 public void StartUdpListener Object state try udpServer new UdpClient new IPEndPoint IPAd
  • 在启动时运行 R 脚本

    首先 这可能需要移至超级用户 我无法决定哪个场地更好 我正在尝试编写一个将在启动 重新启动时运行的 R 脚本 并将该计算机添加到池中doRedis http cran r project org web packages doRedis i
  • 角度指令 - 文件读取器在文件加载时未触发

    我正在尝试读取 angularjs 中上传的图像文件的数据 但正如预期的那样 文件读取器onload功能不起作用 任何人都可以帮助我解决此问题并读取图像文件以获取其数据 这是我的代码 app directive uploadImageFil
  • boost::计算流压缩

    如何使用 boost compute 进行流压缩 例如 如果您只想对数组中的某些元素执行繁重的操作 首先 生成掩码数组 其中包含与要执行操作的元素相对应的元素 mask 0 0 0 1 1 0 1 0 1 然后对掩码数组进行排它扫描 前缀和
  • Unity 从脚本创建 UI 控件

    我通过代码创建了一个切换 但它不会显示 此外 我无法更改文本字段的位置 我尝试了很多 但没有任何效果 这是我当前的版本 也许你看到了错误 我是 Unity 的新手 这非常困难 public class Game MonoBehaviour
  • Posix 线程问题

    我试图通过示例来理解 pthreads 我编写了以下代码 每次运行时都会给出不同的答案 有人可以解释一下这个错误吗 TIA 斯维亚 代码在这里 include
  • 如何从现有的 Azure 服务主体获取客户端密钥

    我已经创建了启用 RBAC 的服务主体 https learn microsoft com en us cli azure ad sp view azure cli latest在 Azure 中使用环境变量在我的操作系统中配置 Key V
  • 当我单击可扩展子元素时,如何获取 EditBox 和 RadioButton 的每个值?

    我有一个可扩展的 ListView 其中每个父视图都有一个子视图 在子视图中 我有一个子子元素 EditText TextView 和 RadioButton 子元素在单选按钮上单击时具有不同的值 并且在输入后编辑文本值 类别活动 java