Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:36:14+00:00 2026-06-15T10:36:14+00:00

I am having a bit of trouble with android. I have a sorted listview

  • 0

I am having a bit of trouble with android. I have a sorted listview of items retrieved from a database using xml parsing.

i have to display the product on list view with sorted by price

This is my xml feed:

<Feed>
<category>
<Product>
<Name>New Masters of Flash</Name>
<Price>79.99</Price>
</Product>
<Product>
<Name>Professional Java Server Programming</Name>
<Price>63.99</Price>
</Product>
<Product>
<Name>Designing Web Usability</Name>
<Price>80.00</Price>
</Product>
</category>
 </Feed>

This is my android code:

public class Catalogue extends Activity {

// static String URL = "https://dl.dropbox.com/u/48258247/catalogue.json";
static final String URL = "http://192.168.1.168/xcart432pro/internet.xml";

 static String KEY_CATEGORY = "Product";
 static final String KEY_TITLE = "Name";

static final String KEY_DESCRIPTION = "Description";
static final String KEY_COST = "Price"; 
static final String KEY_THUMB_URL = "Image";

ListView list;
ListAdapter adapter;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);

        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key =&gt; value

            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
           map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
            map.put(KEY_COST, parser.getValue(e, KEY_COST));
          map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

        list=(ListView)findViewById(R.id.listView1);

        // Getting adapter by passing xml data ArrayList
        adapter=new ListAdapter(this, songsList);
        list.setAdapter(adapter);
       // Bundle bundle = getIntent().getExtras();

        // KEY_CATEGORY=bundle.getString(KEY_SUBCATE); 


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

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                HashMap<String, String> map = songsList.get(position);
                Intent in = new Intent(
                        Catalogue.this,
                        com.ssmobileproductions.catalogue.SingleMenuItem.class);
                in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
                in.putExtra(KEY_DESCRIPTION, map.get(KEY_DESCRIPTION));
                in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL));
                in.putExtra(KEY_COST, map.get(KEY_COST));
                startActivity(in);

            }

        });

Here i have to display the product on list view with sorted by price.How can i do.please help me programmatically in android.

Edit:
I have added some code like below:

Button btninsert = (Button) findViewById(R.id.sort);
   btninsert.setOnClickListener(new View.OnClickListener() {

        final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    public void onClick(View v) {
        Collections.sort(songsList, new PriceComparator());
    }
    }); 
          final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);

create one class and wrote the code below:

public class PriceComparator implements Comparator<HashMap<String, String>> {
static final String KEY_COST = "Price"; 

public PriceComparator() {
    // TODO Auto-generated constructor stub

}



public int compare(HashMap<String, String> map1, HashMap<String, String> map2) {
    return map1.get(KEY_COST).compareTo(map2.get(KEY_COST));
}

Now i have to run the app and click the button means nothing is happened????

But i have to display product list is sorted by price.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:36:15+00:00Added an answer on June 15, 2026 at 10:36 am

    I believe you need to use a custom Comparator:

    Comparator<HashMap<String, String>> comparator = new Comparator<HashMap<String, String>>() {
        @Override
        public int compare(HashMap<String, String> map1, HashMap<String, String> map2) {
            return map1.get(KEY_COST).compareTo(map2.get(KEY_COST));
        }
    };
    

    And then sort your list before creating your adapter with:

    Collections.sort(songsList, comparator);
    

    Addition
    You need to do make a few small changes.

    1. Make songsList a field variable, like list and adapter:

      ListView list;
      ListAdapter adapter;
      ArrayList<HashMap<String, String>> songsList; // Add me!
      
    2. Update how you initialize songsList:

      songsList = new ArrayList<HashMap<String, String>>(); // Shorten me!
      
    3. Change your onClick method:

      public void onClick(View v) {
          Collections.sort(songsList, comparator);
          adapter.notifyDataSetChanged(); // Add me!
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of trouble using the List.Find with a custom predicate i have
I am having a bit of trouble with ADO. I have deployed a database
Having a bit of trouble parsing xml with dom and DocumentBuilder. I'm able to
having a bit of trouble adding some data to a database. I have the
I having a bit of trouble parsing an xml file with a namespace XML
I'm having a bit of trouble with jQuery Deferred. I'm using a jQuery ajax
I recently downloaded iText 5.3.3 and i'm having a bit of a trouble using
Im having a bit of trouble changing the the weather degrees from fahrenheit to
I am having a bit of trouble figuring this one out, basically I have
I'm having a bit of trouble getting the right results from a query. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.