Crasha app when setting an Adapter for a Recyclerview

Asked

Viewed 16 times

0

Hello, I’m new to Recyclerview, I’m trying to create an Adapter for it, but when I define an Adapter for Recyclerview, the crash app, someone knows why?

Mainactivity

/...
RecyclerView recyclerView = findViewById(R.id.recycler_view);

RecyclerView.LayoutManager recyclerManager;

ArrayList<String> listDrawer = new ArrayList<>();
listDrawer.add("Teste");

recyclerView.setHasFixedSize(true);
recyclerManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerManager);

//Agora quando defino a Adapter, o app crasha
recyclerView.setAdapter(new RecyclerAdapter(listDrawer));
/...

Recycleradapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.DataObjectHolder> {
    private ArrayList<String> data;

    public class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        
        public DataObjectHolder(View itemView) {
            super(itemView);
        }
        
        @Override
        public void onClick(View v) {
           
        }
    }
    
    public RecyclerAdapter(ArrayList<String> data) {
        this.data = data;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.main_nav_files_items, parent, false);
            
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }
    
    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {

    }

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

1 answer

0


I solved this plobema, I do not know the reason of the error, but using this Adapter, everything worked right

Recycleradapter

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

    private ArrayList<HashMap<String, Object>> data = new ArrayList<>();

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

 @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.file_item, parent, false);
        return new ViewHolder(view);
    }

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

    public RecyclerAdapter(String path) {
        //...
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.