How can I pass several items in another activity

0

I have a list full of items, and I wanted to pass 3 of the items in another activity, then I go back to the list to go through 3 other items more ... so on (the chosen items should appear as a list).

This is my code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    MenuInflater inflate = getMenuInflater();
    if (v.getId() == R.id.listView) {

        inflate.inflate(R.menu.menu_main, menu);
    }
}
public boolean onContextItemSelected(final MenuItem item) {
    AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    GetContacts get = new GetContacts();
    String datas=get.doInBackground().toString();
    Log.d("RESULT",datas);
        int id = info.position;
    View view=info.targetView;
    switch (item.getItemId()) {

        case  R.id.iva:
            TextView textView3 = (TextView) view.findViewById(R.id.txtCodigo);
            TextView textView4 = (TextView) view.findViewById(R.id.txtDescrip);
            TextView textView5 = (TextView) view.findViewById(R.id.txtPrecio);

            String text3 = textView3.getText().toString();
            String text4 = textView4.getText().toString();
            String text5 = textView5.getText().toString();
            Intent intent3= new Intent(busqueda.this,Carrito.class);
            final Intent intent = intent3.putStringArrayListExtra("CODE", ArrayList);
            intent3.putExtra("CODE", text3);
            intent3.putExtra("PRODUC", text4);
            intent3.putExtra("PRECIO", text5);


            startActivity(intent3);
    }
return true;

and the other activity that it receives ... it receives only a single row of item ... I want to add in the other activity as list all the selected items ... THANK YOU

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carrito);
    productos = new ArrayList<>();
    texto=(TextView)findViewById(R.id.textView3);
    txtcod=(TextView)findViewById(R.id.textView4);
    txtpre=(TextView)findViewById(R.id.textView5);

    final Intent intent = getIntent();
    Bundle extra = intent.getExtras();
    if (extra != null) {
        String dato = extra.getString("CODE");
        String Tok = extra.getString("PRODUC");
        String Token2 = extra.getString("PRECIO");

        texto.setText(dato);
        txtcod.setText(Tok);
        txtpre.setText(Token2);

    }
}
    
asked by Wid Maer 22.06.2017 в 17:04
source

1 answer

1

As an option is to create an object with the fields "CODE", "PRODUC" and "PRICE", it is important to note that this object must implement the class Serializable so that it can be sent using a bundle between Activities , for example:

import java.io.Serializable;

public class Producto implements Serializable{

    private String code;
    private String produc;
    private String precio;


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getProduc() {
        return produc;
    }

    public void setProduc(String produc) {
        this.produc = produc;
    }

    public String getPrecio() {
        return precio;
    }

    public void setPrecio(String precio) {
        this.precio = precio;
    }

}

Subsequently, create a ArrayList where you store these elements with their respective values and in this way you can send the ArrayList with the information between Activities .

Send ArrayList between Activities.

But definitely the ideal way is to send the data between Activities through the bundle .

Where your object must implement class Serializable :

public class Dato implements Serializable {

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

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

To receive the ArrayList in the Destination Activity, it is done in the following way:

ArrayList<listaDatos> listaDatos = (ArrayList<listaDatos> ) getIntent().getSerializableExtra("listaDatos");
    
answered by 22.06.2017 в 21:13