2011-07-21 13 views
0

私はウェブサイトからデータを取得し、そのデータ値をハッシュマップに持っています。私はまた、動的な線形レイアウトを作成し、そこにolハッシュマップの値を表示します。名前とデータ型でハッシュマップの値を表示したいときに問題が発生します。どうすればいいですか?あなたはTreeMapのは自動的ので、キーをソートしAndroidでarraylist hashmapをソートするには?

TreeMap sortedMap1=new TreeMap<String, String>(HashMap Object); 

としてのTreeMapを使用して簡単にHashMapを並べ替えることができます

for (int i = 0; i < nodes.getLength(); i++) 


{ 


     Element e = (Element) nodes.item(i); 
     Node node = nodes.item(i); 
     HashMap<String, String> map = new HashMap<String, String>(); 

     lin = new LinearLayout(this); 
     lin.setOrientation(LinearLayout.VERTICAL); 
     lin.setBackgroundResource(R.drawable.my_border); 

     LinearLayout linbsc = new LinearLayout(this); 
     linbsc.setOrientation(LinearLayout.HORIZONTAL); 
     map.put("name",node.getAttributes().getNamedItem("name").getNodeValue()); 
     TextView txtbasicname = new TextView(this); 
     txtbasicname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1f));   
     //txtbasicname.setBackgroundColor(Color.WHITE); 
     txtbasicname.setTextColor(Color.WHITE); 
     String strbasicname =map.put("name",node.getAttributes().getNamedItem("name").getNodeValue()); 
     txtbasicname.setText(strbasicname); 
     linbsc.addView(txtbasicname); 


     lin.addView(linbsc); 

     LinearLayout linthrd = new LinearLayout(this); 
     linthrd.setOrientation(LinearLayout.HORIZONTAL); 

     map.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue()); 
     TextView txtbasicvenuetype = new TextView(this); 
     txtbasicvenuetype.setTextColor(Color.WHITE); 
     txtbasicvenuetype.setPadding(5, 0, 0, 0); 
     String strbasicvenuetype =map.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue()); 
     txtbasicvenuetype.setText(strbasicvenuetype+"-"); 
     linthrd.addView(txtbasicvenuetype); 



     lin.addView(linthrd); 

     LinearLayout linforth = new LinearLayout(this); 
     linforth.setOrientation(LinearLayout.HORIZONTAL); 
     map.put("address",node.getAttributes().getNamedItem("address").getNodeValue()); 
     TextView txtbasicaddress = new TextView(this); 
     txtbasicaddress.setPadding(5, 0, 0, 0); 
     txtbasicaddress.setTextColor(Color.WHITE); 
     String strbasicaddress =map.put("address",node.getAttributes().getNamedItem("address").getNodeValue()); 
     txtbasicaddress.setText(strbasicaddress+","); 
     linforth.addView(txtbasicaddress); 

     map.put("city",node.getAttributes().getNamedItem("city").getNodeValue()); 
     TextView txtbasiccity = new TextView(this); 
     txtbasiccity.setTextColor(Color.WHITE); 
     String strbasiccity =map.put("city",node.getAttributes().getNamedItem("city").getNodeValue()); 
     txtbasiccity.setText(strbasiccity+","); 
     linforth.addView(txtbasiccity); 


     LinearLayout linfifth = new LinearLayout(this); 
     linfifth.setOrientation(LinearLayout.HORIZONTAL); 

     map.put("zip",node.getAttributes().getNamedItem("zip").getNodeValue()); 
     TextView txtbasiczip = new TextView(this); 
     txtbasiczip.setTextColor(Color.WHITE); 
     txtbasiczip.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1f));   
     String strbasiczip =map.put("zip",node.getAttributes().getNamedItem("zip").getNodeValue()); 
     txtbasiczip.setText(strbasiczip+"."); 
     linfifth.addView(txtbasiczip); 

     lin.addView(linfifth); 
     linm.addView(lin); 
     dataList.add(map); 
     mylist.add(map); 




    } 


    System.out.println("data list = "+dataList); 
    System.out.println("data list = "+dataListfeture); 
+0

HashMapに格納するデータの詳細をキーと値として入力できますか?どのようなキーであり、どのようなデータが価値であるかのように。 –

答えて

1

、我々は(ソートのkeySetを取得することができます)

sortedMap1.keySet(); 
+0

私にいくつかの例を教えてください。私はそれをより良い方法で理解することができます – user846218

0

を次のようにTreeMapは、そのキーのデータをソートするSortedMapです。

hereが良い例です。

+0

おいしいチュートリアルですが、いくつかの詳細がほしいので、私はそれをより良い方法で管理することができます。 – user846218

0

次のようにHashMapを宣言し、任意の項目をマップに追加が自動的にソートされます。

// This will create a map that will order the keys, ignoring casing of the strings 
// (The 'String.CASE_INSENSITIVE_ORDER' can be removed if not wanted) 
TreeMap<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); 

あなたがしなければ意味する:

map.put("Hij", "145"); 
map.put("Abc", "321"); 
map.put("def", "123"); 

その後、出力は次のようになります。

( "ABC"、 "321") ( "DEF"、 "123") ( "量Hij"、 "145")

関連する問題