将所选项目从上下文菜单发送到另一个活动

时间:2017-10-11 08:30:25

标签: java android listview android-activity android-contextmenu

我为ContextMenu制作了ListView,一切都运转良好。但是,我无法弄清楚如何将所选项目发送到其他活动?

我看了很多教程但没找到解决方案。

每个项目代表两列,其中两个字符串(product,price)使用JSON字符串进行解析。

 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;
            }
        }

1 个答案:

答案 0 :(得分:0)

您可以VanzatorProduse实现Parcelable接口,以便将其放入用于启动新活动的Intent中。如果你说这个类同时具有产品和价格,一个可能的实现可能如下:

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);
    }
}

然后在您的onActionItemClicked上,您只需声明ArrayList VanzatorProduse并将其添加到Intent,如下所示:

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);

最后,在您的第二个活动中,您可以从onCreate方法中检索值,如下所示:

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

希望它有所帮助。