import
android.os.Bundle;
import
android.view.View;
import
android.widget.ExpandableListView;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.ArrayList;
import
java.util.LinkedHashMap;
public
class
MainActivity
extends
AppCompatActivity {
private
LinkedHashMap<String, GroupInformation> mainSet =
new
LinkedHashMap<String, GroupInformation>();
private
ArrayList<GroupInformation> subSet =
new
ArrayList<GroupInformation>();
private
CustomizedAdapter listAdapter;
private
ExpandableListView simpleExpandableListView1;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadData();
simpleExpandableListView1 = (ExpandableListView) findViewById(R.id.simpleExpandableListView1);
listAdapter =
new
CustomizedAdapter(MainActivity.
this
, subSet);
simpleExpandableListView1.setAdapter(listAdapter);
simpleExpandableListView1.setOnChildClickListener(
new
ExpandableListView.OnChildClickListener() {
@Override
public
boolean
onChildClick(ExpandableListView parent, View v,
int
groupPosition,
int
childPosition,
long
id) {
GroupInformation headerInfo = subSet.get(groupPosition);
ChildInfo detailInfo = headerInfo.getSubsetName().get(childPosition);
Toast.makeText(getBaseContext(), headerInfo.getName() +
"/"
+ detailInfo.getName(), Toast.LENGTH_LONG).show();
return
false
;
}
});
simpleExpandableListView1.setOnGroupClickListener(
new
ExpandableListView.OnGroupClickListener() {
@Override
public
boolean
onGroupClick(ExpandableListView parent, View v,
int
groupPosition,
long
id) {
GroupInformation headerInfo = subSet.get(groupPosition);
Toast.makeText(getBaseContext(), headerInfo.getName(), Toast.LENGTH_LONG).show();
return
false
;
}
});
}
private
void
loadData() {
addDetails(
"Programming_Languages"
,
"Python"
);
addDetails(
"Programming_Languages"
,
"Java"
);
addDetails(
"Programming_Languages"
,
"Kotlin"
);
addDetails(
"Programming_Languages"
,
"NodeJS"
);
addDetails(
"Programming_Languages"
,
"GO"
);
addDetails(
"Relational_Database"
,
"Oracle"
);
addDetails(
"Relational_Database"
,
"SQLServer"
);
addDetails(
"Relational_Database"
,
"MySQL"
);
addDetails(
"NoSQL_Database"
,
"MongoDB"
);
addDetails(
"NoSQL_Database"
,
"Cassandra"
);
addDetails(
"NoSQL_Database"
,
"CouchDB"
);
}
private
int
addDetails(String mainSet, String subSet) {
int
groupPosition =
0
;
GroupInformation headerInfo =
this
.mainSet.get(mainSet);
if
(headerInfo ==
null
) {
headerInfo =
new
GroupInformation();
headerInfo.setName(mainSet);
this
.mainSet.put(mainSet, headerInfo);
this
.subSet.add(headerInfo);
}
ArrayList<ChildInfo> subList = headerInfo.getSubsetName();
int
listSize = subList.size();
listSize++;
ChildInfo detailInfo =
new
ChildInfo();
detailInfo.setName(subSet);
subList.add(detailInfo);
headerInfo.setSubsetName(subList);
groupPosition =
this
.subSet.indexOf(headerInfo);
return
groupPosition;
}
}