首页 > 解决方案 > RecyclerView 没有附加适配器;跳过布局(无法​​从以前的问题中解决)

问题描述

我正在尝试构建一个应用程序,该应用程序应该使用开源运动 API 来获取足球(足球)锦标赛列表并将其显示在 RecyclerView 上,由于某种原因,我收到两个错误:

1- com.mad.footstats E/RecyclerView:未连接适配器;跳过布局

2- E/MainActivity: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径接受格式错误的 JSON

我无法从以前对相同问题的答案中使它起作用,所以请您花时间看看我的代码:这是 MainActivity:

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private final static String API_KEY = "w7c74newrykj8m57rda6xwrk";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Toast if the API key is empty
    if(API_KEY .isEmpty()){
        Toast.makeText(getApplicationContext(), R.string.api_empty_toast, Toast.LENGTH_SHORT).show();
    }

    final RecyclerView mRecyclerView = findViewById(R.id.tournaments_rv);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);
    Call<TournamentResponse> call = apiService.getTournamentList(API_KEY);
    call.enqueue(new Callback<TournamentResponse>() {
        @Override
        public void onResponse(Call<TournamentResponse> call, Response<TournamentResponse> response) {
            int statusCode = response.code();
            List<Tournament_> tournaments = response.body().getResults();
            mRecyclerView.setAdapter(new TournamentsAdapter(tournaments,R.layout.tournament_item,getApplicationContext()));
        }

        @Override
        public void onFailure(Call<TournamentResponse> call, Throwable t) {
            Log.e(TAG, t.toString());
        }
    });
}

这是 TournamentsAdapter 的代码:

public class TournamentsAdapter extends RecyclerView.Adapter<TournamentsAdapter.LeagueViewHolder>{

private List<Tournament_> mTournaments;
private int rowLayout;
private Context context;

public static class LeagueViewHolder extends RecyclerView.ViewHolder {
    LinearLayout TournamentLayout;
    TextView tournamentName, tournamentNation, tournamentYear;

    public LeagueViewHolder(View v) {
        super(v);
        TournamentLayout = v.findViewById(R.id.league_layout);
        tournamentName = v.findViewById(R.id.tournament_name);
        tournamentNation = v.findViewById(R.id.tournament_nation);
        tournamentYear = v.findViewById(R.id.tournament_year);
    }
}

public TournamentsAdapter(List<Tournament_> tournaments, int rowLayout, Context context){
    this.mTournaments = tournaments;
    this.rowLayout = rowLayout;
    this.context = context;
}

@Override
public TournamentsAdapter.LeagueViewHolder onCreateViewHolder(ViewGroup parent,
                                                              int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
    return new LeagueViewHolder(view);
}

@Override
public void onBindViewHolder(LeagueViewHolder holder, final int position){
    holder.tournamentName.setText(mTournaments.get(position).getName());
    holder.tournamentNation.setText(mTournaments.get(position).getCurrentSeason().getYear());
    holder.tournamentYear.setText(mTournaments.get(position).getCategory().getName());
}

@Override
public int getItemCount() {
    return mTournaments.size();
}

这是 ApiClient 类的代码:

public class ApiClient {

public static final String BASE_URL = "https:api.sportradar.us/soccer-xt3/eu/en/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

由于某种原因,适配器未连接,API 无法正常工作,即使我看到了具有相同代码的示例和教程。谢谢。

标签: javaandroidandroid-recyclerview

解决方案


问题出在您的链接上。您为端点选择了错误的参数。您将永远不会收到 json 响应,因此改造无法为您解析它并且您正在将您的回收器视图填充到从未调用过的改造响应中。

只需将 xml 更改为 json 即可获得正确的响应。

https://api.sportradar.us/soccer-xt3/eu/en/tournaments.xml?api_key=API_KEY

https://api.sportradar.us/soccer-xt3/eu/en/tournaments.json?api_key=API_KEY


推荐阅读