我正在尝试使用ASYNC任务更新来自API的图像的recyclerview,但它无法正常工作。问题在于ASYNC任务的notifyDataSetChanged()
中的onPostExecute()
方法。我检查了json响应,onPostExecute()
值的结果是正确的,但notifydatasetchanged()
没有使用新数据更新适配器。
MainActivity:
public class MainActivity extends AppCompatActivity {
public static final String LOG_TAG = MainActivity.class.getName();
public MovieUtils utils = new MovieUtils();
private RecyclerView mRecyclerView;
private MovieAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public ArrayList<String> movieData=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
//Using a grid layout manager to display the movies in a grid view.
mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
//using the jsonParser to parse the String response and create a new data set.
// passing the data to the Adapter.
mAdapter = new MovieAdapter(movieData, getApplicationContext());
mRecyclerView.setAdapter(mAdapter);
DownloadMovieDetail downloadMovieDetail= new DownloadMovieDetail();
downloadMovieDetail.execute();
}
private class DownloadMovieDetail extends AsyncTask<String, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(String... params) {
ArrayList<String> movieDb = new ArrayList<String>();
try {
movieDb = utils.downloadMovie(responseString);
} catch (IOException e) {
e.printStackTrace();
}
return movieDb;
}
@Override
protected void onPostExecute(ArrayList<String> strings) {
movieData.addAll(strings);
Log.v(LOG_TAG,"the movie data"+movieData.toString());
mAdapter.notifyDataSetChanged();
}
}
}
电影适配器:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
public static final String LOG_TAG = MovieAdapter.class.getName();
public ArrayList<String> dummyData;
public Context context;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView MovieImageView;
public ViewHolder(View itemView) {
super(itemView);
MovieImageView=(ImageView) itemView.findViewById(R.id.movie_image);
}
}
//Constructor for the adapter.
public MovieAdapter(ArrayList<String> data, Context c){
this.context=c;
dummyData=data;
}
@Override
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_view, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(MovieAdapter.ViewHolder holder, int position) {
Picasso.with(context).load(dummyData.toString()).into(holder.MovieImageView);
}
@Override
public int getItemCount()
{
return dummyData.size();
}
}
答案 0 :(得分:-1)
在适配器中为dummyData ArrayList定义一个setter,如下所示
public void setDummyData(ArrayList<String> data){
this.dummyData = data;
notifyDataSetChanged();
}
然后onPostExecute()替换
mAdapter.notifyDataSetChanged();
与
mAdapter.setDummyData(movieData);
答案 1 :(得分:-1)
您没有更改适配器类的数据。为ArrayList
创建一个setter,然后调用notifyDataSetChanged()
public void setList(ArrayList<String> dummyData){
this.dummyData = dummyData;
notifyDataSetChanged();
}
在onPostExecute
上只需致电
adapter.setList(string);