【发布时间】:2014-09-17 08:19:01
【问题描述】:
您好,我在 android 中使用了可扩展的列表视图。为了填充列表,我使用了一个 hashmap,因为我想将相应的类别映射到它的孩子。我的 hashmap 就像 Hashmap> 。现在使用以下代码:
public class ExpandableListAdapter extends BaseExpandableListAdapter
{
private Context _context;
// child data in format of header title, child title
private Map<SaleDetailsMenuItems, List<ModifList>> _listDataChild;
public ExpandableListAdapter(Context context, Map<SaleDetailsMenuItems, List<ModifList>> listChildData)
{
this._context = context;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon)
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
SaleDetailsMenuItems key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
//List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
//Log.e("Child in adapter get Child","get child "+this._listDataChild.get(key).get(childPosititon));
return this._listDataChild.get(key).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)
{
String childText = ((ModifList)getChild(groupPosition, childPosition)).getName();
String childText1 = "1";
String childText2 = ((ModifList)getChild(groupPosition, childPosition)).getPrice();
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);
TextView txtListChild1 = (TextView) convertView.findViewById(R.id.lblListItem1);
TextView txtListChild2 = (TextView) convertView.findViewById(R.id.lblListItem2);
txtListChild.setText(childText);
txtListChild1.setText(childText1);
txtListChild2.setText(childText2);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
SaleDetailsMenuItems key ;
try
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
// List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
//Log.e("Child size in adapter ","get children count "+this._listDataChild.get(key).size());
this._listDataChild.get(key).size();
}
catch(NullPointerException e)
{
e.printStackTrace();
return 0;
}
return this._listDataChild.get(key).size();
}
@Override
public Object getGroup(int groupPosition)
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
//List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
SaleDetailsMenuItems key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
//Log.e("adapter get group ","get group "+key);
return key;
}
@Override
public int getGroupCount()
{
//Log.e("Group Count","Group Count "+_listDataChild.keySet().size());
return _listDataChild.keySet().size();
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
final int index = groupPosition;
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(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);
TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.lblListquantity1);
TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.lblListprice1);
Button add = (Button) convertView.findViewById(R.id.btnadd);
Button minus = (Button) convertView.findViewById(R.id.Button01);
lblListHeader.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getName().toString());
lblListHeader1.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getQuantity().toString());
lblListHeader2.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getPrice().toString());
minus.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
listviewAdapter.notifyDataSetChanged();
putminusquantity(index);
//listviewAdapter.notifyDataSetChanged();
}
});
add.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//Toast.makeText(context, shankar, Toast.LENGTH_SHORT).show();
//DeleteOrderScreen del = new DeleteOrderScreen();
listviewAdapter.notifyDataSetChanged();
putaddquantity(index);
//listviewAdapter.notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
在此,set 用于从 Hashmap 获取键集。因为 set 是无序的,所以我得到了无序形式的列表。我想要有序形式的可扩展列表。那么如何使我的列表有序?请尽快回复我。
【问题讨论】:
-
按什么排序?插入?
-
我不知道我是否正确理解您的问题,但您是在问如何订购 List
吗? -
entrySet() 返回一个包含此 Map 中所有映射的 Set。
-
我希望它们按插入排序...
-
@elvisrusu 是的,但我在问如何将集合转换为列表
标签: java android list hashmap expandablelistview