こんにちは、私はjsonから3つのレベルの拡張可能なリストビューを作成したい。 私は拡張可能なリストビューを2つの2つのレベルまで持ってきましたが、3つのレベルのために私はそれを完了するために私を助けてくださいどのような身体ドンしてください方法を持っていなかった。 ここに私が作成したコードです。AndroidのJSONから3レベルの展開可能なリストビューを作成する方法は?
私の主な活動
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;
}
私の親拡張可能なリストビューは、ここで
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;
}
である3つのレベルのための拡張可能なリストは、その
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;
}
}
私のために働いていませんここでは2番目のレベルの展開リストビューです
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);
}
こんにちは、私は3つのレベルの拡張を作成するのを手伝ってください。上記のコードは、私がExpandableListAdapterだけを使用する場合に機能します。 は、上記のコードでは、私は
iはリストの各要素にチェックボックスを追加したいと私は子供を選択すると、その親も
を選択する必要があります動作していない3つのレベルのためにchildSubChildAdapterを使用していましたここは、この問題で私を助けてください、私の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"
}
]
}
]
}
です。私はこのタイプの仕事を初めてやっています。事前に
おかげ
に従うよう。 –