如果可扩展列表视图android中的孩子为空,如何单击组

Posted

技术标签:

【中文标题】如果可扩展列表视图android中的孩子为空,如何单击组【英文标题】:How to Click on group if child is empty in expandable listview android 【发布时间】:2015-05-31 08:15:10 【问题描述】:

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

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;

【问题讨论】:

我相信您假设在展开时单击可扩展的 Listview,您可以使用 onGroupClicklistener 来查看您的组是否被单击 是的,我写的。但对如何识别子元素是否为空感到困惑? 你必须发布你是如何实现你的可扩展列表视图模型的,你可以很容易地判断孩子是否为空 从您的代码中我可以告诉您的是,您无法读取为空的子项,因为如果您尝试它可能会出现空指针错误,那么它将为空,您可以做的是创建一个数据模型分别为组和项目的父和子,并使子类成为父类中的数组列表变量,这样您就可以在可扩展适配器中轻松检查您的组,如果您想要一些东西,我会给您一些代码 是的 pankanj...我需要一些代码。我尝试了很多,但没有希望。 【参考方案1】:

在此代码中,您将看到子项与父项相关联,因此对于给定的父项,您可以检查它是否为空或为空,并且列表中父项的给定位置,这样您就可以弄清楚您必须做什么如果子列表为空,则执行下一步

子.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;


父类.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_
android:layout_>

<ImageView
    android:layout_
    android:layout_
    android:id="@+id/imageView_icon_group"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"/>

<TextView
    android:id="@+id/textView_title_group"
    android:layout_
    android:layout_
    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_
android:layout_>

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

</LinearLayout>

【讨论】:

【参考方案2】:

在您将适配器设置为expandableListViewmainAcitivity

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

在内部调用其方法ExpandableListView.OnChildClickListener,通过传递 groupPosition 来获取子节点的数量,该 groupPosition 将返回与您传递的 groupPosition 相关的子节点的数量。 这是一个代码sn -p

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

【讨论】:

以上是关于如果可扩展列表视图android中的孩子为空,如何单击组的主要内容,如果未能解决你的问题,请参考以下文章

可扩展的列表视图子项动态计数

在可扩展列表视图android中折叠除选定组之外的所有组

如何在可扩展列表视图 Kotlin 中获取标题中的子项计数

如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图

如何在可扩展的listview android中将孩子添加到特定的父母

如何在可扩展列表视图中绑定 Json 数据?