I'm creating a program for android, in the app, I used ExpandableListview with BaseExpandableListViewAdapter, on default this adapter uses one TextView, I want to create some (two or three) TextViews on child listItem, please help me with code, what do write in the activity?
XML layout listitem child file:
<?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" >
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/lblListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textSize="15dip" />
<TextView
android:id="@+id/lblListItem2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textSize="15dip" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Some code from adapter:
@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);
TextView txtListChild2 = (TextView) convertView.findViewById(R.id.lblListItem2);
txtListChild2.setText(childText2);
return convertView;
}
Prepare datalists at this moment:
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
listDataHeader.add(getResources().getString(R.string.bstart_ver));
listDataHeader.add(getResources().getString(R.string.bstart_op));
List<String> Header1 = new ArrayList<>();
Header1.add(getResources().getString(R.string.bstart_string1));
Header1.add(getResources().getString(R.string.bstart_string2));
Header1.add(getResources().getString(R.string.bstart_string3));
List<String> Header2 = new ArrayList<>();
Header2.add(getResources().getString(R.string.bstart_string4));
Header2.add(getResources().getString(R.string.bstart_string5));
Header2.add(getResources().getString(R.string.bstart_string6));
Header2.add(getResources().getString(R.string.bstart_string7));
Header2.add(getResources().getString(R.string.bstart_string8));
listDataChild.put(listDataHeader.get(0), Header1);
listDataChild.put(listDataHeader.get(1), Header2);
}
Set adapter:
private ExpandableListAdapter listAdapter;
private ExpandableListView expListView;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View cpf = inflater.inflate(R.layout.checklist_bstart, container, false);
expListView = cpf.findViewById(R.id.lv_bstart);
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
return cpf;
}