How to arrange items in list descending instead of the image:
to be like this:
JavaScript
x
10
20
50
100
and this is the below adapter I use:
JavaScript
package com.appgain.BeetElKhairCharity.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.appgain.BeetElKhairCharity.R;
import java.util.ArrayList;
import java.util.List;
public abstract class GenericArrayAdapter<T> extends ArrayAdapter<T> {
// Vars
private LayoutInflater mInflater;
public GenericArrayAdapter(Context context, List<T> objects) {
super(context, 0, objects);
init(context);
}
// Headers
public abstract void drawText(TextView textView, T object);
public abstract void drawSubText(TextView textView, T object);
private void init(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.simple_spinner_item, parent, false);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
drawText(vh.textView, getItem(position));
return convertView;
}
static class ViewHolder {
TextView textView;
private ViewHolder(View rootView) {
textView = (TextView) rootView.findViewById(R.id.row_text);
}
}
static class ViewHolderSub {
TextView textView;
private ViewHolderSub(View rootView) {
textView = (TextView) rootView.findViewById(R.id.sub_text);
}
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final ViewHolderSub vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.simple_spinner_item_child, parent, false);
vh = new ViewHolderSub(convertView);
convertView.setTag(vh);
} else {
vh = (ViewHolderSub) convertView.getTag();
}
drawSubText(vh.textView, getItem(position));
return convertView;
}
}
and this is the setupPrice()
method which I need to arrange the entities in:
JavaScript
private void setupPrice() {
final GenericArrayAdapter priceAdapter = new GenericArrayAdapter<Sm>(getContext(), slectedSmsEntities) {
@Override
public void drawText(TextView textView, Sm object) {
if (object != null && object.getPrice() != null) {
if (LngID == 0)
textView.setText(object.getPrice() + "");
else {
textView.setText(object.getPrice() + "");
}
}
}
@Override
public void drawSubText(TextView textView, Sm object) {
if (object != null && object.getPrice() != null) {
if (LngID == 0)
textView.setText(object.getPrice() + "");
else {
textView.setText(object.getPrice() + "");
}
}
}
};
priceAdapter.setDropDownViewResource(R.layout.simple_spinner_item);
amountspiner.setAdapter(priceAdapter);
amountspiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
slectedSmsEntity = (Sm) priceAdapter.getItem(position);
Log.e("slectedSmsEntities", slectedSmsEntity.toString());
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Advertisement
Answer
You can implement a Comparator like this for your Sm
class before passing it into the adapter. Just like what the first comment said.
JavaScript
Collections.sort(slectedSmsEntities, new Comparator<Sm>(){
public int compare(Sm s1, Sm s2) {
return s2.getPrice() > s1.getPrice() ? -1 : 1;
}
});