我有一个可扩展的ListView,但我遇到了两个问题。 Q1。如果标题不包含任何子项我想隐藏/从列表中删除它。 为此,我有getGroupView方法。
if ( getChildrenCount( groupPosition ) == 0 )
{
convertView.setVisibility(View.GONE);
}
假设我有10个标题和标题3,5,而且8没有任何子节点。当我使用上面的代码时它会隐藏3,5,8th标题,但问题是它留下了空白区域,它看起来不像列表。那么任何想法如何让它看起来像一个列表?
Q2。我想在标题没有任何子节点时显示一条Toast消息,说“数据不可用”。为此,我在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);
答案 0 :(得分: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();
}
});
现在,回到第一个问题。 我假设您有一个数据集,您可以在其中存储所有元素。在为可扩展列表视图创建适配器之前,循环遍历数据集并删除所有不具有子项的元素。在此之后,从数据集创建适配器。
答案 1 :(得分:0)
试试此代码
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/如果有用,请接受ans ...给我信用