您好我在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;
}
}
在此,設置用於獲取從Hashmap.as集鍵集是無序的,我得到。我希望在擴張無序形式列表以有序的形式列出。那麼如何讓我的列表有序?請儘快回覆我。
通過有序什麼?插入? – stevecross
我不知道我是否正確理解你的問題,但你問如何訂購清單? –
elvisrusu
entrySet()返回包含此Map中所有映射的集合。 – Blackbelt