简体   繁体   English

回收站视图:未连接适配器; 跳过布局(API 的回收站视图中的错误)

[英]Recycler view: no adapter attached; skipping layout(Error in recycler view for API )

This is the recycler view activity.这是回收者视图活动。 Here I have initialized the recycler view.在这里,我已经初始化了回收站视图。 Despite that I am getting error.尽管我得到了错误。 Where might have I gone wrong.我可能哪里出错了。 In the log cat I get the error在日志猫中,我得到了错误

No adapter attached; skipping layout No adapter attached; skipping layout . No adapter attached; skipping layout

Here I am getting response display the response in the recycler view using APIs.在这里,我正在使用 API 在回收站视图中显示响应。 I am also using retrofit too.我也在使用 retrofit 。 In the get data() function I am taking the response status and getting data.get data() function 中,我正在获取响应状态并获取数据。

public class MainActivity extends AppCompatActivity {



    RecyclerView recyclerView;
    ListAdapter1 listAdapter;
//    List<SupermarketModels> supermarketModelsList = new ArrayList<>();
    ApiInterface apiInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initialization();
        getdata();
    }

    private void initialization(){
        recyclerView = findViewById(R.id.recyclerview);
        Retrofit retrofit = APIClient.getclient();
        apiInterface = retrofit.create(ApiInterface.class);
    }

    private void setadapter(List<SupermarketModels> supermarketModels){

        listAdapter = new ListAdapter1(this, supermarketModels);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(listAdapter);

        listAdapter.notifyDataSetChanged();


    }
    private void getdata(){
        apiInterface.getList().enqueue(new Callback<GetListResponse>() {
            @Override
            public void onResponse(Call<GetListResponse> call, Response<GetListResponse> response) {

                try {
                    if (response!= null){
                        if (response.body().getStatus().equals("1")){
                            setadapter(response.body().getData());


                        }
                        else {
                            Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e){
                    Log.e("exp", e.getLocalizedMessage());

                }

            }

            @Override
            public void onFailure(Call<GetListResponse> call, Throwable t) {

            }
        });

    }
} 

you have to setOrientation() to your layout manager.and remove notifyDataSetChanged() line from there.你必须setOrientation()到你的布局管理器。并从那里删除notifyDataSetChanged()行。

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); //or HORIZONTLE,whatever you want
    
            recyclerView.setLayoutManager(linearLayoutManager);
    
            recyclerView.setAdapter(listAdapter);
    
            listAdapter.notifyDataSetChanged(); //remove this line from here

This error can be avoid setting empty adapter first before setting actual adapter, when data is actually loading.这个错误可以避免在实际加载数据时,在设置实际适配器之前先设置空适配器。 In your example before calling getdata() you just call empty adapter like this在调用getdata()之前的示例中,您只需像这样调用空适配器

EmptyAdapter emptyAdapter = new EmptyAdapter();
recyclerView.setAdapter(emptyAdapter);

Here is Empty Adapter class, which you need to create这是您需要创建的空适配器 class

import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

public class EmptyAdapter extends RecyclerView.Adapter<EmptyAdapter.EmptyHolder> {
@Override
public EmptyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return null;
}

@Override
public void onBindViewHolder(EmptyHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 0;
}

class EmptyHolder extends RecyclerView.ViewHolder {
    public EmptyHolder(View itemView) {
        super(itemView);
    }
}
}

First, let's create a simple adapter which accepts no data and has a method which allows us to add data and notify adapter.首先,让我们创建一个简单的不接受数据的适配器,并有一个方法允许我们添加数据并通知适配器。

package adapters;

// TODO: Replace with your package

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

// TODO: Replace import of your package
import tech.gjirafa.tonosdemo.R;

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {

private Context context;
// TODO: Replace data type from String with your model
private ArrayList<String> data = new ArrayList<>();

public SimpleAdapter(Context context) {
    this.context = context;
}

// TODO: Replace data type from String with your model
public void setData(ArrayList<String> data) {
    this.data.addAll(data);
    notifyDataSetChanged();
}

@NonNull
@Override
public SimpleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // TODO: adapter_news_item with your layout
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_news_item, parent, false));
}

@Override
public void onBindViewHolder(@NonNull SimpleAdapter.ViewHolder holder, int position) {
    // TODO: Add your bindings
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

    // TODO: Declare your views here so you can access them onBindViewHolder()
    
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
    }
}
}

Then on your activity, do the following:然后在您的活动中,执行以下操作:

package activities;

// TODO: Replace with your package

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// TODO; Replace with your packageId

import com.google.gson.JsonObject;

import adapters.SimpleAdapter;
import tech.gjirafa.tonosdemo.R;

public class MainActivity extends AppCompatActivity {

SimpleAdapter simpleAdapter = new SimpleAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // TODO: Replace your layout
    setContentView(R.layout.activity_demo);
    setupAdapter();
    fetchData();
}

private void setupAdapter() {
    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setAdapter(simpleAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
}

private void fetchData() {
    APIClient.getclient().create(ApiInterface.class).getList().enqueue(new Callback<GetListResponse>() {
        @Override
        public void onResponse(Call<GetListResponse> call, Response<GetListResponse> response) {
            try {
                if (response!= null){
                    if (response.body().getStatus().equals("1")){
                        simpleAdapter.setData(response.body().getData());
                    } else {
                        Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (Exception e){
                Log.e("exp", e.getLocalizedMessage());
            }
        }

        @Override
        public void onFailure(Call<GetListResponse> call, Throwable t) {
            Log.e("onFailure", t.getLocalizedMessage());
        }
    });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM