How to pass a Listobject by Intent to another Activity?

1

Good morning, I have an application that I am developing in which I am trying to pass the adapter from RecyclerView by means of Intent to another Activity I do not know what I am doing wrong, annex code, before hand I appreciate your collaboration.

**** This is my Adapter ****

public class AdaptadorPedido extends RecyclerView.Adapter<AdaptadorPedido.MyHolder> {

    Context context;
    List<DataAdapter> datos;
    String[] etValArr;
    String[] Fin;

    public AdaptadorPedido(Context context, List<DataAdapter> datos) {
        this.context = context;
        this.datos = datos;
        etValArr = new String[datos.size()];
        Fin = new String[datos.size()];
    }

    @Override
    //public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_view, parent, false);
        MyHolder mh = new MyHolder(view, new CustomEtListener());

        return mh;
        //return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        holder.Nproducto.setText(datos.get(position).getNombre());
        holder.Lab.setText(datos.get(position).getLabo());
        holder.Lista.setText(datos.get(position).getList());
        holder.precio.setText(datos.get(position).getPre());
        holder.Cantidad.setText(datos.get(position).getCant());

        holder.myCustomEtListener.updatePosition(position);
        //holder.Cantidad.setText(etValArr[position]);



        holder.Nproducto.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
                adb.setTitle("Eliminar?");
                adb.setMessage("¿Seguro que desea sacar este producto de la OP ?");
                adb.setNegativeButton("No",null);
                adb.setPositiveButton("Si",new AlertDialog.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        datos.remove(position);
                        notifyDataSetChanged();
                    }
                });
                adb.show();
                return false;
            }
        });
        holder.Lab.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
                adb.setTitle("Eliminar?");
                adb.setMessage("¿Seguro que desea sacar este producto de la OP ?");
                final int positionToRemove = position;
                adb.setNegativeButton("No",null);
                adb.setPositiveButton("Si",new AlertDialog.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        datos.remove(position);
                        notifyDataSetChanged();
                    }
                });
                adb.show();
                return false;
            }
        });

    }

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


    public class MyHolder extends RecyclerView.ViewHolder{

        TextView Nproducto, Lab, Lista, precio;
        EditText Cantidad;
        public CustomEtListener myCustomEtListener;

        //public MyHolder(View itemView) {
        public MyHolder(View itemView, CustomEtListener myList) {
            super(itemView);

            Nproducto = (TextView)itemView.findViewById(R.id.tv_Producto);
            Lab = (TextView)itemView.findViewById(R.id.tv_Lab);
            Lista = (TextView)itemView.findViewById(R.id.tv_Lista);
            precio = (TextView)itemView.findViewById(R.id.tv_Precio);
            Cantidad = (EditText)itemView.findViewById(R.id.et_cant);

            myCustomEtListener = myList;
            Cantidad.addTextChangedListener(myCustomEtListener);
        }
    }

    public static class DataAdapter{
        String Nombre,Labo, List, Pre, Cant;

        public DataAdapter(String Nombre,String Labo,String List,String Pre,String Cant){
            this.Nombre = Nombre;
            this.Labo = Labo;
            this.List = List;
            this.Pre = Pre;
            this.Cant = Cant;
        }

        public String getNombre() {
            return Nombre;
        }

        public void setNombre(String nombre) {
            Nombre = nombre;
        }

        public String getLabo() {
            return Labo;
        }

        public void setLabo(String labo) {
            Labo = labo;
        }

        public String getList() {
            return List;
        }

        public void setList(String list) {
            List = list;
        }

        public String getPre() {
            return Pre;
        }

        public void setPre(String pre) {
            Pre = pre;
        }

        public String getCant() {
            return Cant;
        }

        public void setCant(String cant) {
            Cant = cant;
        }
    }

    private class CustomEtListener implements TextWatcher{
        private int position;

        public void updatePosition(int position){
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //etValArr[position] = s.toString();
            datos.get(position).setCant(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    }

}

now I attach the code of the activity that should send me the data to the following by means of Intent

btnok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int ele = Pedido.getChildCount();
                if(ele==0){
                    new AlertDialog.Builder(Pedido.this)
                            .setTitle("Error")
                            .setIcon(R.drawable.ic_action_name)
                            .setMessage("La OP debe tener como minimo un Producto")
                            .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                }else {
                    Intent CierOP = new Intent(Pedido.this, CierreOP.class);
                    Bundle bund = new Bundle();
                    ArrayList<AdaptadorPedido.DataAdapter> t = (ArrayList<AdaptadorPedido.DataAdapter>)Adaptador;
                    Pedido.getAdapter();
                    CierOP.putExtra("Productos", t);
                    CierOP.putExtra("Cliente", Cli);
                    CierOP.putExtra("Cedula", Cel);
                    startActivity(CierOP);
                }
            }
        });

When I run it with an interruption point this gives me the following Excepction

Parcel: unable to marshal value com.example.ddanthe.tpedidosv1.AdaptadorPedido$DataAdapter@d657529
    
asked by Diego Andres Tovar Cardozo 09.05.2017 в 18:25
source

1 answer

3

First your object must implement the Serializable class:

public class MyObjeto implements Serializable {

You would send an ArrayList of objects in Intent using .putExtra() :

Intent intent = new Intent(MainActivity.this, SegundaActivity.class);
intent.putExtra("listaObjetos", listaObjetos);
startActivity(intent);

To receive the ArrayList of objects in the Activity is done this way:

ArrayList<MyObjeto> lista = (ArrayList<MyObjeto> ) getIntent().getSerializableExtra("listaObjetos");
    
answered by 09.05.2017 / 19:01
source