Mam aplikację która pobiera z pliku JSON w assets dane, wpisuje je do 3 tablic, i wyswietla w ExpandableListView. Stworzyłem dla każdej z nich oddzielny adapter:
public class MainParentAdapter extends BaseExpandableListAdapter {
Context context;
String[] MainparentList;
String[] MainchildList;
String[] child2;
AssetManager assets;
SecondAdapter sa;
CustomList cust;
public MainParentAdapter(Context context, AssetManager assets) throws IOException, JSONException {
this.context=context;
this.assets=assets;
ParseJSON();
cust=new CustomList(context);
cust.setAdapter(sa);
}
@Override
public int getGroupCount() {
return MainparentList.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupPosition;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv=new TextView(context);
tv.setText(MainparentList[groupPosition]+" "+MainchildList[groupPosition]+" "+child2[groupPosition]);
tv.setTextSize(30);
return tv;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
cust=new CustomList(context);
cust.setAdapter(sa);
cust.setGroupIndicator(null);
return cust;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public String loadJSON() {
String json = null;
try {
InputStream is = assets.open("test.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public void ParseJSON() throws JSONException {
JSONArray JA=new JSONArray(loadJSON());
JSONObject jo=null;
MainparentList=new String[JA.length()];
MainchildList=new String[JA.length()];
child2=new String[JA.length()];
sa=new SecondAdapter(context);
for (int i=0;i<MainparentList.length;i++) {
jo = JA.getJSONObject(i);
MainparentList[i] = jo.getString("name");
MainchildList[i] = jo.getString("species");
child2[i] = jo.getString("id");
}
sa.SecondParent=MainchildList;
sa.SecondChild=child2;
}
}
W tym glownym Rodzicu tworze dziecko jako customowa ExpandableListView. W metodzie ParseJSON pobieram wszystkie wartosci i kopiuje tablice do drugiego adapetera, aby je tam odczytac.
public class SecondAdapter extends BaseExpandableListAdapter
{
Context context;
String[] SecondParent;
String[]SecondChild;
boolean dip=false,dip2=false;
public SecondAdapter(Context context) {
this.context=context;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return groupPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = new TextView(context);
tv.setText(SecondChild[groupPosition]);
tv.setPadding(150, 5, 5, 5);
tv.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
return tv;
}
@Override
public int getChildrenCount(int groupPosition)
{
return 1;
}
@Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
@Override
public int getGroupCount()
{
return 1;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
TextView tv = new TextView(context);
tv.setText(SecondParent[groupPosition]);
tv.setPadding(50, 7, 7, 7);
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Kiedy apka sie odpala, pokazuje tylko elementy pierwszej listy dobrze, druga lista dziecko ma te same elementy dla kazdego rodzica.
Chcialem zeby kazdy rodzic mial dzieci kolejno jak z pliku JSON:
[
{
"id": 0,
"species": "Capra hircus",
"name": "Goat"
},
{
"id": 1,
"species": "Panthera pardus",
"name": "Leopard"
},
{
"id": 2,
"species": "Equus zebra",
"name": "Zebra"
},
{
"id": 3,
"species": "Equus swinia",
"name": "Swinia"
}
]
Dla kazdego parametru "name" powinno wyswietlic dziecko "species" a dla rodzica "species" dziecko "id". Jednak po odpaleniu, kazdy rodzic ma to samo dziecko "species" a kazdy rodzic "species" to samo dziecko "id".
Jak więc mogę pobrać poprawnie dziecko dla każdego głównego rodzica?