Order items in a Multi Values Listview in Android
Order items in a Multi Values Listview in Android
I'm looking to some way for sort my items inside my listview. Which is a multi values Listview with 3 items for each row. I use a Custom ListViewAdapter to achieve this, but they are sorted in order of introduction inside the ArraysList. Something like:
1-2-3-4-5-6....
I want to inverse this order, having in top of my Listview the last item introduced:
6-5-4-3-2-1....
Here is my Custom ListViewAdapter:
public class ListViewAdapter extends BaseAdapter
static class ViewHolder
TextView number;
TextView elapsedTime;
TextView totalTime;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public ArrayList<HashMap<String, String>> list;
Activity activity;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list)
super();
this.activity=activity;
this.list=list;
@Override
public int getCount()
// TODO Auto-generated method stub
return list.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return list.get(position);
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
final ViewHolder viewHolder;
if(convertView == null)
convertView = inflater.inflate(R.layout.timer_row, null);
viewHolder = new ViewHolder();
viewHolder.number = (TextView) convertView.findViewById(R.id.number);
viewHolder.elapsedTime = (TextView) convertView.findViewById(R.id.elapsed_time);
viewHolder.totalTime =(TextView) convertView.findViewById(R.id.total_time);
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
HashMap<String, String> map=list.get(position);
viewHolder.number.setText(map.get(FIRST_COLUMN));
viewHolder.elapsedTime.setText(map.get(SECOND_COLUMN));
viewHolder.totalTime.setText(map.get(THIRD_COLUMN));
return convertView;
And here is how I call it in my activity:
//ListView Adapter
mListviewLayout = (RelativeLayout)v.findViewById(R.id.listview_times_layout);
mTimesListview = (ListView)v.findViewById(R.id.times_listview);
mTimesArrayList = new ArrayList<HashMap<String,String>>();
mListViewAdapter= new ListViewAdapter(mActivity, mTimesArrayList);
mTimesListview.setAdapter(mListViewAdapter);
Finally, here is how I introduce items inside the Hashmap:
btnNextCicle.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (isTimerPaused())
onTimerStop();
else
mListviewLayout.setVisibility(View.VISIBLE);
mCiclesCounter++;
HashMap<String, String> newEntry = new HashMap<String, String>();
newEntry.put(ListViewAdapter.FIRST_COLUMN, "#" + mCiclesCounter);
newEntry.put(ListViewAdapter.SECOND_COLUMN, Util.getTimeForTimer(mElapsedCicleTime));
newEntry.put(ListViewAdapter.THIRD_COLUMN, Util.getTimeForTimer(mTimeInMilliseconds));
mCicleStartTime = SystemClock.elapsedRealtime();
mTimesArrayList.add(newEntry);
);
1 Answer
1
You can subclass HashMap<String, Sting>
and make it implements Comparable
. Then you can use Arrays.sort(yourComparable)
to sort the array the way you want.
HashMap<String, Sting>
Comparable
Arrays.sort(yourComparable)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It worked! Thanks mate
– Francisco Durdin Garcia
May 23 '16 at 15:23