当应用程序启动时,我得到一个空白屏幕,当我检查日志时,我得到:E/RecyclerView:未连接适配器;跳过布局错误
我不明白为什么?任何想法,它似乎没有附加 recyclerView 或添加任何数据,我已经附加了 Main Activity 、DataAdapter 和数据类。
主要 Activity
public class MainActivity extends AppCompatActivity {
private static final String BASE_URL = "http://demo3317540.mockable.io";
private RecyclerView recyclerView;
private List<Included> data;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<>();
initViews();
loadJSON();
}
private void initViews() {
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
}
private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JsonResponse> call = request.getYesterData();
call.enqueue(new Callback<JsonResponse>() {
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
JsonResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getIncluded()));
adapter = new Adapter(data);
recyclerView.setAdapter(adapter);
}
数据适配器
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private List<Included> includedData;
public Adapter(List<Included> includedData) {
this.includedData = includedData;
}
@Override
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tvCompetitionName.setText(includedData.get(position).getAttributes().getName());
holder.homeTeam.setText(includedData.get(position).getRelationships().getCompetition().getData().getType());
holder.itemView.setTag(includedData.get(position));
}
@Override
public int getItemCount() {
return includedData.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvCompetitionName;
private TextView homeTeam;
private TextView score;
public ViewHolder(View itemView) {
super(itemView);
tvCompetitionName = (TextView) itemView.findViewById(R.id.competitionNameTV);
homeTeam = (TextView) itemView.findViewById(R.id.homeTeamTv);
score = (TextView) itemView.findViewById(R.id.score);
}
}
}
下面是我的数据类,将从其中提取数据
包含.java
public class Included implements Serializable, Parcelable
{
@SerializedName("relationships")
@Expose
private Relationships relationships;
@SerializedName("attributes")
@Expose
private Attributes attributes;
@SerializedName("type")
@Expose
private String type;
@SerializedName("id")
@Expose
private String id;
}
getter 和 setter
这是 jsonResponse 类,它在接口(interface)中被引用
public class JsonResponse {
private Included[] included;
public Included[] getIncluded() {
return included;
}
}
感谢任何帮助。
您需要初始化适配器并调用 recyclerview.setAdapter(adapter)
我是一名优秀的程序员,十分优秀!