我有一个baseAdapter类,可以将数据添加到Gridview。 请问有没有办法在列表添加到适配器之前对其进行排序?
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));
这是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;
}
}
在添加之前,请如何排序(以编程方式,因为我的列表很长。我只是把它弄坏了)这些项目。
答案 0 :(得分:12)
首先,您可以定义自己的Comparator对象:
Comparator<Appis_Infos> myComparator = new Comparator<Appis_Infos>() {
public int compare(Appis_Infos obj1,Appis_Infos obj2) {
return obj1.getName().compareTo(obj2.getName());
}
};
然后调用Collections.sort方法:
Collections.sort(Comm_AppsList, myComparator);
如果您需要在不变模式下进行比较,请使用 compareToIgnoreCase 方法而不是 compareTo 。