gpt4 book ai didi

android自定义 ListView 排序

转载 作者:行者123 更新时间:2023-11-30 02:45:03 28 4
gpt4 key购买 nike

我有一个自定义列表,我试图按字母顺序对 ListView 进行排序,但每次都出错。

我试过比较,但它对我不起作用。

    Context mContext;

private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
mContext = activity;
this.activity = activity;
this.movieItems = movieItems;
}

@Override
public int getCount() {
return movieItems.size();
}

@Override
public Object getItem(int location) {
return movieItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
final Movie m = movieItems.get(position);

// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);


// title
title.setText(m.getTitle());
title.setTextColor(Color.parseColor("#0b6887"));
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
rating.setTextColor(Color.parseColor("#66890e"));

// genre
String genreStr = "";
for (String str : m.getGenre()) {
genreStr += str + ", ";
}
genreStr = genreStr.length() > 0 ? genreStr.substring(0,
genreStr.length() - 2) : genreStr;
genre.setText(genreStr);



// release year
year.setText(String.valueOf(m.getYear()));
year.setTextColor(Color.parseColor("#2c85cb"));

convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext, Show.class);
intent.putExtra("rank",m.getTitle());
intent.putExtra("img",m.getThumbnailUrl());
intent.putStringArrayListExtra("eps", m.getep());
intent.putStringArrayListExtra("typ", m.getGenre());
mContext.startActivity(new Intent(intent));
}
});

return convertView;
}

}

谢谢大家......................................................

最佳答案

有了这个,您可以在 Activity 类中调用new CustomListAdapter后对listView进行排序

Collections.sort(movieItems, new Comparator<User>() {
@Override
public int compare(Movie m1, Movie m2) {
return (int) m1.getTitle().charAt(0) - (int) m2.getTitle().charAt(0);
}
});

关于android自定义 ListView 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25193303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com