2014-07-25 40 views
1

您好我在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集鍵集是無序的,我得到。我希望在擴張無序形式列表以有序的形式列出。那麼如何讓我的列表有序?請儘快回覆我。

+0

通過有序什麼?插入? – stevecross

+0

我不知道我是否正確理解你的問題,但你問如何訂購清單? – elvisrusu

+0

entrySet()返回包含此Map中所有映射的集合。 – Blackbelt

回答

3

一種可能性是你不使用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>; 
+0

我會檢查這個和回扣.... – user2512822

+0

最後通過我自己修改了我的HashMap到LinkedHashMap,並將我的集合轉換爲LinkedHashset,因爲它允許我以插入的順序插入密鑰。 – user2512822

+0

整個問題可以通過在java中使用Collections.synchronised map Concept來解決。 – user2512822

相關問題