0

我想按最高值对我的数组(从 JSON 解析)进行排序。我想做一个高分榜。

我的 JSON 文件:

[   
    {
    "naam": "Peter",
    "wedstrijden": "10",
    "doelpunten": "4"
    },
    {
    "naam": "Harry",
    "wedstrijden": "8",
    "doelpunten": "6"
    }    
]

我必须将哪些代码按“doelpunten”对我的列表视图进行排序?或者这是不可能的?这是我到目前为止的代码:

TopscoreListAdapter.java:

public class TopscoreListAdapter extends ArrayAdapter<Topscore> {
Context context;
List<Topscore> posities;

public TopscoreListAdapter(Context context, List<Topscore> posities) {
    super(context,R.layout.layout_topscorelist, posities);
    this.context = context;
    this.posities = posities;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    TopscoreView  view = (TopscoreView) convertView;

    view = new TopscoreView(context);
    view.setTopscore(posities.get(position));

    return view;

}

Topscore.java:

public class Topscore {

String name;
String wedstrijden;
String doelpunten;

public Topscore(String name, String wedstrijden, String doelpunten) {

    this.name = name;
    this.wedstrijden = wedstrijden;
    this.doelpunten = doelpunten;

}

public String GetName() {
    return name;
}

public String GetWedstrijden() {
    return wedstrijden;
}

public String GetDoelpunten() {
    return doelpunten;
}

}

TopscoreFragment.java:

public class TopscoreFragment extends ListFragment {
private static final String url = "http://www...../spelers.json";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View retView = inflater.inflate(R.layout.fragment_topscore, container,
            false);

    JsonTopscoreParser parser = new JsonTopscoreParser(url,
            new OnTopscoreCompleteListener() {

                @Override
                public void onComplete(ArrayList<Topscore> topscore) {
                    // TODO Auto-generated method stub
                    TopscoreListAdapter adapter = new TopscoreListAdapter(
                            getActivity(), topscore);
                    TopscoreFragment.this.setListAdapter(adapter);

                }

            });
    parser.execute();

    return retView;
}
}

OnTopscoreCompleteListener.java:

public interface OnTopscoreCompleteListener {

void onComplete(ArrayList<Topscore> bedrijven);
}

JsonTopscoreParser.java:

public class JsonTopscoreParser extends AsyncTask<Void, Void, ArrayList<Topscore>> {

private String url;
private ArrayList<Topscore> bosmtiTopscore;
private OnTopscoreCompleteListener listener;

public JsonTopscoreParser(String url, OnTopscoreCompleteListener listener) {
    this.listener = listener;
    this.url = url;
    bosmtiTopscore = new ArrayList<Topscore>();
}


public String readBosMtiFeed() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);

    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e(JsonTopscoreParser.class.toString(), "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return builder.toString();
}

@Override
protected ArrayList<Topscore> doInBackground(Void... params) {
    // TODO Auto-generated method stub
    String readBosMtiFeed = readBosMtiFeed();
    try {
        JSONArray jsonArray = new JSONArray(readBosMtiFeed);
        Log.i(JsonTopscoreParser.class.getName(),
                "Number of entries " + jsonArray.length());

        Topscore topscore;

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String naam = jsonObject.getString("naam");
            String wedstrijden = jsonObject.getString("wedstrijden");
            String doelpunten = jsonObject.getString("doelpunten");

                topscore = new Topscore(naam, wedstrijden, doelpunten);
                if (!bosmtiTopscore.contains(topscore)) {
                    bosmtiTopscore.add(topscore);
                }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bosmtiTopscore;
}

@Override
protected void onPostExecute(ArrayList<Topscore> topscore) {
    listener.onComplete(topscore);
    ListProvider provider = new ListProvider();
    provider.SetTopscore(topscore);
}

}

提前谢谢了!

4

1 回答 1

1

Collections.sort()在您的最高得分者列表中使用并实施您自己Comparable的对它们进行排序。

于 2013-04-13T20:27:29.707 回答