Java 如何在android的ExpandableList中将不同的子级添加到不同的父级

Java 如何在android的ExpandableList中将不同的子级添加到不同的父级,java,android,android-layout,android-listview,Java,Android,Android Layout,Android Listview,我想将不同的子项添加到ExpandableList中的不同子项中 我的主要活动.java:- public class MainActivity extends AppCompatActivity { //private static String TAG = MainActivity.class.getSimpleName(); private Toolbar mToolbar; ExpandableListAdapter listAdapter; ExpandableListView

我想将不同的子项添加到ExpandableList中的不同子项中

我的主要活动.java:-

public class MainActivity extends AppCompatActivity  {

//private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
int arraylistvalue = 0;
ArrayList<String> arraylist1,arraylist2,arraylist3,arrayList4,arrayList5;
int cnt = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    arraylist1  = new ArrayList<String>();
    arraylist2  = new ArrayList<String>();
    arraylist3  = new ArrayList<String>();
    arrayList4 =  new ArrayList<String>();
    arrayList5 =  new ArrayList<String>();
    new ProductsAsynTask().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$");


}
public class ProductsAsynTask extends AsyncTask<String,Void,Void>{

    ProgressDialog dialog;

    protected void onPreExecute(){
        super.onPreExecute();
        Log.d("In onPreExceute","");
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, Please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    protected Void doInBackground(String... param){
        try{
            Log.d("In doInBackground","");

            HttpClient client= new DefaultHttpClient();
            HttpPost post = new HttpPost(param[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if(status == 200){
                Log.d("Status",""+status);

                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
                JSONArray jArray = jsonObject.getJSONArray("categories");

                for(int i = 0;i < jArray.length();i++){
                    cnt++;
                    Log.d("value of array",jArray.length()+"");
                    Log.d("Value of i",""+i);

                    JSONObject jsonObject1 = jArray.getJSONObject(i);




                    arraylist1.add(jsonObject1.getString("name"));
                    //data1 = jsonObject1.getString("name");
                    //      Log.d("hello ",data1);

                    JSONArray jsonArray = jsonObject1.getJSONArray("children");
                    //           JSONObject jsonObject2 = jsonObject1.getJSONObject("children");

                    for(int j=0;j<jsonArray.length();j++){

                        JSONObject jsonObject2 = jsonArray.getJSONObject(j);

                        arraylist2.add(jsonObject2.getString("name"));
                        //  data2 = jsonObject2.getString("name");
                        JSONArray jsonArray1 = jsonObject2.getJSONArray("children_lv3");

                        for(int k=0;k<jsonArray1.length();k++){
                            JSONObject jsonObject3 = jsonArray1.getJSONObject(k);

                            arraylist3.add(jsonObject3.getString("name"));
                            arrayList4.add(jsonObject3.getString("href"));
                           /*  data3 = jsonObject3.getString("name");
                             data4 = jsonObject3.getString("href");   */
                        }
                        arrayList5.add(jsonObject2.getString("href"));
                        // data5 = jsonObject2.getString("href");
                    }
                }

            }
        }catch(IOException e){
            Log.e("Error IOException :",e.getMessage());
        }catch (JSONException e){
            Log.e("Error JSONException",e.getMessage());
        }
        return null;
    }

    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dialog.dismiss();
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        expListView.setAdapter(new ParentLevel());
        Log.d("Counter value",""+cnt);
    }
}

public class ParentLevel extends BaseExpandableListAdapter
{

    @Override
    public Object getChild(int arg0, int arg1)
    {
        return arg1;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent)
    {
        CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
        SecondLevelexplv.setAdapter(new SecondLevelAdapter());
        SecondLevelexplv.setGroupIndicator(null);
        return SecondLevelexplv;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return 3;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public int getGroupCount()
    {
        int columr = arraylist1.size();
        return columr;
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent)
    {
        TextView tv = new TextView(MainActivity.this);
            tv.setText("");
            tv.setBackgroundColor(Color.BLUE);
            tv.setPadding(10, 7, 7, 7);
        return tv;
    }

    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }
}

public class CustExpListview extends ExpandableListView
{

    int intGroupPosition, intChildPosition, intGroupid;

    public CustExpListview(Context context)
    {
        super(context);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public class SecondLevelAdapter extends BaseExpandableListAdapter
{

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent)
    {
        TextView tv = new TextView(MainActivity.this);
        tv.setText("child");
        tv.setPadding(15, 5, 5, 5);
        tv.setBackgroundColor(Color.YELLOW);
        tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return tv;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return 5;
    }

    @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(MainActivity.this);
        tv.setText("-->Second Level");
        tv.setPadding(12, 7, 7, 7);
        tv.setBackgroundColor(Color.RED);

        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;
    }

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if(id == R.id.action_search){
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />


    </LinearLayout>

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />




</LinearLayout>

<ExpandableListView
    android:id="@+id/lvExp"
    android:layout_height="match_parent"
    android:layout_width="260dp"
    android:layout_gravity="start"/>


   <!-- <fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.android.ShoppingMazza.activity.ExpandableListAdapter"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" /> -->


 </android.support.v4.widget.DrawerLayout>
public类MainActivity扩展了AppCompatActivity{
//私有静态字符串标记=MainActivity.class.getSimpleName();
私有工具栏mToolbar;
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
int arraylistvalue=0;
ArrayList ArrayList 1、ArrayList 2、ArrayList 3、ArrayList 4、ArrayList 5;
int-cnt=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar=(工具栏)findviewbyd(R.id.Toolbar);
设置支持操作栏(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ArrayList 1=新的ArrayList();
ArrayList 2=新的ArrayList();
ArrayList 3=新的ArrayList();
ArrayList 4=新的ArrayList();
ArrayList 5=新的ArrayList();
新产品同步任务()。执行(“http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$");
}
公共类ProductsAsynTask扩展了AsyncTask{
进程对话;
受保护的void onPreExecute(){
super.onPreExecute();
Log.d(“在onPreExceute中,”);
dialog=新建ProgressDialog(MainActivity.this);
setMessage(“正在加载,请稍候”);
setTitle(“连接服务器”);
dialog.show();
对话框。可设置可取消(false);
}
受保护的Void doInBackground(字符串…参数){
试一试{
Log.d(“在doInBackground”中,“”);
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(参数[0]);
HttpResponse response=client.execute(post);
int status=response.getStatusLine().getStatusCode();
如果(状态==200){
Log.d(“状态”,“状态+”);
HttpEntity=response.getEntity();
字符串数据=EntityUtils.toString(实体);
JSONObject JSONObject=新的JSONObject(数据);
JSONArray jArray=jsonObject.getJSONArray(“类别”);
for(int i=0;i
我还需要工具栏下方的抽屉。我是android开发新手,请帮助我,提前感谢

activitymain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ExpandableListView
 android:layout_width="fill_parent"
 android:id="@+id/ParentLevel"
 android:groupIndicator="@null"
 android:layout_height="fill_parent">
</ExpandableListView>
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

import android.widget.TextView;

import android.widget.LinearLayout.LayoutParams;


public class Home extends Activity 
{

   ExpandableListView explvlist;  
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel());

}

  public class ParentLevel extends BaseExpandableListAdapter
  {

  @Override
  public Object getChild(int arg0, int arg1) 
  {   
   return arg1;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) 
  {
   return childPosition;
  }

   @Override
   public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) 
  {
   CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
   SecondLevelexplv.setAdapter(new SecondLevelAdapter());
   SecondLevelexplv.setGroupIndicator(null);   
   return SecondLevelexplv;
  }

  @Override
  public int getChildrenCount(int groupPosition) 
  {   
   return 3;
  }

  @Override
  public Object getGroup(int groupPosition) 
  {
   return groupPosition;
  }

  @Override
  public int getGroupCount() 
  {   
   return 5;
  }

  @Override
  public long getGroupId(int groupPosition) 
  {   
   return groupPosition;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent) 
  {
   TextView tv = new TextView(Home.this);
   tv.setText("->FirstLevel");
   tv.setBackgroundColor(Color.BLUE);
   tv.setPadding(10, 7, 7, 7); 

   return tv;
  }

  @Override
  public boolean hasStableIds() 
  {
    return true;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) 
  {
    return true;
  }     
  }

  public class CustExpListview extends ExpandableListView
  {

int intGroupPosition, intChildPosition, intGroupid;

  public CustExpListview(Context context) 
  {
   super(context);     
  }

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
  {
   widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
   heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }  
  }

  public class SecondLevelAdapter extends BaseExpandableListAdapter
  {

   @Override
   public Object getChild(int groupPosition, int childPosition) 
   {   
    return childPosition;
   }

   @Override
   public long getChildId(int groupPosition, int childPosition) 
   {   
     return childPosition;
   }

   @Override
   public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
  {
   TextView tv = new TextView(Home.this);
    tv.setText("child");
    tv.setPadding(15, 5, 5, 5);
    tv.setBackgroundColor(Color.YELLOW);
    tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
    }

    @Override
    public int getChildrenCount(int groupPosition) 
    {
       return 5;
    }

     @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(Home.this);
   tv.setText("-->Second Level");
   tv.setPadding(12, 7, 7, 7);
   tv.setBackgroundColor(Color.RED);       
   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;
      }

  }
}