ArrayList<HashMap<String, Object>>

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ArrayList<HashMap<String, Object>>

Postby aminaz » Fri Jan 29, 2010 11:56 am

Good Morning
I have a big problem i want to sort a list like this
ArrayList<HashMap<String, Object>> list1 = new ArrayList<HashMap<String, Object>
Every Hashmap is like this :
if(c1.getCount()>0){

for(int i=0; i<c1.getCount();i++)
{
c1.moveToNext();
itemPouvoir = new HashMap<String, Object>();
itemPouvoir.put("nom",c1.getString(c1.getColumnIndex(basemobile.KEY_NAME)));
itemPouvoir.put("phone",c1.getString(c1.getColumnIndex(basemobile.KEY_PHONE)));

list1.add(itemPouvoir);
}
}
And i want to sort the names in my list in alphabetic order
Thnks a lot
aminaz
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jan 07, 2010 5:07 pm

Top

Postby spring » Fri Jan 29, 2010 12:25 pm

You use a HashMap to hold the structure data?I think it's not a good practice.

Obviously,the data you will operate is abstractive,you should define a class to do that

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class ContractRecord  implements Comparable<Record>{
  2.  
  3.  
  4.  
  5.   private String name;
  6.  
  7.   private String phone;
  8.  
  9.  
  10.  
  11.   @Override
  12.  
  13.         public int compareTo(Record another) {
  14.  
  15.                
  16.  
  17.                 return this.name.compareTo(another.getName());
  18.  
  19.  
  20.  
  21.         }
  22.  
  23. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


then you can overwrite the compareTo method to campare two record acroding your condition.

and hold these objects in an ArrayList like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayList<ContractRecord> list;
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


so,you can sort them by the way which you wanted.
a simplest way is

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Arrays.sort(list.toArray());
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests