我有问题。当我最初使用newsPojo
时 - 所有显示都正常(但刷卡刷新不起作用,当我使用postAdapter.notifyDataSetChanged()
时 - 我的项目不会显示。我该如何解决?
这是我的Activity类
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NewsApi newsApi;
private NewsPojo fromRetrofit;
private MainAdapter postAdapter;
@BindView(R.id.activity_main_swipe_to_refresh) SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fromRetrofit = new NewsPojo();
fromRetrofit.setArticles(new ArrayList<Article>());
response();
initRecycleView();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
response();
swipeRefreshLayout.setRefreshing(false);
}
});
}
private void response() {
newsApi = RitApplication.getApi();
newsApi.getData().enqueue(new Callback<NewsPojo>() {
@Override
public void onResponse(Call<NewsPojo> call, Response<NewsPojo> response) {
fromRetrofit.getArticles().clear();
fromRetrofit = response.body();
postAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<NewsPojo> call, Throwable t) {
}
});
}
public void initRecycleView() {
recyclerView = findViewById(R.id.activity_main_recycle_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
postAdapter = new MainAdapter(fromRetrofit, new OnIteamClickListener() {
@Override
public void onItemClick(View itemView) {
}
}, getApplicationContext());
recyclerView.setAdapter(postAdapter);
}
}
在这里你可以看到我的Adapter类
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
private NewsPojo newsPojo;
private OnIteamClickListener listener;
private Context mContext;
public MainAdapter(NewsPojo newsPojo, OnIteamClickListener listener, Context mContext) {
this.newsPojo = newsPojo;
this.listener = listener;
this.mContext = mContext;
}
public void clear(){
newsPojo.getArticles().clear();
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_news, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(newsPojo.getArticles().get(position));
}
@Override
public int getItemCount() {
if (newsPojo != null) {
return newsPojo.getArticles().size();
} else {
return 0;
}
}
为什么postAdapter.notifyDataSetChanged();
无效?
答案 0 :(得分:0)
我建议如下:
更改以下行:
fromRetrofit.getArticles().clear();
fromRetrofit = response.body();
postAdapter.notifyDataSetChanged();
为此:
postAdapter.cleanAddArticles(response.body());
并在适配器中添加以下内容:
public void cleanAddArticles(NewsPojo newsPj){
this.newsPojo.getArticles().clear();
this.newsPojo.getArticles().addAll(newsPj.getArticles());
notifyDataSetChanged();
}
注意:根据具体情况,错误必须通过对象引用。
答案 1 :(得分:0)
当您通过调用response()
方法获取新值时,您必须调用initRecycleView()
将新值设置为适配器,然后使用notifyDataSetChanged()
@Override
public void onResponse(Call<NewsPojo> call, Response<NewsPojo> response) {
fromRetrofit.getArticles().clear();
fromRetrofit = response.body();
initRecycleView() //*** Add this.
postAdapter.notifyDataSetChanged();
}
}
答案 2 :(得分:0)
首先检查当你刷新刷新那个时候你的postAdapter没有得到null其他明智的你的代码是好的。 如果你想为下面的适配器制作一个单独的方法..
private void setAdater(){
if (postAdapter==null) {
postAdapter = new MainAdapter(fromRetrofit, new OnIteamClickListener() {
@Override
public void onItemClick(View itemView) {
}
}, getApplicationContext());
recyclerView.setAdapter(postAdapter);
}
else{
postAdapter.notifyDataSetChanged();
}
}
并检查fromRetrofit对象的值。我认为适配器没有任何价值因为只有明确的文章,但对象有一些价值,我认为你在获取数据时使对象为空...
fromRetrofit=null;
fromRetrofit = response.body();