【问题标题】:Expandable List View in AndroidAndroid 中的可扩展列表视图
【发布时间】:2014-05-05 08:40:23
【问题描述】:

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

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

假设我有 10 个标题和标题 3,5,而 8 没有任何子项。当我使用上面的代码时,它隐藏了第 3、5、8 个标题,但问题是它在那里留下了空白空间,它看起来不像一个列表。那么知道如何让它看起来像一个列表吗?

第二季度。当标题没有任何孩子时,我想显示一条吐司消息,说“数据不可用”。为此,我在 getGroupView 方法中使用了以下代码,

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

                }
            });

这显示了 toast 消息,但这里的问题是 - 当我单击具有子项的标题时,它们不会被展开。如何解决这个问题?

这是填充数据集的代码

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

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    让我先回答第二个问题,因为这样更直接。

    问题在于,当您设置自定义点击侦听器时,它会覆盖列表中的默认值。

    因此,您需要首先检查孩子的数量。如果计数为零,则仅添加侦听器。

    if ( getChildrenCount( groupPosition ) == 0 )
     convertView.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View arg0) {
                       Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();
    
                    }
                });
    

    现在,回到第一个问题。 我假设您有一个存储所有元素的数据集。在为可扩展列表视图创建适配器之前,遍历数据集并删除所有没有任何子元素的元素。在此之后,从数据集创建适配器。

    【讨论】:

    • 是的,我已经这样解决了。谢谢,我会接受你的回答。对 Q1 有什么想法吗?
    • 好的,谢谢! :) 我试图用几行来解释逻辑。还是不清楚吗?检查我的答案的更新,如果我需要详细说明,请告诉我。
    • 我明白你的意思。唯一的问题是我需要提出更好的逻辑。我已经添加了有问题的代码。你能帮我解决一下吗?
    • 给我一些时间。甚至我现在也想研究一下这件事,看看是否有更好的方法。如果我得到任何东西,我会告诉你的。
    • 好的。如果我想办法做到这一点,我会在这里发布。
    【解决方案2】:

    试试这个代码

       import java.util.HashMap;
       import java.util.List;
    
       import android.content.Context;
       import android.graphics.Typeface;
       import android.view.LayoutInflater;
       import android.view.View;
       import android.view.ViewGroup;
       import android.widget.BaseExpandableListAdapter;
       import android.widget.TextView;
    
       public class ExpandableListAdapter extends BaseExpandableListAdapter {
    
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    
    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
    
        final String childText = (String) getChild(groupPosition, childPosition);
    
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }
    
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
    
        txtListChild.setText(childText);
        return convertView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
    
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
    
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
    
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
    
        return convertView;
    }
    
    @Override
    public boolean hasStableIds() {
        return false;
    }
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    

    }

    使用以下链接http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/如果有用,请接受答案...给我学分

    【讨论】:

    • 很抱歉,我没有看到任何对解决我发布的两个问题有用的代码部分。我希望我的问题很清楚。无论如何,感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2013-10-29
    相关资源
    最近更新 更多