2

I have made a ContextMenu for a ListView, and everything is working perfectly. However, I can't figure out how I can send selected items to another activity?

I have looked at a lot of tutorials but found no solution.

Each item represents two columns with two strings (product, price) parsed with a JSON string.

 listViewProduse.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listViewProduse.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            final int checkedCount = listViewProduse.getCheckedItemCount();
            mode.setTitle(checkedCount + " product selected");
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.main_context, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()){
                case R.id.add_id:
                    SparseBooleanArray selected = adapter.getSelectedIds();

                    String copyText="";
                    for (int i = (selected.size() - 1); i >= 0; i--){
                        if (selected.valueAt(i)){
                            VanzatorProduse selectedListItem = adapter.getItem(selected.keyAt(i));                                
                        }
                    }
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
clauub
  • 1,134
  • 9
  • 19
  • The very basic way to send something to another Activity is using an `Intent` and add the things to be sended as an `Extra`. See https://stackoverflow.com/questions/6707900/pass-a-string-from-one-activity-to-another-activity-in-android, or https://stackoverflow.com/questions/18146614/how-to-send-string-from-one-activity-to-another or many more… – deHaar Oct 11 '17 at 08:33

1 Answers1

0

You can make VanzatorProduse implement the Parcelable interface in order to put it into the Intent used to start the new activity. If you say this class has both the product and price, one possible implementation could be as follows:

public class VanzatorProduse  implements Parcelable {
    private String product;
    private double price;

    public VanzatorProduse(String product, double price) {
        this.product = product;
        this.price = price;
    }

    protected VanzatorProduse(Parcel in) {
        product = in.readString();
    }

    public static final Creator<VanzatorProduse> CREATOR = new   Creator<VanzatorProduse>() {
        @Override
        public VanzatorProduse createFromParcel(Parcel in) {
            return new VanzatorProduse(in);
        }

        @Override
        public VanzatorProduse[] newArray(int size) {
            return new VanzatorProduse[size];
        }
    };

    public String getProduct() {
        return product;
    }

    public double getPrice() {
        return price;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(product);
        parcel.writeDouble(price);
    }
}

Then on your onActionItemClicked you simply declare an ArrayList of VanzatorProduse and add it to the Intent as follows:

ArrayList<VanzatorProduse> products = new ArrayList<>();
for (int i = (selected.size() - 1); i >= 0; i--) {
    if (selected.valueAt(i)) {
        VanzatorProduse selectedListItem = adapter.getItem(selected.keyAt(i));      
        products.add(selectedListItem);      
    }
}
Intent intent = new Intent(YourActivity.this, YourSecondActivity.class);
intent.putParcelableArrayListExtra("your_key", products);
startActivity(intent);

Finally, on your second activity you retrieve the values from the onCreate method as follows:

Intent intent = getIntent();
ArrayList<VanzatorProduse> products = intent.getParcelableArrayListExtra("your_key"); // same key used as before

Hope it helps.

fergaral
  • 2,077
  • 6
  • 17
  • 34
  • how can I display it in a listview wich I have in the second activity ? – clauub Oct 11 '17 at 10:50
  • As I have shown above, in the second activity you already have access to the ArrayList. So, the only thing left is to create an adapter for the RecyclerView by extending the BaseAdapter class, or if your requirements are really, really simple you could use an ArrayAdapter (and override toString on VanzatorProduse to return the text each item would have inside the ListView). – fergaral Oct 12 '17 at 09:30