Listview in numeric order - Android

Asked

Viewed 535 times

0

In my project, I have to leave an option for the user if he wants to view the list by mileage order or by price. In my case, I’m filling out this list for a ArrayAdpter, through a class to inflate the ArrayAdapter, where it will be filled with API data.

public class MarketListArrayAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] Values;
    private MarketList[] list;

    public MarketListArrayAdapter(Context context, String[] values, MarketList[] list) {
        super(context, R.layout.search_list, values);
        this.context = context;
        Values = values;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.search_list, parent, false);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView txtProduto = (TextView) rowView.findViewById(R.id.produto);
        TextView txtMercado = (TextView) rowView.findViewById(R.id.mercado);
        TextView txtQuilometros = (TextView) rowView.findViewById(R.id.quilometros);
        TextView txtValor = (TextView) rowView.findViewById(R.id.valor);

        Bitmap instIcon = loadImgCustomerFromFile(list[position].getImage());

        if (instIcon != null) {
            Bitmap roundedImage = getRoundedCornerBitmap(instIcon);
            imageView.setImageBitmap(roundedImage);
        }
        txtProduto.setText(list[position].getProduto());
        txtMercado.setText(list[position].getMercado());
        txtQuilometros.setText(list[position].getKm());
        txtValor.setText("R$ "+list[position].getValor());

        return rowView;
    }

public class MarketList {
    String Image;
    String Produto;
    String Mercado;
    String Km;
    float Valor;

    public MarketList(String image, String produto, String mercado, String km, int valor) {
        Image = image;
        Produto = produto;
        Mercado = mercado;
        Km = km;
        Valor = valor;
    }
    public String getImage() {
        return Image;
    }
    public void setImage(String Image) {
        this.Image = Image;
    }
    public String getProduto() {
        return Produto;
    }
    public void setProduto(String Produto) {
        this.Produto = Produto;
    }
    public String getMercado() {
        return Mercado;
    }
    public void setMercado(String Mercado) {
        this.Mercado = Mercado;
    }
    public String getKm() {
        return Km;
    }
    public void setKm(String Km) {
        this.Km = Km;
    }
    public float getValor() {
        return Valor;
    }
    public void setValor(float Valor) {
        this.Valor = Valor;
    }
}

    private void loadList() {  
        list = (ListView) findViewById(R.id.listMarkets);
        String[] valuesMercados = new String[]{
               new String("Texto 1"),
               new String ("Texto 2"),
               new String ("Texto 3")
        };
        MarketList[] marketLists = new MarketList[]{
                new MarketList("_1.png", "Produto 1", "Mercado 1", "4.3 km", 3),
                new MarketList("_2.jpg", "Produto 2", "Mercado 2", "5 km", 4),
                new MarketList("_3.jpg", "Produto 2", "Mercado 2", "5 km", 4)
        };
        MarketListArrayAdapter listAdapter = new MarketListArrayAdapter(this, valuesMercados, marketLists);

        list.setAdapter(listAdapter);
    }
  • The path is Rafael’s solution, but in the case of mileage, it will be necessary to change the type of property to double (or something similar), since it will not be possible to sort numerically String.

1 answer

3


You can order your MarketList using:

Arrays.sort(marketLists, new Comparator<MarketList>() {
    @Override
    public int compare(MarketList market1, MarketList market2) {
        return market1.getKm().compareTo(market2.getKm());
    }
});

Then create your own MarketListArrayAdapter passing the sorted list and use the setAdapter again.

You can also create the sorting method within the Adapter and call it in its class. After that it will be necessary to call the method notifyDataSetChanged() of your Adapter.

  • It worked the way you were wanting @Rafael, but only one question, as you would double?

  • You can use: Return Float.valueOf(market1.getValor()). compareTo(market2.getValor());

  • worked perfect, that’s what I was looking for !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.