自定义适配器
class MoviesAdapter : BaseAdapter<Movies>
{
private List<Movies> movie_list;
private Context context;
public MoviesAdapter(Context context, List<Movies> movie_list)
{
this.movie_list = movie_list;
this.context = context;
}
public override Movies this[int position]
{
get{ return movie_list[position]; }
}
public override int Count
{
get{ return movie_list.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
var row = convertView;
if(row != null)
{
holder = row.Tag as ViewHolder;
}
else
{
row = LayoutInflater.From(this.context).Inflate(Resource.Layout.show_times, null);
holder = new ViewHolder();
holder.title = row.FindViewById<TextView>(Resource.Id.movie_title);
holder.rating = row.FindViewById<TextView>(Resource.Id.movie_rating);
holder.summary = row.FindViewById<TextView>(Resource.Id.movie_description);
holder.poster = row.FindViewById<ImageView>(Resource.Id.poster);
row.Tag = holder;
}
holder.title.Text = movie_list[position].title;
holder.rating.Text = movie_list[position].rating;
holder.summary.Text = movie_list[position].summary;
if (!String.IsNullOrWhiteSpace(movie_list[position].poster))
{
holder.poster.SetImageBitmap(movie_list[position].image);
}
else
{
holder.poster.SetImageResource(Resource.Drawable.Icon);
}
return row;
}
我的活动:
public class ShowTimes : Activity
{
private List<Movies> movie_list = new List<Movies>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Activity2);
string theater_name = Intent.GetStringExtra("theater_name");
TextView theater = FindViewById<TextView>(Resource.Id.theater_name);
ListView movies_view = FindViewById<ListView>(Resource.Id.movies);
theater.Text = theater_name;
var movies = this.get_movies(theater_name);
foreach(var movie in movies)
{
Movies movie_object = movie.ToObject<Movies>();
if(movie_object.poster != null)
{
download_image(movie_object);
}
movie_list.Add(movie_object);
}
MoviesAdapter movie_adpater = new MoviesAdapter(this, movie_list);
movies_view.Adapter = movie_adpater;
}
private JArray get_movies(string theater_name)
{
var encode_arg = WebUtility.UrlEncode($"{theater_name}");
string url = $"http://some_address/api/get_showtimes?theater=" + encode_arg;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var api_response = (HttpWebResponse)request.GetResponse();
string output;
using (StreamReader response_stream = new StreamReader(api_response.GetResponseStream()))
{
output = response_stream.ReadToEnd();
}
JObject movies = JObject.Parse(output);
return (JArray)movies["response"];
}
public async void download_image(Movies movie_object)
{
using (var webclient = new WebClient())
{
var image_bytes = await webclient.DownloadDataTaskAsync(movie_object.poster);
var bit_map = BitmapFactory.DecodeByteArray(image_bytes, 0, image_bytes.Length);
movie_object.image = bit_map;
}
}
}
}
这就是它的样子
但在向下滚动图像时,所有图像都会出现,向上滚动时,上图中的上一张图像会显示出来。
最新情况如何解决?
我很困惑,因为。描述就在那里,一切都只是表现得很有趣的图像。