3

我正在制作一个高分列表视图。ListView 有 2 个元素,播放器的名称和分数。分数是一个整数值。我正在使用 Collections.sort,但是,在启动活动时,列表未排序。我已经为列表加载了虚拟值。这些在 strings.xml 中声明为字符串数组。这是我的 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();

}

我的更新列表:

   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);
    }

}

我的获取详细信息类

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);
}

}

我的自定义适配器

  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;
    }
}

我的 Comparator 类用作排序的第二个参数(我什至不确定这是否需要!)

            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());
}
}

然而输出仍然是这样的:

在此处输入图像描述

有没有办法解决这个问题?

4

2 回答 2

3

每次添加不必要的值时,您都会对列表进行排序,并将分数排序为字符串,这不是您想要的。您可能想要转换为整数。的典型实现compareTo()是取一个值-另一个值,在您的情况下类似于:

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

而且您确实对升序进行了排序,这也可能不是您想要的。(我在上面的代码中通过切换objAobjB.

于 2012-10-10T11:59:41.070 回答
0

输出不正确吗?你没有按相反的顺序排序......

你可以使用类似的东西

Collections.reverseOrder();

或更改覆盖的比较方法...

于 2012-10-10T11:56:11.777 回答