How to share content from a listiview?

Asked

Viewed 54 times

0

Hi, I have a question. I’m making an APP, which has a listview that has text and an image. I would like to know how I can share the content the person clicked on.

Just follow my code:

// DECLARANDO COMPONENTES

    private ListView list;



        // ARRAY CONTRA-AC ________________________________________________________________________________

    String[] ac_Contra ={
            "Flaviano Melo",
            "Jéssica Sales"
        };

    Integer[] ac_Contra_Imgid={
            R.drawable.flaviano_melo,
            R.drawable.jessica_sales,
    };


    String[] ac_Contra_ComoVotou={
            "SIM"
    };


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // VINCULANDO COMPONENTES

        list = (ListView) findViewById(R.id.list);


        // CAPTURANDO INFORMAÇÕES DE OUTRA ACTIVITY

        Bundle extra = getIntent().getExtras();

        // Condição para execução do comando

        if (extra != null)

        {
            String textoTransferido1 = extra.getString("CONTRA");
            String textoTransferido2 = extra.getString("FAVOR");
            String textoTransferido3 = extra.getString("INVESTIGADOS");

            // CONDIÇÃO - ESTADO AC

            if ("CONTRA-AC".equals(textoTransferido1))
            {
                CustomListAdapter adapter=new CustomListAdapter(this, ac_Contra, ac_Contra_Imgid, ac_Contra_partido,ac_Contra_ComoVotou);
                list=(ListView)findViewById(R.id.list);
                list.setAdapter(adapter);
            }

// EVENTO DE CLIQUE


        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {


Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);


            }
        });

THANK YOU VERY MUCH!!!

  • Share as? Send to another screen?

  • Share on social networks?

1 answer

2

All you have to do is capitulate the position relative to the String vector inside the setOnItemClickListener. See below for an example:

String text = ac_Contra[position];  

How it should look in the onItemClick:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String text = ac_Contra[position];

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        // aqui você define o texto para compartilhamento.
        sendIntent.putExtra(Intent.EXTRA_TEXT, text);
        sendIntent.setType("text/plain");
        startActivity(sendIntent);

    }
});

Following this same logic, you define other values for the variable text in which you want to share through Intent.ACTION_SEND.

  • Thanks a lot, acklay! The text content worked perfectly. How could you share the images that are in listview? Includes the following lines, but without success : sendIntent.putExtra(Intent.EXTRA_STREAM, ac_Contra_Imgid[position]); sendIntent.setType("image/jpeg");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.