salut je veux créer l'affichage de liste expansible de trois niveaux du json. J'ai creted liste d'affichage extensible jusqu'à deux deux niveau, mais pour trois niveaux je n'ai pas eu comment faire s'il vous plaît si n'importe quel corps s'il vous plaît aidez-moi à le remplir. voici mon code que j'ai créé.Comment faire à trois niveaux extensible Voir la liste de JSON dans Android?
ma principale activité
String Tag="MainActivity";
private ExpandableListView expandableListView;
ArrayList<String>mainlist =new ArrayList<>();;
HashMap<String, List<String>> childContent = new HashMap<String, List<String>>();
HashMap<String, HashMap<List<String>,List<String>>> subchildContent = new HashMap<String, HashMap<List<String>,List<String>>>();
List<String> childlist;
List<String> subchildlist;
private List<String> parentHeaderInformation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReleaseOverviewReleaseAsynchTask().execute();
}
@Override
protected void onResume() {
super.onResume();
expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
//ExpandableListAdapter expandableListViewAdapter = new ExpandableListAdapter(getApplicationContext(), mainlist, childContent);
ChildSubChildExpandableListAdapter adapter=new ChildSubChildExpandableListAdapter(getApplicationContext(),mainlist,childlist,childContent);
expandableListView.setAdapter(adapter);
}
class ReleaseOverviewReleaseAsynchTask extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
String jsonStr = Util.loadJSONFromAsset(MainActivity.this,"release.json");
if (jsonStr != null) {
try {
JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {
JSONObject jo_object = array.getJSONObject(i);
childlist= new ArrayList<String>();
subchildlist= new ArrayList<String>();
String release= jo_object.get("Release").toString();
mainlist.add(jo_object.get("Release").toString());
Log.i("main release",release);
JSONArray jsonArrayDetails=jo_object.getJSONArray("details");
if (!jsonArrayDetails.isNull(i)){
JSONObject jo_object1 = jsonArrayDetails.getJSONObject(i);
String builddate= jo_object1.get("Build Date").toString();
Log.i(Tag,builddate);
String deploydate= jo_object1.get("Deploy Date").toString();
Log.i(Tag,deploydate);
String respindate= jo_object1.get("Respin Date").toString();
Log.i(Tag,respindate);
String releaselead= jo_object1.get("Release Lead").toString();
Log.i(Tag,releaselead);
String engineerlead= jo_object1.get("Engineering Lead").toString();
Log.i(Tag,engineerlead);
String testlead=jo_object1.get("Test Lead").toString();
Log.i(Tag,testlead);
}
JSONArray childarray=jo_object.getJSONArray("children");
for (int i_child = 0; i_child < childarray.length(); i_child++) {
JSONObject jo_objectchild = childarray.getJSONObject(i_child);
childlist.add(jo_objectchild.get("Release").toString());
Log.i("child release",jo_objectchild.get("Release").toString());
JSONArray subchildarray=jo_objectchild.getJSONArray("children");
for (int i_subchild=0;i_subchild<=subchildarray.length();i_subchild++){
if (!subchildarray.isNull(i_subchild)){
JSONObject jo_objectsubchild = subchildarray.getJSONObject(i_subchild);
Log.i("sub release",jo_objectsubchild.get("Release").toString());
subchildlist.add(jo_objectsubchild.get("Release").toString());
}
/*JSONObject jo_objectsubchild = subchildarray.getJSONObject(i_subchild);
*/
}
}
childContent.put(mainlist.get(i),subchildlist);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
else{
// Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
mes parents de liste extensible est
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> parentDataSource;
private HashMap<String, List<String>> childDataSource;
private HashMap<String, List<String>> subchildDataSource;
public ExpandableListAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child){
this.context = context;
this.parentDataSource = childParent;
this.childDataSource = child;
}
@Override
public int getGroupCount() {
return this.parentDataSource.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.childDataSource.get(this.parentDataSource.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return parentDataSource.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.childDataSource.get(parentDataSource.get(groupPosition)).get(childPosition);
}
@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 getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.parentlayout, parent, false);
}
String parentHeader = (String)getGroup(groupPosition);
TextView parentItem = (TextView)view.findViewById(R.id.parent_layout);
parentItem.setText(parentHeader);
notifyDataSetChanged();
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.childlayout, parent, false);
}
String childName = (String)getChild(groupPosition, childPosition);
TextView childItem = (TextView)view.findViewById(R.id.child_layout);
childItem.setText(childName);
notifyDataSetChanged();
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
est la liste extensible ici pour trois niveaux sa ne fonctionne pas pour moi
public class ChildSubChildExpandableListAdapter extends
BaseExpandableListAdapter{
Context context;
HashMap<String, List<String>> childdata;
private List<String> parentDataSource;
List<String> secondLevel;
private HashMap<String, List<String>> childDataSource;
public ChildSubChildExpandableListAdapter(Context context, List<String> Parent, List<String> secondLevel, HashMap<String, List<String>> data) {
this.context = context;
this.parentDataSource = Parent;
this.secondLevel = secondLevel;
this.childdata = data;
}
@Override
public int getGroupCount() {
return this.parentDataSource.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int i) {
return i;
}
@Override
public Object getChild(int i, int i1) {
return i1;
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
View view = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parentlayout, null);
TextView text = (TextView) convertView.findViewById(R.id.parent_layout);
text.setText(this.parentDataSource.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup group) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String headers =secondLevel.get(groupPosition);
List<String> header=new ArrayList<>();
header.add(headers);
List<List<String>> childData = new ArrayList<>();
HashMap<String, List<String>> secondLevelData= (HashMap<String, List<String>>) childdata.get(groupPosition);
for(String key : secondLevelData.keySet())
{
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new ExpandableListAdapter(context, header,secondLevelData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
voici la deuxième liste extensible de niveau de vue
public class SecondLevelExpandableListView extends ExpandableListView
{
public SecondLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
salut amis s'il vous plaît aidez-moi à créer le trois niveaux extensible. le code ci-dessus fonctionne si j'utilise uniquement ExpandableListAdapter. dans le code ci-dessus i ont utilisé le childSubChildAdapter pour trois niveaux, alors il ne fonctionne pas
Je veux ajouter la case à cocher dans chaque élément de la liste et si je choisis un enfant le son parent doit également être sélectionné
ici est mon JSON
{
"Release": "16.1",
"details": [],
"children": [
{
"Release": "16.1R3",
"details": [
{
"Build Date": "",
"Deploy Date": "",
"Respin Date": "",
"Release Lead": "Jl",
"Engineering Lead": "LG",
"Test Lead": "Dl"
}
],
"children": [
{
"Release": "16.1R3-S5",
"details": [
{
"Build Date": "2017-08-17",
"Deploy Date": "2017-08-25",
"Respin Date": "",
"Release Lead": "Jk",
"Engineering Lead": "Ly",
"Test Lead": "FD"
}
]
}
]
}
s'il vous plaît aidez-moi à ce problème. Je fais ce type de travail la première fois.
merci à l'avance
avec case expandableListview ne fonctionne pas correctement. –