show an empty group of expandablelistview

1

I was having a problem showing a group in the expandable list view to show an empty message when it has no elements.

I detail the code to understand it

 <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/view"
            android:layout_marginLeft="@dimen/page_margin_left_double"
            android:layout_marginRight="@dimen/page_margin_right_double"
            android:layout_marginTop="@dimen/page_margin_top_double"
            android:divider="@color/white"
            android:groupIndicator="@drawable/groupindicator" />

This is the title

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/listTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:layout_marginLeft="25dp"
    android:textColor="@color/greenDark" />

This is the item where it has a relative that is hidden and only shown if it has no items

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@android:color/white">

<RelativeLayout
    android:id="@+id/datos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp">

    <TextView
        android:id="@+id/expandedListPhone"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="0993333333"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/gris"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/expandedListDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/expandedListPhone"
        android:gravity="end"
        android:paddingRight="5dp"
        android:text="2018-05-10"
        android:textColor="@color/verdeClaro"
        android:textSize="12sp" />

    <RelativeLayout
        android:id="@+id/rl2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/expandedListPhone">

        <TextView
            android:id="@+id/expandedListReason"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="we wwwwwwwwe"
            android:textColor="@color/verdeClaro"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/expandedListAmount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/expandedListReason"
            android:gravity="end"
            android:paddingRight="3dp"
            android:text="$ 2.34"
            android:textColor="@color/gris"
            android:textSize="20sp" />
    </RelativeLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/line_separator_height"
        android:layout_below="@+id/rl2"
        android:layout_marginRight="5dp"
        android:background="@color/grey" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlEmpty"
    android:layout_width="match_parent"
    android:visibility="gone"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:paddingRight="3dp"
        android:text="No hay Movimientos"
        android:textColor="@color/gris"
        android:textSize="20sp" />
</RelativeLayout>

And here's the class that does this

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<Item>> expandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<Item>> expandableListDetail) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final Item expandedList = (Item) getChild(listPosition, expandedListPosition);

    final String expandedListTextPhone = expandedList.getPhone();
    final String expandedListTextAmount = expandedList.getAmount();
    final String expandedListTextDate = expandedList.getDate();
    final String expandedListTextReason = expandedList.getReason();



    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (layoutInflater != null) {


            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
    }
    if(expandedListPosition==1)
    {
        RelativeLayout relativeLayout = convertView.findViewById(R.id.rlEmpty);
        relativeLayout.setVisibility(View.VISIBLE);
    }


    TextView expandedListTextViewPhone = convertView.findViewById(R.id.expandedListPhone);
    expandedListTextViewPhone.setText(expandedListTextPhone);

    TextView expandedListTextViewDate = convertView.findViewById(R.id.expandedListDate);
    expandedListTextViewDate.setText(expandedListTextDate);

    DecimalFormat formater = new DecimalFormat("###.00");
    String saldoFormateado = formater.format(Double.parseDouble(expandedListTextAmount));
    TextView expandedListTextViewAmount = convertView.findViewById(R.id.expandedListAmount);
    String cadena = context.getString(R.string.MonedaMonto) + " " + saldoFormateado;
    expandedListTextViewAmount.setText(cadena);

    TextView expandedListTextViewReason = convertView.findViewById(R.id.expandedListReason);
    expandedListTextViewReason.setText(expandedListTextReason);
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (layoutInflater != null) {
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
    }
    TextView listTitleTextView = convertView.findViewById(R.id.listTitle);
    listTitleTextView.setText(listTitle);
    return convertView;
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}

I would have to achieve that but I am not satisfied with the solution I found to hide the text that shows that there are no movements and I can not validate when there are items and when not .. any help or suggestion is appreciated.

    
asked by jonatan diaz 03.07.2018 в 16:59
source

0 answers