如何在Android上的RecyclerView中设置数据

问题描述:

我想从服务器获取一些数据并将其显示到Recyclerview。我可以显示这些数据,但有些时候不显示这些数据,只显示progressBar。
我在Log.e中显示此数据并快速显示,但不在RecyclerView中显示此数据,只显示进度。
请参考下面的图片就知道我的意思是:
Click too see如何在Android上的RecyclerView中设置数据

我响应代码:

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); 
       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

这取决于互联网连接速度,有时要花费更多的时间,因为速度较慢的网络,并有理由对logcat的数据的快速显示的是,在logcat中你只是显示终端的数据,其中如回收站视图中有一个用户界面,当所有数据都被填充时它将显示数据 –

+0

@AbdulKawee,如果互联网连接速度较慢,则不会在LogCat中显示数据!为什么在logcat中显示而不显示在recyclerview中?我怎样才能加速显示数据到recyclerview?你可以帮我吗? – Dongle

+0

在循环中检查您的代码行,意味着记录一收到,记录就会显示的每条记录,其中在回收站视图中它将只显示完整收到的数据。 –

试试这个,你没有初始化成功响应的适配器对象。 添加此mAdapter = new CountryAdapter(models,getActivity(),listener);

@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,getActivity(),listener); 
       countryRecycler.setAdapter(mAdapter); 
      } 
     } catch (Exception e) { 
     } 
    } 
+0

感谢我的朋友,但我在onCreate metyhod初始化适配器。在我上面的代码显示一些时间数据到recyclerview并没有显示一些时间。一些时间显示和一些时间不显示!我该如何解决它? – Dongle

+0

试试吧,并运行代码,它会工作 –

+0

@东方,你有onSuccess中的数据,你怎么能初始化适配器在onCreate() –