2

I receive data from a server using JSON and I want to order them alphabetically with alphabet indexed section and store them in a ListView. Maybe something that will happen in :

for(int i=0;i<jArray.length();i++){
// here
}

I read that you can order elements like that only using a cursor. In my case would be very inefficient to store the elements from the server in the database and read them again. Waste of time and memory.

So, I am asking you if there could be any solution for my problem : order alphabetically with alphabet indexed section string received from JSON .

EDIT: I want my listview to look like this http://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/ . I mean with those sections . All tutorials I found said that you need to fetch information with a cursor. My question was if I could't do this wihout a cursor, because it would be a waste of memory to store them in the local database too.

stanga bogdan
  • 724
  • 2
  • 8
  • 26
  • Hi, i believe best way to do is to get the sorted data from server itself. Servers have more computational power compared to a handheld device. This will speed up your process and will also save you from using cursors. – mudit Aug 13 '12 at 09:40
  • This link may give you some idea..http://stackoverflow.com/questions/7051785/when-jsonobject-keys-are-iterate-it-is-not-in-the-same-order-as-in-response-from – Narendra Pal Aug 13 '12 at 09:53
  • Yes, I know how to order. What I do not know is how to implement the Indexed Section. It is the thing that makes the scroll to pop out the first letter of your section. – stanga bogdan Aug 13 '12 at 10:03

2 Answers2

1

You may need to parse the JSON Array :

List<Project> list = new ArrayList<Project>();

for (int i = 0; i < jArray.length(); i++) {
    JSONObject obj = (JSONObject) jArray.get(i);
    project = new Project();
    project.setId( Long.parseLong(obj.get("id").toString()));
    project.setKey(obj.get("key").toString());
    project.setName(obj.get("name").toString());

    list.add(project);
}

You can use the comparator class like this to sort them :

Collections.sort(list), new Comparator<Project>() {
    public int compare(Project p1, Project p2) {
        return p1.getKey().compareToIgnoreCase(p2.getKey());
    }
});

You can also have Project class and implements Comparable:

public class Project implements Comparable<Project> {
   private long id;
   private String key;
   private String name;

   public int compareTo(Project p) {
    if (this.key > p.key)
        return -1;
    else if (this.key < p.key)
        return 1;
    return 0;
   }
}

And then sort the list by Collections.sort(list);

My suggestion is try to sort the data in the Server-side, because the memory of the phone is limited and it may make you application time consuming to show the data, but you do not have memory limitation problem in the Server-side.

Ali
  • 9,800
  • 19
  • 72
  • 152
  • 1
    I can sort the data in the server but I want to implement the Alphabetic Indexed Section. When I scroll to pop-out the first letter. That is my problem. – stanga bogdan Aug 13 '12 at 09:59
  • Ok, did you sort them in the client-side as the ways that I suggested? – Ali Aug 13 '12 at 10:03
  • Not yet, I receive the information sorted from the server so I do not need to sort here too. – stanga bogdan Aug 13 '12 at 10:05
  • The sorting instructions is the same in both server- and client- sides. So I think you can just follow the codes, and make your decision to sort in which side. Good luck! – Ali Aug 13 '12 at 10:09
  • Yes, thank you . But this was not my problem. You see, I want my listview to look like this http://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/ . I mean with those sections . All tutorials I found said that you need to fetch information with a cursor. My question was if I could't do this wihout a cursor, because it would be a waste of memory to store them in the local database too. – stanga bogdan Aug 13 '12 at 10:13
  • You can implement your own Adapter class which `extends ArrayAdapter or ListAdapter` and then inside it compare the letters. If it is a new one, add a new section. – Ali Aug 13 '12 at 10:18
0

use a comparator to sort the arraylist as described here . And then use an ArrayAdapter to show the items in Listview

Community
  • 1
  • 1
nandeesh
  • 24,740
  • 6
  • 69
  • 79