扩展列表视图中的Andr​​oid [英] Expandable List View in Android

查看:211
本文介绍了扩展列表视图中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可扩展的ListView,但我面临两个问题。
Q1。从列表中如果标题不包含任何的孩子,我想隐藏/删除它。
为此,我有getGroupView方法中。

 如果(getChildrenCount(groupPosition)== 0)
{
   convertView.setVisibility(View.GONE);
}

让我们说,我有10头和头3,5和8没有任何子女。当我使用上述code将其隐藏3,5,8th头,但问题是它留下空白那里,它不会像列表。因此,任何想法如何使它看起来像一个列表?

Q2。我想显示敬酒消息说数据不可用当标题没有任何子女。为此,我已在getGroupView方法使用低于code,

  convertView.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(查看为arg0){
                    // TODO自动生成方法存根
                    如果(getChildrenCount(groupPosition)== 0)
                        Toast.makeText(_context,无可用数据,Toast.LENGTH_SHORT).show();                }
            });

这显示敬酒消息,但这里的问题是 - 这有孩子的头,当我点击他们没有得到扩大。如何解决这个问题呢?

下面是填充数据集的code

  listDataHeader =新的ArrayList<串GT;();
listDataChild =新的HashMap<字符串列表<串GT;>();    listDataHeader.add(dayName_first);
    listDataHeader.add(dayName_second);
    listDataHeader.add(dayName_third);
    listDataHeader.add(dayName_fourth);
    listDataHeader.add(dayName_fifth);
    listDataHeader.add(dayName_sixth);
    listDataHeader.add(dayName_seventh);    firstDay = checkEmpty(firstDay); //函数来检查列表是否为空。我需要在这里检查每个列表和添加或别的地方?
    listDataChild.put(listDataHeader.get(0),firstDay);
    listDataChild.put(listDataHeader.get(1),secondDay);
    listDataChild.put(listDataHeader.get(2),thirdDay);
    listDataChild.put(listDataHeader.get(3),fourthDay);
    listDataChild.put(listDataHeader.get(4),fifthDay);
    listDataChild.put(listDataHeader.get(5),sixthDay);
    listDataChild.put(listDataHeader.get(6),seventhDay);    listAdapter =新StaffExpandableListAdapter(这一点,listDataHeader,
            listDataChild);
    llExpandable.invalidateViews();    llExpandable.setAdapter(listAdapter);


解决方案

让我先回答第二个问题,因为这是比较简单的。

问题是,当你设置你自定义的点击监听器,它会覆盖列表的默认之一。

所以,你需要做的是为孩子计数第一次检查。如果计数是零,则仅添加听者

 如果(getChildrenCount(groupPosition)== 0)
 convertView.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(查看为arg0){
                   Toast.makeText(_context,无可用数据,Toast.LENGTH_SHORT).show();                }
            });

现在,回来的第一个问题。
我假设你已经在你存储所有的要素数据集。在创建的适配器可扩展列表视图,遍历数据集,并删除所有没有任何儿童的元素。在此之后,创建从数据集中的适配器。

I have an Expandable ListView but I'm facing two problems. Q1. If the header doesn't contain any children I would like to hide/remove it from the list. For this I have inside getGroupView method.

if ( getChildrenCount( groupPosition ) == 0 ) 
{
   convertView.setVisibility(View.GONE);
}

Let's say that I have 10 headers and the header 3,5, and 8 doesn't have any children. When I use the above code it hides the 3,5,8th headers but the problem is it leaves blank space there and it won't look like a list. So any idea how to make it look like a list ?

Q2. I want to display a toast message saying "Data is not available" when the header doesn't have any children. For this I have used below code in getGroupView method,

     convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if ( getChildrenCount( groupPosition ) == 0 )
                        Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();

                }
            });

This shows the toast message but the problem here is - the headers which have children doesn't get expanded when I click on them. How to solve this problem?

Here is the code of populating the dataset

listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

    listDataHeader.add(dayName_first);
    listDataHeader.add(dayName_second);
    listDataHeader.add(dayName_third);
    listDataHeader.add(dayName_fourth);
    listDataHeader.add(dayName_fifth);
    listDataHeader.add(dayName_sixth);
    listDataHeader.add(dayName_seventh);

    firstDay = checkEmpty(firstDay);  // Function to check whether the list is empty or not. Do I need to check each list here and add or somewhere else?
    listDataChild.put(listDataHeader.get(0), firstDay);
    listDataChild.put(listDataHeader.get(1), secondDay);
    listDataChild.put(listDataHeader.get(2), thirdDay);
    listDataChild.put(listDataHeader.get(3), fourthDay);
    listDataChild.put(listDataHeader.get(4), fifthDay);
    listDataChild.put(listDataHeader.get(5), sixthDay);
    listDataChild.put(listDataHeader.get(6), seventhDay);

    listAdapter = new StaffExpandableListAdapter(this, listDataHeader,
            listDataChild);
    llExpandable.invalidateViews();

    llExpandable.setAdapter(listAdapter);

解决方案

Let me answer the second question first, because that is more straightforward.

The problem is that when you set you custom click listener, it overrides the default one of the list.

So, what you need to do is first check for the child count. If the count is zero, then only add the listener.

if ( getChildrenCount( groupPosition ) == 0 )
 convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                   Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();

                }
            });

Now, coming back to the first question. I am assuming that you have a dataset in which you store all the elements. Before you create the adapter for the expandable listview, loop through the dataset and remove all the elements which don't have any children. After this, create the adapter from the dataset.

这篇关于扩展列表视图中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆