我使用async task
获取数据,其中包含ImageUrl,title,rating,e.t.c等电影的详细信息。我为movie
制作了一个电影课,我正在ImageUrl
中加载gridView
的图片。单击图像将导航到movie_detail_activity
。
现在,当我最初运行应用程序时,单击图像导航到具有正确详细信息的活动,但是当我向下滚动然后向上滚动时,imageViews会自动更改,但详细信息活动保持原来的状态。
import...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://api.themoviedb.org/3/movie/top_rated?api_key=MY_KEY";
new FetchPopularMoviesData().execute(url);
}
public void launch_activity(View view, MovieClass movie){
Intent intent= new Intent(this,MovieDetails.class);
Gson gson = new Gson();
String movieJson = gson.toJson(movie);
intent.putExtra("MovieJSON",movieJson);
startActivity(intent);
}
public class FetchPopularMoviesData extends AsyncTask<String, Void, ArrayList<MovieClass>> {
private Exception exception;
protected void onPreExecute() {
}
protected ArrayList<MovieClass> doInBackground(String... urls) {
ArrayList<MovieClass> movies = new ArrayList<>();
//code for fetching data from api
return movies;
}
protected void onPostExecute(final ArrayList<MovieClass> movies) {
if (movies == null) {
Log.e("After Parse Error", "No data found ,THERE WAS AN ERROR");
Toast.makeText(getApplicationContext(), "No working internet connection", Toast.LENGTH_LONG).show();
}
else {
for (int i = 0; i < movies.size(); i++) {
String title_name = movies.get(i).getTitle();
Log.i("INFO", title_name);
}
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MovieAdapter(MainActivity.this, movies));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
launch_activity(v, movies.get(position));
}
});
}
}
}
以下是适配器的代码
public class MovieAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<MovieClass> movies;
public MovieAdapter(Context c,ArrayList<MovieClass> movies) {
mContext = c;
this.movies = movies;
}
public int getCount() {
return movies.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
Picasso.with(mContext)
.load(movies.get(position).getPoster())
.into(imageView);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
如何让ImageView
与movie_details_activity
内容保持同步?
答案 0 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Picasso.with(mContext)
.load(movies.get(position).getPoster())
.into(imageView);
return imageView;
}
检查这是否可行。
答案 1 :(得分:0)
使用ViewHolder。
这肯定会解决你的问题。希望它有所帮助。
private MovieClass mItem;//declare globally
private ViewHolder holder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mItem = getItem(position);
holder = null;
if (convertView == null) {
convertView = inflater
.inflate(R.layout.row_fav_mem_list_item, null); //add imageview in this layout..
holder = new ViewHolder(); //declare this globally
holder.userPic = (ImageView) convertView
.findViewById(R.id.img_user_pic);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
String imgURL = mItem.getPoster();
if (!TextUtils.isEmpty(imgURL)) {
imgLoader.load(imgURL).error(R.drawable.user_profile_image) //use default image if there is no image.
.placeholder(R.drawable.user_profile_image)
.into(holder.userPic);
} else
holder.userPic.setImageResource(R.drawable.user_profile_image);
return convertView;
}
public static class ViewHolder {
ImageView userPic;
}
@Override
public MovieClass getItem(int position) {
return movies.get(position);
}