Ad

Custom Expandablelistview Adding 2nd Child To Be Shown Next To Child1

I have a Expandablelistview inside a TabFragment which is working fine but I am struggling to adda 2nd child (lblListValue) next to the 1st child (lblListItem).

The 2nd childitem also needs to be filled through a HashMap, I already scouted StackOverflow and the internet to gather some ideas but somehow I cannot find something usefull. I always get stuck in the ExpandableListAdapter code, so I am trying from scratch again.

Some help with this is very welcome.

Edit: actually the answer from Frank N. Stein is correct, the LinearLayout should be removed.

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/lblListItem"
        android:paddingTop="5sp"
        android:paddingBottom="5dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="man1"
        android:textSize="14sp"
        android:textStyle="bold" >
    </TextView>
    <TextView
        android:id="@+id/lblListValue" <--- 2nd child to be displayed
        android:paddingTop="5sp"
        android:paddingBottom="5dp"
        android:paddingRight="20sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="test1"
        android:textSize="14sp">
    </TextView>      
  </RelativeLayout>
</LinearLayout>

TabFragment:

public class TabFragment1 extends Fragment {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
View rootView;

public TabFragment1() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);

    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // get the listview
    expListView = (ExpandableListView) rootView.findViewById(R.id.expListView);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview on child click listener
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(
                    getActivity(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                            listDataHeader.get(groupPosition)).get(
                            childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });
}

/*   * Preparing the list data   */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Factuurgegevens");
    listDataHeader.add("Contactgegevens");
    listDataHeader.add("Betalingsgegevens");
    listDataHeader.add("Mijn account");
    listDataHeader.add("Wachtwoord");

    // Adding child data
    List<String> Factuurgegevens = new ArrayList<String>();
    Factuurgegevens.add("Naam");
    Factuurgegevens.add("Straat en huisnummer");
    Factuurgegevens.add("Postcode en woonplaats");

    List<String> Contactgegevens = new ArrayList<String>();
    Contactgegevens.add("Telefoonummer");
    Contactgegevens.add("E-mail");

    List<String> Betalingsgegevens = new ArrayList<String>();
    Betalingsgegevens.add("Rekeningnummer");
    Betalingsgegevens.add("Naam rekeninghouder");

    List<String> Mijn_account = new ArrayList<String>();
    Mijn_account.add("Gebruikersnaam");

    List<String> Wachtwoord = new ArrayList<String>();
    Wachtwoord.add("Wachtwoord");

    listDataChild.put(listDataHeader.get(0), Factuurgegevens); // Header, Child data
    listDataChild.put(listDataHeader.get(1), Contactgegevens);
    listDataChild.put(listDataHeader.get(2), Betalingsgegevens);
    listDataChild.put(listDataHeader.get(3), Mijn_account);
    listDataChild.put(listDataHeader.get(4), Wachtwoord);
}
}

ExpandableListAdapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

//private final LayoutInflater inf;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;

    //inf = LayoutInflater.from(MainActivity.mContext);
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

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

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

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

@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);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

Answer

This is my version of list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/lblListItem"
        android:paddingTop="5sp"
        android:paddingBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="man1"
        android:textSize="14sp"
        android:textStyle="bold"
    />
    <TextView
        android:id="@+id/lblListValue"
        android:paddingTop="5sp"
        android:paddingBottom="5dp"
        android:paddingRight="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/lblListItem"
        android:text="test1"
        android:textSize="14sp"
    />
</RelativeLayout>
Ad
source: stackoverflow.com
Ad