I have a baseAdapter class that adds data to a Gridview.
Please is there a way to sort the list before it is added to the Adapter?
Comm_AppsList=new ArrayList<Appis_Infos>();
Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.skype), "Skype","com.skype.raider"));
Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.yahoomessenger),"Messenger","com.yahoo.mobile.client.android.im"));
Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.whatsappm),"WhatsApp","com.whatsapp"));
Comm_AppsList.add(new Appis_Infos(BitmapFactory.decodeResource(com_res, R.drawable.yahoomail),"Mail","com.yahoo.mobile.client.android.mail"));
Com_gridview.setAdapter(new CommAppis_Adapter(this, Comm_AppsList));
And here is the BaseAdapter
class CommAppis_Adapter extends BaseAdapter{
private Context AIcontext;
private List<Appis_Infos>AdList;
CommAppis_Adapter(Context Aicontext, List<Appis_Infos> aDlist){
AIcontext=Aicontext;
AdList=aDlist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return AdList.size();
}
@Override
public Object getItem(int it_position) {
// TODO Auto-generated method stub
return AdList.get(it_position);
}
@Override
public long getItemId(int id_position) {
// TODO Auto-generated method stub
return id_position;
}
@Override
public View getView(int Vposition, View Aview, ViewGroup Vparent) {
// TODO Auto-generated method stub
Appis_Infos appsData=AdList.get(Vposition);
Comm_Viewholder holder=null;
if(Aview==null){
LayoutInflater apps_inflater=LayoutInflater.from(AIcontext);
Aview=apps_inflater.inflate(R.layout.comm_appis, null);
holder=new Comm_Viewholder();
holder.apps_icons=(ImageView)Aview.findViewById(R.id.comm_imageview);
holder.apps_name=(TextView)Aview.findViewById(R.id.comm_tvName);
Aview.setTag(holder);
}else{
holder=(Comm_Viewholder)Aview.getTag();
}
holder.apps_icons.setImageBitmap(appsData.getIcon());
holder.apps_name.setText(appsData.getName());
return Aview;
}
}
Please how do I sort (Programmatically because my List is very long.I just croped it) these items before I add them.
First of all you can define your own Comparator object:
and then call the Collections.sort method:
If you need to compare in case invariant mode use the compareToIgnoreCase method instead of compareTo.