【问题标题】:Return item name from ExpandableListView after onClick as String在 onClick 作为 String 之后从 ExpandableListView 返回项目名称
【发布时间】:2015-07-17 21:44:34
【问题描述】:

所以我试图将子项的名称作为字符串获取,以便可以将其添加到我收集的其他数据中并将其插入到数据库中,但是这种方法不起作用,我得到的只是名称父母的。

 exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            TextView the = (TextView) findViewById(R.id.child_txt);
            String tx = the.getText().toString();
            Toast.makeText(getBaseContext(), tx + " Child Clicked", Toast.LENGTH_SHORT).show();

            // Store value in sharedPreferences();

            Intent l = new Intent(CategoryActivity.this, AddRecipeActivity.class);
            startActivity(l);
            return false;
        }
    });

【问题讨论】:

    标签: android textview expandablelistview


    【解决方案1】:

    尝试从适配器而不是视图获取数据(您的字符串):

        exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
                 @Override 
                  public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
    
                    String tx = parent.getExpandableListAdapter().getChild(groupPosition, childPosition);// this will return your child Object (i guess String in your case)
                    Toast.makeText(getBaseContext(), tx + " Child Clicked", Toast.LENGTH_SHORT).show();
    
                    // Store value in sharedPreferences();
    
                    Intent l = new Intent(CategoryActivity.this, AddRecipeActivity.class);
                    startActivity(l);
                    return false;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      相关资源
      最近更新 更多