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 numericallyString
.– Paulo Rodrigues