我创建了一个自定义列表视图,用于从数据库加载数据。然后将这些数据放入数组中。
我可以将所有数据显示在列表视图中应该显示的位置。那里没有问题。 我遇到的问题是滚动。当我查看列表视图时,它似乎运行缓慢。
以下是我的代码摘录:
static class ViewHolder {
public TextView txtTitle;
public TextView txtDescription;
public TextView txtLocations;
public TextView txtShowingDate;
public TextView txtAdded;
public TextView txtProvider;
public TextView txtTicketUrl;
public ImageView imgProviderImage;
}
public class FilmItemAdapter extends ArrayAdapter<FilmRecord> {
public ArrayList<FilmRecord> films;
public FilmItemAdapter(Context context, int textViewResourceId, ArrayList<FilmRecord> films) {
super(context, textViewResourceId, films);
this.films = films;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
/* There is no view at this position, we create a new one.
In this case by inflating an xml layout */
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listitem, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
//Find find by ID once, and store details in holder.
viewHolder.txtTitle = (TextView)convertView.findViewById(R.id.filmtitle);
viewHolder.txtShowingDate = (TextView) convertView.findViewById(R.id.filmshowingtime);
viewHolder.txtAdded = (TextView) convertView.findViewById(R.id.filmadded);
viewHolder.txtLocations = (TextView) convertView.findViewById(R.id.filmlocations);
viewHolder.txtDescription = (TextView) convertView.findViewById(R.id.filmdescription);
viewHolder.txtProvider = (TextView) convertView.findViewById(R.id.filmprovider);
viewHolder.txtTicketUrl = (TextView) convertView.findViewById(R.id.filmticketurl);
viewHolder.imgProviderImage = (ImageView) convertView.findViewById(R.id.providerImage);
convertView.setTag(viewHolder);
} else {
/* We recycle a View that already exists */
viewHolder = (ViewHolder) convertView.getTag();
}
FilmRecord film = films.get(position);
//Get film details from ARRAY and not the database - getting records from a db is time consuming.
viewHolder.txtTitle.setText(films.get(position).filmTitle);
viewHolder.txtDescription.setText(films.get(position).filmDescription);
viewHolder.txtLocations.setText(films.get(position).filmLocations);
viewHolder.txtShowingDate.setText("Showing time: " + films.get(position).filmShowingDate);
viewHolder.txtAdded.setText("Added on: " + films.get(position).filmAdded);
viewHolder.txtProvider.setText(films.get(position).filmProvider);
viewHolder.txtTicketUrl.setText(films.get(position).filmTicketUrl);
return convertView;
}
}
对此的帮助会很棒:)