我试图在我的应用程序中显示JSON响应,在我的应用程序中显示图标和标题,我使用了recyclerview在cardview上垂直列出项目。但我的应用程序崩溃,而我向下滚动,我谷歌一些sollution它说使用分页。我从不使用分页。帮助我解决问题
class ListSourceViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
{
ItemClickListener itemClickListener;
TextView source_title;
CircleImageView source_image;
public ListSourceViewHolder(View itemView) {
super(itemView);
source_image=(CircleImageView)itemView.findViewById(R.id.source_image);
source_title=(TextView) itemView.findViewById(R.id.source_name);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
public class ListSourceAdapter
extendsRecyclerView.Adapter<ListSourceViewHolder> {
private Context context;
private WebSite webSite;
private IconBetterIdeaService mService;
public ListSourceAdapter(Context context, WebSite webSite) {
this.context = context;
this.webSite = webSite;
mService= Common.getIconService();
}
@Override
public ListSourceViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view=inflater.inflate(R.layout.source_layout,parent,false);
return new ListSourceViewHolder(view);
}
@Override
public void onBindViewHolder(final ListSourceViewHolder holder, final int
position) {
StringBuilder iconBetterAPI=new StringBuilder("https://icons.better-
idea.org/allicons.json?url=");
iconBetterAPI.append(webSite.getSources().get(position).getUrl());
mService.getIconUrl(iconBetterAPI.toString()).enqueue(new
Callback<IconBetterIdea>() {
@Override
public void onResponse(Call<IconBetterIdea> call,
Response<IconBetterIdea> response) {
if (response.body().getIcons().size() > 0)
{
Picasso.with(context).load(response.body().getIcons().get(0).getUrl()).into(holder.source_image);
}
}
@Override
public void onFailure(Call<IconBetterIdea> call, Throwable t) {
}
});
holder.source_title.setText(webSite.getSources().get(position).getName());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent intent=new Intent(context, ListNews.class);
intent.putExtra("source",
webSite.getSources().get(position).getId());
intent.putExtra("sortBy",
webSite.getSources().get(position).getSortBysAvailable().get(0));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return webSite.getSources().size();
}
}
//Here is my mainActivity
package pay4free.in.primenews;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.gson.Gson;
import dmax.dialog.SpotsDialog;
import io.paperdb.Paper;
import pay4free.in.primenews.Adapter.ListSourceAdapter;
import pay4free.in.primenews.Common.Common;
import pay4free.in.primenews.Interface.NewsService;
import pay4free.in.primenews.Model.WebSite;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
RecyclerView listWebsite;
RecyclerView.LayoutManager layoutManager;
NewsService mService;
ListSourceAdapter adapter;
SpotsDialog Dialog;
SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paper.init(this);
mService= Common.getNewsService();
swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipe);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadWebsiteSource(true);
}
});
listWebsite=(RecyclerView)findViewById(R.id.list_source);
listWebsite.setHasFixedSize(false);
layoutManager=new LinearLayoutManager(this);
listWebsite.setLayoutManager(layoutManager);
Dialog=new SpotsDialog(this);
loadWebsiteSource(false);
}
private void loadWebsiteSource(boolean isRefreshed) {
if(!isRefreshed)
{
String cache =Paper.book().read("cache");
if(cache!=null&&!cache.isEmpty())
{
WebSite webSite=new Gson().fromJson(cache,WebSite.class);
adapter=new ListSourceAdapter(getBaseContext(),webSite);
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
}
else
{
Dialog.show();
mService.getSources().enqueue(new Callback<WebSite>() {
@Override
public void onResponse(Call<WebSite> call, Response<WebSite> response) {
adapter=new ListSourceAdapter(getBaseContext(),response.body());
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
Paper.book().write("cache",new Gson().toJson(response.body()));
}
@Override
public void onFailure(Call<WebSite> call, Throwable t) {
}
});
}
}
else
{
Dialog.show();
mService.getSources().enqueue(new Callback<WebSite>() {
@Override
public void onResponse(Call<WebSite> call, Response<WebSite> response) {
adapter=new ListSourceAdapter(getBaseContext(),response.body());
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
Paper.book().write("cache",new Gson().toJson(response.body()));
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call<WebSite> call, Throwable t) {
}
});
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
帮助我,当我向下滚动到底部时,我的应用程序崩溃了。
答案 0 :(得分:0)
每次都在创建适配器。只创建一次适配器并更新它。 删除这些行
adapter=new ListSourceAdapter(getBaseContext(),response.body());
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
创建一个方法来更新适配器中的数据。
adapter.yourMethodToUpdate(response.body());
adapter.notifyDataSetChanged();