Android:按最高值排序JSON对象

时间:2013-04-13 20:24:53

标签: android arrays json list sorting

我想按最高值对数组进行排序(从JSON解析)。我想制作一个topscorelist。

我的JSON文件:

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

我必须在'doelpunten'中输入哪些代码来对listview进行排序?或者这不可能吗? 这是我到目前为止的代码:

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

}

非常感谢提前!

1 个答案:

答案 0 :(得分:1)

在您的优秀选手列表中使用Collections.sort()并实施您自己的Comparable进行排序。