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 8653605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:35:04+00:00 2026-06-12T14:35:04+00:00

I am making a HighScore Listview. The ListView has 2 elements, the Name of

  • 0

I am making a HighScore Listview. The ListView has 2 elements, the Name of the player and the Score. The score is an integer value. I am using Collections.sort, however, upon starting the activity, the list isn’t sorted. I have already loaded dummy values for the list. These are declared in strings.xml as string arrays. Here is the snippet for my onCreate:

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    backgroundMusic = MediaPlayer.create(HighScores.this, R.raw.highscore);
    backgroundMusic.setLooping(true);
    backgroundMusic.start();

    String databasePath = getAbsolutePath("highscore.dbs");

    // open the database
    try {
        db = StorageFactory.getInstance().createStorage();
        db.open(databasePath, 40 * 1024);
        Toast.makeText(this, "HERE", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // check if a root object is present in this file
    Index<FetchDetails> root = (Index<FetchDetails>) db.getRoot();
    if (root == null) {
        // Root is not yet defined: storage is not initialized
        root = (Index) db.createIndex(String.class, false);
        String[] nameList = getResources().getStringArray(
                R.array.name_array);
        String[] scoreList = getResources().getStringArray(
                R.array.score_array);
        for (int i = 0; i < scoreList.length; i++) {
            FetchDetails populate = new FetchDetails();
            populate.setName(nameList[i]);
            populate.setScore(scoreList[i]);
            root.put(populate.getScore(), populate);
            db.setRoot(root);
        }

    }

    String filter = "";
    ArrayList<FetchDetails> items = root.getPrefixList(filter);
    results = new ArrayList<FetchDetails>();
    ScoreComparator compare = new ScoreComparator();
    for (int i = 0; i < items.size(); i++) {
        FetchDetails arraylist = new FetchDetails();
        arraylist.setName(items.get(i).getName());
        arraylist.setScore(items.get(i).getScore());
        results.add(arraylist);
        Collections.sort(results, compare);
    }

    adapter = new CustomAdapter(results);
    setListAdapter(adapter);
    updateList();

}

My updateList:

   private void updateList() {
    // TODO Auto-generated method stub
    Intent updateIntent = getIntent();
    if ((updateIntent.getStringExtra(HighScores.NAME) != null)
            &&   (updateIntent.getStringExtra(MegamanStrikes.PLAYER_SCORE) != null)) {
        FetchDetails updateList = new FetchDetails();
        ScoreComparator compare = new ScoreComparator();
        updateList.setName(updateIntent.getStringExtra(HighScores.NAME));
        updateList.setScore(updateIntent
                .getStringExtra(MegamanStrikes.PLAYER_SCORE));

        Toast.makeText(this, updateIntent.getStringExtra(MegamanStrikes.PLAYER_SCORE), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, updateIntent.getStringExtra(HighScores.NAME), Toast.LENGTH_SHORT).show();

        results.add(updateList);
        adapter.notifyDataSetChanged();
        Collections.sort(results, compare);

        Index<FetchDetails> rootEdit = (Index<FetchDetails>) db.getRoot();
        rootEdit.put(updateList.getScore(), updateList);
        db.setRoot(rootEdit);
    }

}

My Fetch Details class

package com.cs119.megamanstrikes;

import org.garret.perst.Persistent;

public class FetchDetails extends Persistent implements Comparable<FetchDetails> {

    private String name;
    private String score;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

@Override
public int compareTo(FetchDetails another) {
    // TODO Auto-generated method stub
    return score.compareTo(another.score);
}

}

My Custom Adapter

  private class CustomAdapter extends BaseAdapter {

    Index<FetchDetails> root = (Index<FetchDetails>) db.getRoot();
    String filter = "";
    ArrayList<FetchDetails> items = root.getPrefixList(filter);

    public CustomAdapter(ArrayList<FetchDetails> highscore) {
        items = highscore;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return items.get(index);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View view;

        if (convertView == null) {
            view = inflater.inflate(R.layout.row, null);
        } else {
            view = convertView;
        }

        // extract the views to be populated
        TextView Name = (TextView) view.findViewById(R.id.name);
        TextView Score = (TextView) view.findViewById(R.id.score);

        FetchDetails score = items.get(position);

        Name.setText(score.getName());
        Score.setText(score.getScore());

        // return the view
        return view;
    }
}

My Comparator class used as second parameter for sort (Im not even sure if this is needed!)

            package com.cs119.megamanstrikes;

import java.util.Comparator;


class ScoreComparator implements Comparator<FetchDetails> {

@Override
public int compare(FetchDetails objA, FetchDetails objB) {
    // TODO Auto-generated method stub
    return objA.getScore().compareTo(objB.getScore());
}
}

And yet the output is still like this:

enter image description here

Is there a way to remedy this?

  • 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-12T14:35:05+00:00Added an answer on June 12, 2026 at 2:35 pm

    You sort the list every time you add a value which unnecessary, and you sort the score as strings which is not what you want. You probably to want to convert to integers. A typical implementation of compareTo() is to take one value – the other so in your case something like:

    return Integer.parseInt(objB.getScore()) - Integer.parseInt(objA.getScore());
    

    And you did sort ascending which is probably not what you want either. (I fixed that in the code above by switching objA and objB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

making a thumbnail from video using the picker is straight forward. However, when I
Making a small WCF test program which is based on a Store that has
Making sense out of an .MSI verbose trace. I created the .MSI using VisualStudio
Making .htaccess (mod_rewrite) work has been very difficult I already have this script for
Making a footer for a web site I stumbled upon some strange behavior of
Im making a website where it has a user registration. What I was going
) I am making a SQL query to display a high score list of
Im making a simple score keeping system and each user can have stats for
I'm making a batch game, that counts a highscore in a separate file called
For a little iPhone application I am making, I want to sort a NSMutableArray.

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.