如果在expandable listview android中子项为空,如何单击组

时间:2015-03-27 04:23:18

标签: android android-listview arraylist expandablelistview expandablelistadapter

我在展开式列表视图中遇到问题。实际上,组和子的详细信息来自服务器。有些组不包含任何子元素。所以我们如何使该组可点击,并在点击时移动到一个活动。当有孩子为团体时,它工作正常,当点击该孩子时,它移动到一个活动。组中没有子元素时出现问题。 非常感谢任何帮助。提前致谢...这是活动类

public class NewsTabActivity extends Activity {

public static String Id, LOGO, WEB_SERV, SUB_CAT;
ImageView image;
ImageLoader imgloader;
String URL_MAIN_CAT, nDrawerSubCat, URL_SUB_CAT, URL_HOME_PAGE,
        URL_MAIN_CAT_ONLY;
String[] mainCatName, mainCatId, subCatParentID;
JSONArray jarray, jarray1;
JSONObject jobject, jobject1;

private DrawerLayout mDrawerLayout;
private ListView homeListview, headerListView;
private ExpandableListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;

ArrayList<NewsList> newsList;
private ArrayList<NewsDrawerItem> navDrawerItems;
private NewsDrawerItem newsDrawerItems;

private ArrayList<NewsDrawerItem> listNewsHeader = null;
private ArrayList<NewsDrawerChildItem> listNewsChild = null;
private ArrayList<ArrayList<NewsDrawerChildItem>> listChildXXXXXXXXX = null;

NewsListAdapter adapter1;
private NewsDrawerListAdapter adapter;
private NewsDrawerHeaderAdapter adapterGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hometab);

    mTitle = mDrawerTitle = getTitle();

    image = (ImageView) findViewById(R.id.imageView1);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu);
    homeListview = (ListView) findViewById(R.id.listView1);
    headerListView = (ListView) findViewById(R.id.list_header);

    // homeListview.setVisibility(View.VISIBLE);
    // mDrawerList.setVisibility(View.VISIBLE);

    imgloader = new ImageLoader(NewsTabActivity.this);
    imgloader.DisplayImage(LOGO, image);
    nDrawerSubCat = SUB_CAT;

    navDrawerItems = new ArrayList<NewsDrawerItem>();
    newsList = new ArrayList<NewsList>();
    listNewsHeader = new ArrayList<NewsDrawerItem>();
    listNewsChild = new ArrayList<NewsDrawerChildItem>();// ArrayList<ArrayList<ChildTerbaru>>>();
    listChildXXXXXXXXX = new ArrayList<ArrayList<NewsDrawerChildItem>>();

    System.out.println("DRAWER_SUBCAT ? = " + nDrawerSubCat);
    URL_HOME_PAGE = WEB_SERV + "homenews.php";
    URL_MAIN_CAT = WEB_SERV + "maincategory.php";
    URL_MAIN_CAT_ONLY = WEB_SERV + "maincategory.php";

    if (nDrawerSubCat.equalsIgnoreCase("yes")) {
        URL_SUB_CAT = WEB_SERV + "subcategory.php";
        System.out.println("DRAWER_SUBCAT = " + URL_SUB_CAT);
    }

    if (nDrawerSubCat.equalsIgnoreCase("yes")) {
        mDrawerList.setOnChildClickListener(new SlideMenuClickListener());
    } else {
        headerListView
                .setOnItemClickListener(new SlideMenuClickHeaderListener());
    }

    if (WebserviceUtils.isConnectingToInternet(NewsTabActivity.this))   {
        new NewsHomePage().execute(URL_HOME_PAGE);
        if (nDrawerSubCat.equalsIgnoreCase("yes")) {
            new NewsPaperSubCat().execute();
        } else {
            new NewsPaperMainCat().execute(URL_MAIN_CAT_ONLY);

            // mDrawerList.setVisibility(View.GONE);
            // headerListView.setVisibility(View.VISIBLE);
            // adapterGroup = new
            // NewsDrawerHeaderAdapter(getApplicationContext(),
            // navDrawerItems);
            // headerListView.setAdapter(adapterGroup);
        }
    }

    // setting the nav drawer list adapter

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, // nav menu toggle icon
            R.string.app_name, // nav drawer open - description for
                                // accessibility
            R.string.app_name // nav drawer close - description for
                                // accessibility
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("News Live");
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("News Live");
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView();
    }

}

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickHeaderListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView();
        mDrawerList.setVisibility(View.GONE);
        headerListView.setVisibility(View.VISIBLE);
        NewsListActivity.valuesID(navDrawerItems.get(position).getTitle());
        NewsListActivity.valueWEBSERV(WEB_SERV);
        System.out.println("NEWS_LIST_ID = "
                + navDrawerItems.get(position).getTitle());
    }
}

private class SlideMenuClickListener implements
        ExpandableListView.OnChildClickListener,
        ExpandableListView.OnGroupClickListener {

    @Override
    public boolean onChildClick(ExpandableListView parent, View view,
            int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        // if (nDrawerSubCat.equalsIgnoreCase("no")) {
        // displayView();
        // homeListview.setVisibility(View.GONE);
        // NewsListActivity.valuesID(navDrawerItems.get(groupPosition)
        // .getId());
        // NewsListActivity.valueWEBSERV(WEB_SERV);
        // System.out.println("NEWS_EXP_GROUP_LIST_ID = "
        // + navDrawerItems.get(groupPosition).getId());
        // } else {
        displayView();
        homeListview.setVisibility(View.GONE);
        NewsListActivity.valuesID(listNewsChild.get(childPosition).getId());
        NewsListActivity.valueWEBSERV(WEB_SERV);
        System.out.println("NEWS_EXP_CHILD_LIST_ID = "
                + listNewsChild.get(childPosition).getId());
        // }
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id) {
        // TODO Auto-generated method stub
        displayView();
        // homeListview.setVisibility(View.GONE);
        NewsListActivity
                .valuesID(navDrawerItems.get(groupPosition).getId());
        NewsListActivity.valueWEBSERV(WEB_SERV);
        System.out.println("NEWS_EXP_GROUP_LIST_ID = "
                + navDrawerItems.get(groupPosition).getId());
        return false;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    return super.onOptionsItemSelected(item);
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView() {
    // update the main content by replacing fragments
    Fragment fragment = null;
    fragment = new NewsListActivity();

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        if (nDrawerSubCat.equalsIgnoreCase("yes")) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.closeDrawer(headerListView);
        }
    } else {
        // error in creating fragment
        Log.e("NewsTabActivity", "Error in creating fragment");
    }
}

private class NewsHomePage extends AsyncTask<String, String, String> {

    private ProgressDialog p_dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                "Loading...Please wait...", true);
        p_dialog.setCancelable(true);
        p_dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        JsonParsor json = new JsonParsor();
        JSONObject jobject = json.getJSONFromUrl(URL_HOME_PAGE);

        try {
            JSONArray jarray = jobject.getJSONArray("news");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject jobj = jarray.getJSONObject(i);
                String id = jobj.getString("Id");
                String cid = jobj.getString("c_id");
                String title = jobj.getString("title");
                String image = jobj.getString("image");

                NewsList news = new NewsList();
                news.setId(jobj.getString("Id"));
                news.setCid(jobj.getString("c_id"));
                news.setTitle(jobj.getString("title"));
                news.setImage(jobj.getString("image"));
                newsList.add(news);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return "sucess";
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        p_dialog.dismiss();
        if (result.compareTo("sucess") == 0) {
            navDrawerItems.clear();
            adapter1 = new NewsListAdapter(NewsTabActivity.this,
                    R.layout.newslist_single_item, newsList);
            homeListview.setAdapter(adapter1);
            adapter1.notifyDataSetChanged();
        } else {
            Toast.makeText(NewsTabActivity.this, "No data Found...",
                    Toast.LENGTH_LONG).show();
        }
    }

}

   private class NewsPaperSubCat extends AsyncTask<String, String, String> {

    private ProgressDialog p_dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                "Loading...Please wait...", true);
        p_dialog.setCancelable(true);
        p_dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        JsonParsor json = new JsonParsor();
        jobject = json.getJSONFromUrl(URL_MAIN_CAT);
        // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
        jobject1 = json.getJSONFromUrl(URL_SUB_CAT);
        // }

        try {
            if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                NewsDrawerItem LT = null;
                NewsDrawerChildItem CT = null;

                listNewsChild = new ArrayList<NewsDrawerChildItem>();

                jarray = jobject.getJSONArray("categories");
                // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                jarray1 = jobject1.getJSONArray("categories");
                // }

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject jobj = jarray.getJSONObject(i);

                    mainCatName = new String[jarray.length()];
                    mainCatId = new String[jarray.length()];

                    mainCatId[i] = jobj.getString("Id");
                    mainCatName[i] = jobj.getString("name");
                    System.out.println("mainCatId[" + i + "] = "
                            + mainCatId[i]);

                    String id = jobj.getString("Id");
                    String name = jobj.getString("name");

                    System.out
                            .println("MAIN_ID = " + id + " NAME= " + name);

                    // adding nav drawer items to array
                    LT = new NewsDrawerItem(jobj.getString("Id"),
                            jobj.getString("name"));
                    listNewsHeader.add(LT);

                    // int listsize = listNewsHeader.size();
                    // for(int k=0 ; k<listsize; k++) {
                    // System.out.println("HEADER_VALUE["+k+"] = "+listNewsHeader.get(k));
                    // }
                }

                // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                for (int i = 0; i < jarray1.length(); i++) {
                    JSONObject jobj = jarray1.getJSONObject(i);

                    subCatParentID = new String[jarray1.length()];
                    subCatParentID[i] = jobj.getString("parent_id");
                    System.out.println("mainCatId_SubCatId[" + i + " = "
                            + subCatParentID[i]);

                    CT = new NewsDrawerChildItem(jobj.getString("Id"),
                            jobj.getString("name"));

                    System.out.println("SUB_MAIN_ID = "
                            + jobj.getString("Id") + " NAME= "
                            + jobj.getString("name"));

                    // if(mainCatId[i].contains(subCatParentID[i])) {
                    // String[] temp = new String[mainCatId.length];
                    // temp[i] = jobj.getString("Id");
                    // System.out.println("mainCatId_Temp = "+ temp[i]);
                    // }
                    List similarities = new ArrayList();
                    for (int j = 0; j < Math.max(mainCatId.length,
                            subCatParentID.length); j++) {
                        if (mainCatId[i] == subCatParentID[i])
                            similarities.add(mainCatId[i]);
                        System.out.println("VALU = " + similarities);
                    }

                    listNewsChild.add(CT);
                    listChildXXXXXXXXX.add(listNewsChild);
                }
                // }
            }
            // else {
            // jarray = jobject.getJSONArray("categories");
            //
            // for (int i = 0; i < jarray.length(); i++) {
            // JSONObject jobj = jarray.getJSONObject(i);
            //
            // newsDrawerItems = new NewsDrawerItem();
            //
            // mainCatName = new String[jarray.length()];
            // mainCatId = new String[jarray.length()];
            //
            // mainCatId[i] = jobj.getString("Id");
            // mainCatName[i] = jobj.getString("name");
            //
            // String id = jobj.getString("Id");
            // String name = jobj.getString("name");
            //
            // System.out.println("MAIN_ID = " + id + " NAME= " + name);
            //
            // // adding nav drawer items to array
            // // navDrawerItems.add(new NewsDrawerItem(mainCatName[i],
            // mainCatId[i]));
            // }
            // }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return "sucess";
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        p_dialog.dismiss();
        super.onPostExecute(result);

        if (result.compareTo("sucess") == 0) {
            if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                mDrawerList.setVisibility(View.GONE);
                adapter = new NewsDrawerListAdapter(NewsTabActivity.this,
                        listNewsHeader, listChildXXXXXXXXX);
                mDrawerList.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        } else {
            Toast.makeText(NewsTabActivity.this, "No data Found...",
                    Toast.LENGTH_LONG).show();
        }
    }
}

private class NewsPaperMainCat extends AsyncTask<String, String, String> {

    private ProgressDialog p_dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                "Loading...Please wait...", true);
        p_dialog.setCancelable(true);
        p_dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        JsonParsor json1 = new JsonParsor();
        JSONObject jObject = json1.getJSONFromUrl(URL_MAIN_CAT_ONLY);

        try {
            NewsDrawerItem NDI = null;

            JSONArray jarr = jObject.getJSONArray("categories");

            for (int i = 0; i < jarr.length(); i++) {
                JSONObject jObj = jarr.getJSONObject(i);

                mainCatName = new String[jarr.length()];
                mainCatId = new String[jarr.length()];

                mainCatId[i] = jObj.getString("Id");
                mainCatName[i] = jObj.getString("name");

                String id = jObj.getString("Id");
                String name = jObj.getString("name");

                System.out.println("MAIN_ID_ONLY = " + id
                        + " MAIN_NAME_ONLY= " + name);

                // adding nav drawer items to array
                navDrawerItems.add(new NewsDrawerItem(mainCatName[i],
                        mainCatId[i]));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return "sucess";
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        p_dialog.dismiss();
        super.onPostExecute(result);

        if (result.compareTo("sucess") == 0) {
            mDrawerList.setVisibility(View.GONE);
            headerListView.setVisibility(View.VISIBLE);
            adapterGroup = new NewsDrawerHeaderAdapter(
                    NewsTabActivity.this, navDrawerItems);
            headerListView.setAdapter(adapterGroup);
            adapterGroup.notifyDataSetChanged();
        } else
            Toast.makeText(NewsTabActivity.this, "No data Found...",
                    Toast.LENGTH_LONG).show();
    }
}

public static void valueID(String id) {
    // TODO Auto-generated method stub
    Id = id;
}

public static void valueLogo(String logo) {
    // TODO Auto-generated method stub
    LOGO = logo;
}

public static void valueWebService(String webServ) {
    // TODO Auto-generated method stub
    WEB_SERV = webServ;
}

public static void valueSubCat(String subCat) {
    // TODO Auto-generated method stub
    SUB_CAT = subCat;
}

}

这是可扩展适配器类

 public class NewsDrawerHeaderAdapter extends BaseAdapter {

private Context context;
private ArrayList<NewsDrawerItem> navDrawerItems;

public NewsDrawerHeaderAdapter(Context context,
        ArrayList<NewsDrawerItem> navDrawerItems) {
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return navDrawerItems.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

    txtTitle.setText(Html.fromHtml(navDrawerItems.get(position).getId()));
    return convertView;
}

}

这是模型类

public class NewsDrawerItem {

private String title, Id, PId;
private int icon;

private ArrayList<NewsDrawerItem> childItemList = new ArrayList<NewsDrawerItem>();

public NewsDrawerItem() {
}

public NewsDrawerItem(String Id, String title) {
    this.Id = Id;
    this.title = title;
}

public NewsDrawerItem(String Id, String title, String PId) {
    this.Id = Id;
    this.title = title;
    this.PId = PId;
}

public String getTitle() {
    return this.title;
}

public String getId() {
    return this.Id;
}

public String getPId() {
    return this.PId;
}


public int getIcon() {
    return this.icon;
}

public ArrayList<NewsDrawerItem> getChildItem() {
    System.out.println("CHILD_ITEM = "+childItemList);
    return childItemList;
}

public void setTitle(String title) {
    this.title = title;
}

public void setId(String Id) {
    this.Id = Id;
}

public void setPId(String PId) {
    this.PId = PId;
}

public void setIcon(int icon) {
    this.icon = icon;
}

public void setChildItem(ArrayList<NewsDrawerItem> childItemList) {
    this.childItemList = childItemList;
}

}

2 个答案:

答案 0 :(得分:1)

在此代码中,您将看到该子项与父项关联,因此对于给定的父项,您可以检查它是否为空或者列表中父项的给定位置为空,这样您就可以找出所需的内容如果子列表为空,请执行下一步

Child.java

public class Child {


private String _Title_child = "";

public String get_Title_child() {
    return _Title_child;
}

public void set_Title_child(String _Title_child) {
    this._Title_child = _Title_child;
}
}

Parent.java

public class Parent {

private int _icon_ID = 0;
private String _Title_parent = "";
private ArrayList<Child> _Children = null;

public int get_icon_ID() {
    return _icon_ID;
}

public void set_icon_ID(int _icon_ID) {
    this._icon_ID = _icon_ID;
}

public String get_Title_parent() {
    return _Title_parent;
}

public void set_Title_parent(String _Title_parent) {
    this._Title_parent = _Title_parent;
}

public ArrayList<Child> get_Children() {
    return _Children;
}

public void set_Children(ArrayList<Child> _Children) {
    this._Children = _Children;
}
}

适配器

public class MyExpandableAdapter extends BaseExpandableListAdapter{

private Context _Context;
private ArrayList<Parent> _Parents;
private LayoutInflater _Inflater;
private static MyExpandableAdapter adapter = new MyExpandableAdapter();

private MyExpandableAdapter(){}

public static MyExpandableAdapter deInitialization(Context context, ArrayList<Parent> parents){
    adapter._Context = context;
    adapter._Parents = parents;
    adapter._Inflater = LayoutInflater.from(adapter._Context);
    return adapter;
}


@Override
public int getGroupCount() {
    return _Parents.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return _Parents.get(groupPosition).get_Children().size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return _Parents.get(groupPosition).get_Children().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) {
    if(convertView == null){
        convertView = _Inflater.inflate(R.layout.list_group,parent,false);
    }
    ImageView icon_Group = (ImageView)convertView.findViewById(R.id.imageView_icon_group);
    TextView title_group = (TextView)convertView.findViewById(R.id.textView_title_group);

    icon_Group.setImageResource(_Parents.get(groupPosition).get_icon_ID());
    title_group.setText(_Parents.get(groupPosition).get_Title_parent());

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if(convertView==null){
        convertView = _Inflater.inflate(R.layout.list_item,parent,false);
    }

    TextView title_child = (TextView)convertView.findViewById(R.id.textView_title_child);
    title_child.setText(_Parents.get(groupPosition).get_Children().get(childPosition).get_Title_child());

    return convertView;
}

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

list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="4dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="40dip"
    android:layout_height="40dip"
    android:id="@+id/imageView_icon_group"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"/>

<TextView
    android:id="@+id/textView_title_group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:padding="4dp"
    android:textSize="18dip"
    android:text="Parent Item"
    android:textColor="#c0000000" />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/textView_title_child"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="14dip"
    android:text="Child Item"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="40dp" />

</LinearLayout>

答案 1 :(得分:0)

mainAcitivity中,您将适配器设置为expandableListView

expandableListAdapter = new ExpandableListAdapter(headerList, hashMap);
        listViewExpandable.setAdapter(expandableListAdapter);

通过传递groupPosition调用其方法ExpandableListView.OnChildClickListener来获取子计数,该groupPosition将返回与您传递的groupPosition相关的子项数。
这是一段代码片段

listViewExpandable.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
                                        long id) {

                Log.e("GroupClicked"," Position = " + groupPosition;

                int ChildCountWay1 = expandableListAdapter .getChildrenCount(groupPosition);
          // OR
                int childCountWay2 = parent.getExpandableListAdapter().getChildrenCount(groupPosition);
                if (childCountWay2<1) {
                    // do whatever you want
                }

                return true;
            }
        });