我有ListView
。每個Item
得到3 TextViews
。 Items
如何按照字母順序排列的TextView
?如何在ListView中對項目進行排序
我Adapter
代碼如下:
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] textName;
private final String[] imagePath;
private final String[] textAge;
private final String[] textSex;
public CustomList(Activity context,
String[] imagePath, String[] textName, String[] textSex, String[] textAge) {
super(context, R.layout.listview_row, textName);
this.context = context;
this.textName = textName;
this.imagePath = imagePath;
this.textAge = textAge;
this.textSex = textSex;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listview_row_mix, null, false);
TextView txtTitle = (TextView) rowView.findViewById(R.id.NameText);
TextView txtSex = (TextView) rowView.findViewById(R.id.SexText);
TextView txtAge = (TextView) rowView.findViewById(R.id.AgeText);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
txtTitle.setText(textName[position]);
txtSex.setText(textSex[position]);
txtAge.setText(textAge[position]);
String root = Environment.getExternalStorageDirectory().toString();
File imageFile = new File(root + "/WorkImages/" + imagePath[position]);
Log.i("Debug", imageFile.toString());
if(imageFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
Log.i("Debug", imageFile.toString());
}else{
imageView.setImageResource(R.drawable.profile_icon);}
return rowView;
}
}
我想整理應在adapter
代碼進行。我看了類似的問題,但他們與TextView
Items
合作,這意味着他們可以簡單地比較和排序內String[]
內的值。如果我在這裏做類似的事情,我只會搞砸名單,因爲名字和信息不會對應。
Arrays.sort(string_arr); – Ajinkya