Well here I am doing a program that takes data from a website and the target in an expandable ListView, I am getting this data and I have everything organized, the ArrayList "courses" is the Array that contains the titles of the main Array and Faculty_main is an ArrayList that contains the various sub-arrays. But the problem is that the Expandable ListView is not being done.
Here is the code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> faculades = new ArrayList<String>();
ArrayList<ArrayList<String>> faculades_main = new ArrayList<ArrayList<String>>();
ArrayList<String> curso = new ArrayList<String>();
HashMap<String, List<String>> listDataChild;
private ExpandableListView ExlistView ;
List<String> listDataHeader;
ExpandableListAdapter listAdapter;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExlistView = (ExpandableListView) findViewById(R.id.ExpandableList);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String site1 = "http://www.dges.gov.pt/guias/indcurso.asp?letra=";
Document document;
Elements lista;
document = Jsoup.connect(site1 +"A").get();
Elements boxes = document.select("div.box10");
for (Element box : boxes) {
String linAreaC1 = box.select(".lin-area-c1").text();
String linAreaC2 = box.select(".lin-area-c2").text();
String linAreaC3 = box.select(".lin-area-c3").text();
curso.add(linAreaC2);
Log.d("tag","runned");
Element linCurso = box.nextElementSibling();
while (linCurso.hasClass("lin-curso")) {
String linCursoC2 = linCurso.select(".lin-curso-c2").text();
String linCursoC3 = linCurso.select(".lin-curso-c3").text();
String linCursoC4 = linCurso.select(".lin-curso-c4").text();
faculades.add(linCursoC3);
linCurso = linCurso.nextElementSibling();
}
faculades_main.add(faculades);
faculades = new ArrayList<String>();
}
for(int contador=0;contador<curso.size();contador++){
}
} catch (Exception e) {
e.printStackTrace();
}
prepareListData();
}
});
thread.start();
listAdapter = new ExpandableListAdapter(MainActivity.this, curso, listDataChild);
ExlistView.setAdapter(listAdapter);
}
private void prepareListData() {
listDataChild = new HashMap<String, List<String>>();
for(int contador2=0;contador2<curso.size();contador2++){
listDataChild.put(curso.get(contador2),faculades_main.get(contador2));
}
Log.d("tag",""+listDataChild);
}
}
ExpandableListAdapter.java
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;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@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 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 int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@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 hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
I have already confirmed and the HashMap listDataChild is correct and with all the data.