【问题标题】:How to share an Arraylist through Intent.ACTION_SEND如何通过 Intent.ACTION_SEND 共享一个 Arraylist
【发布时间】:2018-11-16 13:47:43
【问题描述】:

我正在开发一个应用程序,它有一个包含产品名称及其数量的 Arraylist。现在我想通过 WhatsApp、电子邮件或任何他能做到的方式分享这些信息,比如列表。 示例:

List<Compartilhar> listaCompras2 = new ArrayList<>( );

//listaCompras2 has a for loop to to get a new content every time the client input a product and quantity.
listaCompras2.add(new Compartilhar(doc.getString("inputNome"), doc.getString("inputQtd")));

fabShare = view.findViewById(R.id.fabShare);
fabShare.setOnClickListener(new View.OnClickListener( ) {

@Override
public void onClick(View v) {

Intent shareIntent = new Intent(Intent.ACTION_SEND);

    for (int i=0 ; i < listaCompras2.size(); i++)
    {
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Produto: " + listaCompras2.get(0).getProduto() + "      Qtd: " + listaCompras2.get(0).);
        i++;
    }
    startActivity(Intent.createChooser(shareIntent, "share Via"));
}
});

导入 java.util.ArrayList;

导入 java.util.List;

公共类 Compartilhar 实现 Parcelable {

private List<String> produto;
private List<String> qtd;

public Compartilhar(List<String> produto, List<String> qtd) {
    this.produto = produto;
    this.qtd = qtd;
}

public List<String> getProduto() {
    return produto;
}

public void setProduto(List<String> produto) {
    this.produto = produto;
}

public List<String> getQtd() {
    return qtd;
}

public void setQtd(List<String> qtd) {
    this.qtd = qtd;
}

protected Compartilhar(Parcel in) {
    if (in.readByte() == 0x01) {
        produto = new ArrayList<String>();
        in.readList(produto, String.class.getClassLoader());
    } else {
        produto = null;
    }
    if (in.readByte() == 0x01) {
        qtd = new ArrayList<String>();
        in.readList(qtd, String.class.getClassLoader());
    } else {
        qtd = null;
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (produto == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(produto);
    }
    if (qtd == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(qtd);
    }
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Compartilhar> CREATOR = new Parcelable.Creator<Compartilhar>() {
    @Override
    public Compartilhar createFromParcel(Parcel in) {
        return new Compartilhar(in);
    }

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

}

【问题讨论】:

    标签: java android google-cloud-firestore


    【解决方案1】:

    要将 ArrayList 放入 Intent 中,您可以使用 putParcelableArrayListExtra(String name, ArrayList value) 方法。请注意,此方法允许传递实现 Parcelable 接口的项目列表。所以在你的情况下,你的 Compartilhar 类应该实现 Parcelable 接口。

    据我了解,您希望将这些项目共享给其他应用程序,这可能会很棘手,因为每个独立应用程序(Whatsapp 等)都有自己的逻辑,它们如何处理/解析来自意图的数据。

    【讨论】:

    • 那么有什么办法可以把这个 Arraylist 变成一个文档然后分享这个文档?
    • 您可能可以创建一个本地文件并将您的列表写入此文件(并格式化文件),然后使用此文件发送一个隐式共享意图,看看哪个应用程序可以处理您的意图。跨度>
    • 正如您的第一个答案所说,我实现了我的 Compartilhar 类并编辑了这篇文章。我现在如何更改我的代码以使其发送列表?
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2013-03-19
    • 2020-02-13
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多