Android Studio RecycleViewer没有连接适配器,URL上也有错误

3htmauhk  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(129)

我正在创建一个应用程序,我想在其中调用一个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);
    }
}

我调用reflection的代码和创建recycle视图的方法:

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/回收机视图:未连接适配器;跳跃布局

请,如果有人可以告诉我如何克服这一点,以及如何我的recycleview将获得适配器

非常感谢!

o7jaxewo

o7jaxewo1#

很可能是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);
        }
    );
}

相关问题