如何通过Intent.ACTION_SEND分享Arraylist

问题描述 投票:0回答:1

我正在开发一个应用程序,它有一个Arraylist,其中包含产品名称及其数量。现在,我想通过WhatsApp,电子邮件或其他任何方式制作一个Intent来分享这些信息。例:

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

import java.util.ArrayList;

import java.util.List;

公共类Share实现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个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.