2012-10-10 33 views
3

我正在創建一個HighScore Listview。 ListView有2個元素,玩家的名字和得分。得分是一個整數值。我正在使用Collections.sort,但是,在開始活動時,列表未被排序。我已經爲列表加載了虛擬值。這些在strings.xml中聲明爲字符串數組。這裏是我的onCreate代碼段:Android:對ListView中的整數值進行排序

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

} 

我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; 
    } 
} 
作爲第二個參數進行排序

我比較級(!林如果需要甚至不知道)

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

然而輸出仍然是這樣的:

enter image description here

有一種方法來解決這個問題?

+0

我沒有看到問題,列表已經排序如果你想對它進行降序排序,請將comperators的返回值反向 –

回答

1

你每次排序列表中添加一個價值是不必要的,你將得分排序爲不是你想要的字符串。你可能想要轉換爲整數。的compareTo()一個典型的實現是一個值的 - 你的情況有點像其他這樣:

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

你做得升序這可能不是你想要什麼任。 (我在上面的代碼中通過切換objAobjB來解決這個問題

+0

這派上了用場,以及實際上,謝謝以及先生托比亞斯! – Erasmus

+0

我不如果沒有解釋原因,我們可以看到沒有解釋爲什麼的原因,這是什麼問題?這是不是回答了這個問題? –

+0

我不知道誰低估了它,但是你的建議完全回答了我的問題。 – Erasmus

0

輸出是否正確?你不能以相反的順序排序...

您可以使用類似

Collections.reverseOrder(); 

或更改重寫比較法...

+0

哇,err,I有點覺得自己很蠢。所有我需要做的就是reverseOrder。謝謝! – Erasmus

+0

見我的回答如果你想數字和字符串不排序... –

相關問題