How to organize data from a listview?

0

I have a list view of schedules where it says the name of the line, the stop, the time and state.

If someone can explain to me how the android organization works thanks, I just need to know how to organize it in ascending and ascending order, for example the line name field.

The goal is for example what I saw in the English stackoverflow:

Unsorted:

Record  Color   Clothes
0       blue    shoes
1       yellow  pants
2       red     boots
3       black   coat

Sorted by Color:

Record  Color   Clothes
3       black   coat
0       blue    shoes
2       red     boots
1       yellow  pants

Sorted by Clothes:

  Record  Color   Clothes
    2       red     boots
    3       black   coat
    1       yellow  pants
    0       blue    shoes

So for a listview with the "ArrayList" only with the two ascending and descending order. The English post:

link

My code from my list:

 try {
            jsonArray = new JSONArray(myJSON);


            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject cursor = jsonArray.getJSONObject(i);

                String title = cursor.getString(TAG_TITLE);

                horarioHora.add(cursor.getString(TAG_TITLE));
                String estado = cursor.getString(TAG_DATESTART);
                horarioLinha.add(cursor.getString("linhaNome"));
                horarioParagem.add(cursor.getString("paragemRua"));
                HashMap<String, String> horario = new HashMap<>();
                horario.put(TAG_TITLE, title);
                horario.put(TAG_DATESTART, estado);

                horarioList.add(horario);
            }

            ListAdapter adapter = new SimpleAdapter(this, horarioList, R.layout.list_item,
                    new String[]{TAG_TITLE, TAG_DATESTART},
                    new int[]{R.id.title, R.id.estado});
            listaHorarios.setAdapter(adapter);
            dialog.dismiss();
            Url = "http://dagobah.grifin.pt/tiagocoelho/utilizadorAndroid.php";
            funcao = "utilizador";

            get_data();
            listaHorarios.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            HorariosActivity.this);
                    alertDialogBuilder
                            .setMessage("Quer adicionar o horário aos favoritos?")
                            .setCancelable(false)
                            .setPositiveButton("Adicionar",


                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                            int id) {
                                            try {
                                                horarioitem = jsonArray.getJSONObject(position).toString();
                                                Sthoras = horarioHora.get(position);
                                                Stlinhas = horarioLinha.get(position);
                                                Stparagens = horarioParagem.get(position);
                                                tvlinhas.setText(Sthoras);

                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }

                                            Url = "http://dagobah.grifin.pt/tiagocoelho/confirmaAddFavAndroid.php?Sthoras="+Sthoras+"&Stlinhas="+Stlinhas+"&Stparagens="+Stparagens+"&utilizadorId="+UtilizadorId2;
                                            Url=Url.replace(" ", "%20");
                                            funcao = "verificar";
                                            get_data();


                                        }
                                    });
                    alertDialogBuilder.setNegativeButton("Cancelar",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = alertDialogBuilder.create();
                    alert.show();

                }
            }); //end setOnItemClickListener
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

I hope you can help me because really from what I already researched I can not understand the android organization.

For my program the organization can not be done via php or sql needs to be done by Android.

    
asked by anonymous 05.07.2016 / 12:28

1 answer

0

The ListView organizes the items in the order that you place them in it, if your data is cluttered, it is most probably because of the JSON Object, as the documentation

  

Json Object is an unordered set of name / value pairs.

In this way, libraries are free to reorder it however they want, and in the case of Android, it does (do not ask me why).

I already had problems with this sort order, and the way I found it to correct was to organize the list before entering it in the list, picking up the Json Key and inserting it in the "0" position, then in position "1" and so on on.

Some libraries like GSON maintain order, it's a much easier solution than treating Key to Key (depending on the number of keys)

    
05.07.2016 / 15:51