我想从服务器获取一些数据并将其显示在Recyclerview
中。我可以显示这些数据,但有时候不显示这些数据,只显示progressBar
我会在Log.e
中显示此数据并快速显示 ,但不会在RecyclerView
中显示此数据,只显示进度。
请参阅下图了解我的意思:
Click too see
我的回复代码:
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CountryResponse> call = api.getCountryList();
call.enqueue(new Callback<CountryResponse>() {
@Override
public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
try {
if (response.body() != null) {
models.clear();
models.addAll(response.body().getData());
for (int i = 0; i <= models.size(); i++) {
Log.e("CountryInfoTAG", response.body().getData().get(i).getId() + " : " +
response.body().getData().get(i).getName());
}
countryProgress.setVisibility(View.GONE);
mAdapter = new CountryAdapter(models, context, listener);
countryRecycler.setAdapter(mAdapter);
}
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<CountryResponse> call, Throwable t) {
Toasty.error(context, context.getResources().getString(R.string.failRequest),
Toast.LENGTH_LONG, true).show();
}
});
适配器代码:
public class CountryAdapter extends RecyclerView.Adapter {
private List<CountryDatum> mData;
private Context context;
private sendDataListener listner;
public CountryAdapter(List<CountryDatum> mData, Context context, sendDataListener listner) {
this.mData = mData;
this.context = context;
this.listner = listner;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);
vh = new DataViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof DataViewHolder) {
((DataViewHolder) holder).countryListTxt.setText(mData.get(position).getName() + "");
((DataViewHolder) holder).countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listner.onSendIdName(mData.get(position).getId(), mData.get(position).getName());
if (context instanceof RegisterActivity) {
((RegisterActivity) context).dismissCountryDialog();
}
}
});
}
}
@Override
public int getItemCount() {
return mData.size();
}
public void add(List<CountryDatum> models) {
mData.addAll(models);
notifyDataSetChanged();
}
public void clear() {
mData.clear();
notifyDataSetChanged();
}
public class DataViewHolder extends RecyclerView.ViewHolder {
private TextView countryListTxt;
public DataViewHolder(View itemView) {
super(itemView);
countryListTxt = (TextView) itemView.findViewById(R.id.countryNameTxt);
}
}
}
我该如何解决?请帮帮我
答案 0 :(得分:1)
尝试用此替换您的适配器:
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> {
private List<CountryDatum> mData;
private Context context;
private sendDataListener listner;
public CountryAdapter(List<CountryDatum> mData, Context context, sendDataListener listner) {
this.mData = mData;
this.context = context;
this.listner = listner;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
holder.countryListTxt.setText(mData.get(position).getName() + "");
holder.countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listner.onSendIdName(mData.get(position).getId(), mData.get(position).getName());
if (context instanceof RegisterActivity) {
((RegisterActivity) context).dismissCountryDialog();
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void add(List<CountryDatum> models) {
mData.addAll(models);
notifyDataSetChanged();
}
public void clear() {
mData.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView countryListTxt;
public DataViewHolder(View itemView) {
super(itemView);
countryListTxt = (TextView) itemView.findViewById(R.id.countryNameTxt);
}
}
}
答案 1 :(得分:0)
请更新适配器类中的onBindView方法,不要像这样进行转换
((DataViewHolder) holder)
试试这个
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
holder.countryListTxt.setText(mData.get(position).getName() + "");
holder.countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listner.onSendIdName(mData.get(position).getId(),
mData.get(position).getName());
if (context instanceof RegisterActivity) {
((RegisterActivity) context).dismissCountryDialog();
}
}
});
}
并在活动类中的Wep API回调的成功方法中设置适配器之后打印模态
答案 2 :(得分:0)
您从vh
返回的对象onCreateViewHolder
被定义为RecyclerView.ViewHolder
。
RecyclerView.ViewHolder vh;
View v = // ...
vh = new DataViewHolder(v);
return vh;
因此onBindViewHolder
中的以下语句可能返回false(值得用调试器检查)。
if (holder instanceof DataViewHolder)
为了解决这个问题,请尝试像这样定义您的类:
public class CountryAdapter extends RecyclerView.Adapter<DataViewHolder> {
// ...
}
从这里你还需要改变几种方法:
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);
return new DataViewHolder(v);
}
@Override
public void onBindViewHolder(DataViewHolder holder, final int position) {
holder.countryListTxt.setText(mData.get(position).getName() + "");
holder.countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listner.onSendIdName(mData.get(position).getId(), mData.get(position).getName());
if (context instanceof RegisterActivity) {
((RegisterActivity) context).dismissCountryDialog();
}
}
});
}
}