【问题标题】:How to convert a set to list ? The set is used as a key in Hashmap如何将集合转换为列表?该集合用作Hashmap中的键
【发布时间】: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


【解决方案1】:

一种可能性是您不使用 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>;

【讨论】:

  • 我自己终于得到了解决方案,只需将我的 HashMap 修改为 LinkedHashMap,并将我的集合转换为 LinkedHashset,因为它允许我按插入顺序插入键..
  • 在java中使用Collections.synchronised map Concept解决了整个问题。
猜你喜欢
  • 2017-02-06
  • 2013-12-16
  • 2010-10-09
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2017-12-24
  • 1970-01-01
相关资源
最近更新 更多