RecycleViewer没有连接适配器。 URL上也有错误

时间:2019-01-05 19:14:24

标签: java android android-studio retrofit recycler-adapter

大家好,我正在创建一个应用程序,我想在其中使用改造功能来调用Api。并在回收查看器中显示对象。我在尝试使用调用Api的方法并将对象放在回收查看器中时遇到错误。

下面是寻呼机适配器的代码。

public class IssueAdapter2 extends RecyclerView.Adapter<IssueAdapter2.SymptomViewHolder> {
    private ArrayList<Issue> dataList;

public IssueAdapter2(ArrayList<Issue> dataList){
    this.dataList = dataList;
}

@NonNull
@Override
public SymptomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
    View view = layoutInflater.inflate(R.layout.row_item, viewGroup, false);
    return new SymptomViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SymptomViewHolder symptomViewHolder, int position) {
    symptomViewHolder.symptomTitle.setText(dataList.get(position).getName());

}

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

public class SymptomViewHolder extends RecyclerView.ViewHolder {

    TextView symptomTitle;

    public SymptomViewHolder(@NonNull View itemView) {
        super(itemView);
        symptomTitle = itemView.findViewById(R.id.IssueName);
    }
}

还有我称为改造的代码以及创建回收视图的方法:

public class MainActivity extends AppCompatActivity {

    private Button btnDiagnosis;
    private RadioButton radioButton;
    public TextView textResults;
private RecyclerView recyclerView;
private IssueAdapter2 issueAdapter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_layout);

    btnDiagnosis = findViewById(R.id.bDiagnosis);

    radioButton = findViewById(R.id.feverYes);

    btnDiagnosis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startButtonActivity();
        }
    });


}

   public void startButtonActivity(){
        //create Retrofit instance
       setContentView(R.layout.results);
       //textResults = findViewById(R.id.resultsview);

       Retrofit.Builder builder = new Retrofit.Builder()
               .baseUrl("https://priaid-symptom-checker-v1.p.mashape.com/diagnosis/")
               .addConverterFactory(GsonConverterFactory.create());

       Retrofit retrofit = builder.build();

       //get Symptoms


       JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
       Call<ArrayList<Issue>> call = jsonPlaceHolderApi.searchForIssues(

               "male",
               "10,5",
               "en-gb",
               1994);

       Log.wtf("URL Called", call.request().url() + "");
       call.enqueue(new Callback<ArrayList<Issue>>() {
           @Override
           public void onResponse(Call<ArrayList<Issue>> call, Response<ArrayList<Issue>> response) {

               if(response.isSuccessful()){
                   configViews(response.body());
               }
           }
           @Override
           public void onFailure(Call<ArrayList<Issue>> call, Throwable t) {
               Toast.makeText(MainActivity.this, "Something went wrong...Error message: " + t.getMessage(),
                       Toast.LENGTH_LONG).show();
           }
       });
   }

    private void configViews(ArrayList<Issue> issueArrayList) {



        recyclerView = findViewById(R.id.recyclerview);

        issueAdapter2 = new IssueAdapter2(issueArrayList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(issueAdapter2);

    }

}

我遇到的错误:

  

E / URL,称为:https://priaid-symptom-checker-v1.p.mashape.com/diagnosis?Gender=male&Symptoms=10,5&Language=en-gb&Year%20of%20Birth=1994

E / RecyclerView:未连接适配器;跳过布局

如果有人能告诉我如何克服这个问题以及我的recycleview如何获得适配器,请

非常感谢您!

1 个答案:

答案 0 :(得分:0)

大多数在主线程上未调用call.enqueue(...)的回调。在Android上,这是唯一可以更新UI的地方。在主线程上设置适配器应该可以解决您的问题:

 private void configViews(ArrayList<Issue> issueArrayList) {
    recyclerView = findViewById(R.id.recyclerview);
    issueAdapter2 = new IssueAdapter2(issueArrayList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(issueAdapter2);
        }
    );
}