Search code examples
androidexpandablelistviewget-childitem

Get ID of EditText in Expandable ListView


I'm using an Expandable ListView, I have a single EditText in Every Child Item. list_item.xml:

<TextView
            android:id="@+id/lblListItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="TextView"
            android:textColor="#000000"
            android:textSize="17dp" />

        <EditText
            android:id="@+id/marks"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Obtained"
            android:inputType="number"
            android:maxLines="1"
            tools:layout_editor_absoluteX="142dp"
            tools:layout_editor_absoluteY="60dp" />

There is a button on mainActivity, When the button is clicked, I want to get Text of Every Child's EditText Entered by User, I tried:

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

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

        edt=(EditText)convertView.findViewById(R.id.marks);
        String abc=edt.getText().toString();


        Toast.makeText(convertView.getContext(),"This: "+ abc,Toast.LENGTH_SHORT).show();
        txtListChild.setText(childText);
        return convertView;
    }

But, It works only when a group is Expanded,(Toast shows me text which was readily present in EditText) Not when User hits the Button.

So, How do I call getChildView method in Button Click? I don't know what arguments to pass when calling it in OnClick(). OR

Maybe a Hack will work: When Button is Clicked, the expanded group is collapsed and expanded, So it will do what I want ? Maybe


Solution

  • You have to set ID of Every EditText Manually. This can be done in getChildView(). Although It won't help you enter and Get Values From Child. It's only for setting and getting ID:

    @Override
       public View getChildView(int groupPosition, final int childPosition,
                               boolean isLastChild, View convertView, ViewGroup parent) {
    
         final String childText = (String) getChild(groupPosition, childPosition);
    
         if (convertView == null) {
                  LayoutInflater infalInflater = (LayoutInflater) this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  convertView = infalInflater.inflate(R.layout.list_item, null);
                }
    
         TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    
         if(childPosition==0)//Original Id will be get only when First Child is being populated
    {
              edt=(EditText)convertView.findViewById(R.id.marks);
        }
        edt.setId(childPosition); //here the ID will be same as childPosition i.e. 0,1,2...
        //Now You can get the ID by getID(id); //id can be 0,1,2...
    
    
        Toast.makeText(convertView.getContext(),"This: "+ abc,Toast.LENGTH_SHORT).show();
        txtListChild.setText(childText);
        return convertView;
            }