你好,我在android中使用了一个可扩展的列表视图.为了填充列表,我使用了hashmap,因为我想将相应的类别映射到它的子目录。我的hashmap类似于Hashmap> .Now,使用了以下代码:
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.as获取密钥集,set是无序的,我得到了无序的列表,.I想要有序的可扩展列表。那么如何安排我的单子呢??请尽快回复我。
发布于 2014-07-25 05:14:25
一种可能是您不使用HashMap或HashSet,而是使用TreeMap和TreeSet。您输入的对象中有一个正确的compareTo方法,或者您必须使用自定义比较器,这是您在TreeMap或TreeSet的构造函数中提供的。
然后对地图或集合进行排序:例如:
// auto sorted because Integer implements Comparable
SortedSet set = new TreeSet<Integer>();
// asume MyObject implements Comparable
SortedSet set = new TreeSet<MyObject>();
// asume MyObject needs a special other sorting
SortedSet set = new TreeSet<MyObject>(MyCustomComparator);
与地图相似:
Map map = new TreeMap<Integer>;
或者你也可以用
SortedMap map = new TreeMap<Integer>;
https://stackoverflow.com/questions/24956379
复制