简体   繁体   中英

How to sort ArrayList<String> containing elements of ArrayList<HashMap<String, String>> that has been sorted by a comparator?

I am getting some data via JSON which I store in a Hashmap map eg map.put(KEY_TITLE, parser.getString("title")) and then add the map to an ArrayList> bookList . This is displayed in a list view with bookList as source for the adapter. Additionally I also store all the book info (title, author, published date) of the map in Arraylist arrTitles, arrAuthors, arrDates as I need to pass these values to my next activity. My problem is when I use a comparator to sort the bookList on basis of titles then the bookList gets sorted just fine. I however want to sort my arrTitles, arrAuthors and arrDates as per the new sorting of the bookList so that my arrTitles.getItem(position) sends the correct value to the next activity. Any idea how I can do this? Below are the hashmap, arraylist, onItemClick & comparator code:

 for (int i = 0; i < json.Length(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        // adding each child node to HashMap key => value
        map.put(KEY_ID, json.getString("id"));
        map.put(KEY_TITLE, json.getString("title"));
        map.put(KEY_AUTHOR, json.getString("author"));
        map.put(KEY_DATE, json.getString("date"));
        arrTitles.add(json.getString("title");
        arrAuthors.add(json.getString("author");
        arrDates.add(json.getString("date");
        //add map to ArrayList
        booksList.add(map);
}

        list=(ListView)findViewById(R.id.list);
        Collections.sort(booksList, mapComparator);
        adapter=new LazyAdapter(this, booksList);        
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        Intent intent = new Intent(this, editList.class);
        intent.putExtra("title", arrTitles.getItem(position));
        intent.putExtra("date", arrDates.getItem(position));
        startActivity(intent);


        }
    }); 
 } 

       public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
       public int compare(Map<String, String> m1, Map<String, String> m2) {
       return m1.get(KEY_TITLE).compareTo(m2.get(KEY_TITLE));
    }
};

Right, with the new code it seems reasonably simple: just create the individual lists after you've sorted the list of maps:

// This loop looks wrong at the moment - you're not using i
for (int i = 0; i < json.Length(); i++) {
     HashMap<String, String> map = new HashMap<String, String>();
     // adding each child node to HashMap key => value
     map.put(KEY_ID, json.getString("id"));
     map.put(KEY_TITLE, json.getString("title"));
     map.put(KEY_AUTHOR, json.getString("author"));
     map.put(KEY_DATE, json.getString("date"));
     booksList.add(map);
}

for (Map<String, String> map : booksList) {
     arrTitles.add(map.get(KEY_TITLE));
     arrAuthors.add(map.get(KEY_AUTHOR));
     arrDates.add(map.get(KEY_DATE));
}

Note that I would still strongly recommend creating a Book class to store the data in, instead of using a Map<String, String> with fixed keys. It'll be a lot simpler to work with elsewhere in the code, I suspect.

Your code is correct only. You have to format as per requirement. you getting arrTitles, arrAuthors and arrDates from the json object. in my understand you have to get the data after shot the map. Below is some modify code. have look on this

for (int i = 0; i < json.Length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map.put(KEY_ID, json.getString("id"));
            map.put(KEY_TITLE, json.getString("title"));
            map.put(KEY_AUTHOR, json.getString("author"));
            map.put(KEY_DATE, json.getString("date"));

            //add map to ArrayList
            booksList.add(map);
    }





            list=(ListView)findViewById(R.id.list);
            Collections.sort(booksList, mapComparator);
            adapter=new LazyAdapter(this, booksList);        
            list.setAdapter(adapter);

        for (Map<String, String> map : booksList) {
                arrTitles.add(json.getString("title");
                arrAuthors.add(json.getString("author");
                arrDates.add(json.getString("date");
        }

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            Intent intent = new Intent(this, editList.class);
            intent.putExtra("title", arrTitles.getItem(position));
            intent.putExtra("date", arrDates.getItem(position));
            startActivity(intent);


            }
        });

书本列表中的地图具有所有必需的详细信息,所以为什么不从适配器中获取该特定列表项的地图对象,而从地图中获取详细信息。

@gvmani: I think you are referring to an implementation similar to the one I have done below:-

            HashMap <String,String> mapBook = booksList.get(position);
            String strTitle = mapBook.get(KEY_TITLE);
            String strAuthor = mapBook.get(KEY_AUTHOR);

            Intent intent = new Intent(ListView.this,editList.class);
            intent.putExtra("title", strTitle);
            intent.putExtra("author", strAuthor);
            startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM