0

sort ListView Android by float number ...

I have a listview price type float display of a mysql database ,,

I want to sort it (by order) but I have not found a solution ..

how can I do it please !!!

it's the code of my listView with prices done !!

    private class GetHttpResponse extends AsyncTask<Void, Void, Void>{ 
    public Context context;
    String ResultHolder;
    List<subjects> subjectsList;
    public GetHttpResponse(Context context)
    {
        this.context = context;
    }
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg0)
    {
        HttpServicesClass httpServiceObject = new 
     HttpServicesClass(ServerURL);
        try
        {
            httpServiceObject.ExecutePostRequest();
         if(httpServiceObject.getResponseCode() == 200)
            {
                ResultHolder = httpServiceObject.getResponse();
         if(ResultHolder != null)
                {
                    JSONArray jsonArray = null;
                   try {
       jsonArray = new JSONArray(ResultHolder);
       JSONObject jsonObject;
       subjects subjects;
       subjectsList = new ArrayList<subjects>();
       for(int i=0; i<jsonArray.length(); i++)
       { subjects = new subjects();
       jsonObject = jsonArray.getJSONObject(i);
      subjects.SubjectName = jsonObject.getString("tarif");
      subjectsList.add(subjects);
                        }  }
        catch (JSONException e) {
         e.printStackTrace();
                    } } } else
            {
                Toast.makeText(context, httpServiceObject.getErrorMessage(), 
           Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result)
   { progressBarSubject.setVisibility(View.GONE);
       SubjectListView.setVisibility(View.VISIBLE);
       if(subjectsList != null)
        {
       ListAdapterClass adapter = new ListAdapterClass(subjectsList, 
        context);
       SubjectListView.setAdapter(adapter);
            SubjectListView.setOnItemClickListener(new 
        AdapterView.OnItemClickListener(){
        @Override
         public void onItemClick(AdapterView<?> parent, View view, 
        int pos, long id) {
                    String selectedItem = 
         parent.getItemAtPosition(pos).toString();
        Toast.makeText(TechnicienActivity.this,selectedItem,
      Toast.LENGTH_SHORT).show();
                    Intent intent = new 
      Intent(getApplicationContext(),Main2Activity.class);
      intent.putExtra("Position" , String.valueOf(id));
                    startActivity(intent);
                }   }); }  } }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • refer this link hope it's help for you https://stackoverflow.com/questions/9109890/android-java-how-to-sort-a-list-of-objects-by-a-certain-value-within-the-object/9109990?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Ali May 25 '18 at 10:59

1 Answers1

0

You can sort the list by using

Collections.sort(list);

and then set this to the listview. Example;

subjectsList.add(subjects);
Collections.sort(subjectsList);
theanilpaudel
  • 3,348
  • 8
  • 39
  • 67
  • like this ? private void sortData(boolean asc) { //SORT ARRAY ASCENDING AND DESCENDING if (asc) Collections.sort(subjects); else { Collections.reverse(subjects); } SubjectListView.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,subjects)); } – Med Zidi May 28 '18 at 12:15