ListView in numerical order - Android

0

In my project, I have to leave an option for the user if he wants to view the list in order of mileage or by price. In my case, I'm populating is listed by ArrayAdpter , through a class to inflate ArrayAdapter , where it will be populated 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);
    }
    
asked by anonymous 13.04.2015 / 19:10

1 answer

3

You can sort 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 MarketListArrayAdapter by passing the sorted list and use setAdapter again.

You can also create the sort method within Adapter and call it in your class. After that you will need to call the method notifyDataSetChanged() of your Adapter .

    
13.04.2015 / 19:38