[英]notifydatasetchanged() is not updating the recycler view
i am trying to update recyclerview with images from a API using ASYNC task but its not working properly . 我正在尝试使用ASYNC任务通过API更新图像中的recyclerview,但其无法正常工作。 The problem is with the notifyDataSetChanged()
method in the onPostExecute()
of the ASYNC task. 问题在于ASYNC任务的onPostExecute()
中的notifyDataSetChanged()
方法。 i have checked the json response and the result of the onPostExecute()
the value are correct but the notifydatasetchanged()
is not updating the adapter with the new data. 我已经检查了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();
}
}
}
Movie Adapter: 电影适配器:
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();
}
}
Define a setter for dummyData ArrayList in your adapter like this 像这样在您的适配器中为dummyData ArrayList定义一个setter
public void setDummyData(ArrayList<String> data){
this.dummyData = data;
notifyDataSetChanged();
}
and then onPostExecute() replace 然后onPostExecute()替换
mAdapter.notifyDataSetChanged();
with 与
mAdapter.setDummyData(movieData);
You did't change the data on the adapter class. 您没有更改适配器类上的数据。 Create a setter for the ArrayList
and then call notifyDataSetChanged()
为ArrayList
创建一个setter,然后调用notifyDataSetChanged()
public void setList(ArrayList<String> dummyData){
this.dummyData = dummyData;
notifyDataSetChanged();
}
And on onPostExecute
just call 然后在onPostExecute
上调用
adapter.setList(string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.