Error opening dialog with recyclerview, when adding click on list item

Asked

Viewed 152 times

0

Error: RecyclerView﹕ No adapter attached; skipping layout

I saw several items really similar to this error here, but none solved my problem. The list is loaded normally, only after adding the interface, which I created, to click on an item in the list (this click interface is used in other situations, usually), is that the error occurs. And recyclerview is created in alertDialog.

I tried to change the position of the code as I saw that the list might not be filled in or the Adapter not loaded yet, but it didn’t work.

After going to the server, return a list of items and only after this return, create the items related to recyclerview and Adapter. Working with asyncTask, this returns to Activity via a method that is overwritten.

follows the codes

Activity/dialog:

public void buildDialogToPredicted(String whichDialog) {
//método criado, após o retorno para a criação do dialog com a lista de itens preditos.
        startVoiceWave(this, this);
        View viewAlertDialog = LayoutInflater.from(this).inflate(R.layout.predict_layout, null);
        recyclerView = viewAlertDialog.findViewById(R.id.recycler_predict);
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);

        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setView(viewAlertDialog)
                .setCancelable(true);
        final AlertDialog dialog1 = dialog.create();
        Button close = viewAlertDialog.findViewById(R.id.btn_close_predic);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpPager.setCurrentItem(vpPager.getCurrentItem() + 1, true);

                dialog1.dismiss();

            }
        });

        dialog1.show();
        dialog1.getWindow().setLayout(1050, 1400);
        switch (whichDialog) {
            case "predicaoCid.sucesso":
                final List<CidVo> list = MyApplicationInstance.getInstanceApplication().getCidVo();
                recyclerViewAdapterPredictCid = new CustomRecyclerViewAdapterPredictCid(list, this, new CustomItemClickListener() {
                    @Override
                    public void onItemClick(View v, int position) {
                        list.remove(position);
                        recyclerViewAdapterPredictProd.notifyDataSetChanged();
                    }
                });


                recyclerView.setAdapter(recyclerViewAdapterPredictCid);
                break;

            case "predicaoProc.sucesso":
                final List<PredicaoProcedimentoVo> listProc = MyApplicationInstance.getInstanceApplication().getProcedimentoVosList();
                recyclerViewAdapterPredictProd = new CustomRecyclerViewAdapterPredictProd(listProc, this, new CustomItemClickListener() {
                    @Override
                    public void onItemClick(View v, int position) {
                        listProc.remove(position);
                        recyclerViewAdapterPredictProd.notifyDataSetChanged();
                    }
                });

                recyclerView.setAdapter(recyclerViewAdapterPredictProd);
                break;

        }


    }

Adapter:

public class CustomRecyclerViewAdapterPredictProd extends
        RecyclerView.Adapter<CustomRecyclerViewAdapterPredictProd.ViewHolder> implements CustomItemClickListener {

    private AppCompatActivity appCompatActivity;
    private List<PredicaoProcedimentoVo> mDataset;
    // Animation
    private int lastPosition = -1;
    private RadioButton lastCheckedRB = null;
    private ItemAgendaVo itemAgendaVo;
    private CustomItemClickListener listener;
    private View v;

    // Provide a suitable constructor (depends on the kind of dataset)
    public CustomRecyclerViewAdapterPredictProd(List<PredicaoProcedimentoVo> myDataset, AppCompatActivity appCompatActivity, CustomItemClickListener listener) {

        this.appCompatActivity = appCompatActivity;
        this.mDataset = myDataset;
        this.listener = listener;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(
            ViewGroup parent, int viewType) {
        // create a new view
        v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_predict, parent, false);
        // set the view's size, margins, paddings and layout parameters

        return new ViewHolder(v);
    }

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

        PredicaoProcedimentoVo predicaoCidVo = getProcVo(position);

        setAnimation(holder.txtCID, position);

        holder.txtCID.setText(predicaoCidVo.getTextPredictedProc());
        listener.onItemClick(v,position);//erro ocorre nesta linha TODO

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {

        if (mDataset != null) {

            return mDataset.size();
        }
        return 0;
    }

    /**
     * @param position
     * @return
     */
    public PredicaoProcedimentoVo getProcVo(int position) {

        return mDataset.get(position);
    }

    /**
     * Here is the key method to apply the animation
     */
    private void setAnimation(View viewToAnimate, int position) {
        // If the bound view wasn't previously displayed on screen, it's
        // animated
        if (position > lastPosition) {

            Animation animation = AnimationUtils.loadAnimation(
                    appCompatActivity, android.R.anim.slide_in_left);
            // Animation animation = AnimationUtils.loadAnimation(
            // appCompatActivity, R.anim.slide_top_to_bottom);
            // Animation animation =
            // AnimationUtils.loadAnimation(appCompatActivity,
            // R.anim.clockwise_rotation);
            // animation.setRepeatCount(Animation.START_ON_FIRST_FRAME);
            viewToAnimate.startAnimation(animation);

            lastPosition = position;
        }
    }

    @Override
    public void onItemClick(View v, int position) {

    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView txtCID;
        TextView txtCIDDesc;

        ViewHolder(View v) {
            super(v);

            txtCID = itemView.findViewById(R.id.txtCard1_item1);
            txtCIDDesc = itemView.findViewById(R.id.txtCard1_item2);

        }
    }
}

Error occurs in Adapter when it passes through listener.onItemClick(v,position) I appreciate the help.

1 answer

0


Whoa, that’s all right!?

Follows a simple and objective way to implement the code you are in need of:

My Adapter, where RacaClickListener is my interface, I declare it and sign the click method.

public class RacasAdapter extends RecyclerView.Adapter<RacasAdapter.ItemViewHolder> {
private static List<String> dataList;
private LayoutInflater mInflater;
private Context context;
private RacaClickListener clicklistener = null;
private boolean marcado = false;

public RacasAdapter(Context ctx, List<String> data) {
    context = ctx;
    dataList = data;
    mInflater = LayoutInflater.from(context);
}

public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private TextView txtRaca;

    public ItemViewHolder(View itemView) {
        super(itemView);

        itemView.setOnClickListener(this);

        txtRaca = itemView.findViewById(R.id.txtRaca);
    }

    @Override
    public void onClick(View v) {

        if (clicklistener != null) {
            //assinando o clique na posicao xx
            clicklistener.itemClicked(v, getAdapterPosition());
        }
    }
}

public void setClickListener(RacaClickListener listener) {
    this.clicklistener = listener;
}

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

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    holder.txtRaca.setText(dataList.get(position));
}

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

}

Interface Racaclicklistener

public interface RacaClickListener {
void itemClicked(View view, int position);
}

In my Activity I implement the interface

public class RacaPetActivity extends AppCompatActivity implements RacaClickListener

and implement its method:

@Override
public void itemClicked(View view, int position) {
//aqui vc fará suas acoes, no meu caso setei um objeto e chamei minha proxima atividade passando o mPet(que é minha classe model).
    mPet.setRaca(mListaGenerica.get(position));
    Intent intent = new Intent(this, SexoPetActivity.class);
    intent.putExtra("mPet", mGson.toJson(mPet));
    startActivity(intent);
}

Hugs, my dear!

  • Thanks, I ended up answering in English and not here. The error was in Software, I had not implemented it inside a CLICK. Thank you and I mark as an answer!

Browser other questions tagged

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