Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How can I share the text from a List Item in an application?

I'm trying to use the Share Intent to share a list item from the listView in my application. When the user selects an item from the listView and holds it down, they should have the option to share the text from the item. I've been able to do the Share Intent to share other things from the application but I can't figure out how to make it work with the listView.

Here's my current code:

'''java import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView;

public class ReadAll extends Activity {

ListView list;
String[] answers;

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

    answers = getResources().getStringArray(R.array.answers);

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, answers) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        View view = super.getView(position, convertView, parent);
                        TextView text = (TextView) view.findViewById(android.R.id.text1);
                        text.setTextColor(Color.WHITE);
                        return view;
                    }

                };
    list = (ListView) findViewById(R.id.listView);
    list.setAdapter(adapter);
}

//CODE TO SHARE ANSWER...hint this part is not working...

@Override
public boolean onCreateOptionsMenu (Menu menu){
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.read_all, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected (MenuItem item){
    int itemId = item.getItemId();
    if (itemId == R.id.action_share) {
        shareAnswer();
    }

    return super.onOptionsItemSelected(item);
}

private void shareAnswer() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, list.getSelectedItem().toString() );
    startActivity(Intent.createChooser(shareIntent, getString(R.string.share_chooser_title)));
}

}

//END CODE TO SHARE ANSWER

'''

1 Answer

Hi Naomi

Firstly I would change your shareAnswer() method to accept a variable so we can re-use the same code to send whatever we wish.

private void shareAnswer(String answer) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, answer);
    startActivity(Intent.createChooser(shareIntent, getString(R.string.share_chooser_title)));
}

Now what you need to do is implement the onListItemClicked() method. What you can do then is use the position of the item clicked to reference the same position in your answers array which you used to populate the list. You can select the particular string in question then pass it into your shareAnswer() method using something like the code below (please be aware I can't see the code you posted very clearly, might be my screen? so I haven't checked it. Should be on the right track though).

There is a link to the documentation below also.

Hint: http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)

onListItemClick(ListView l, View v, int position, long id){

    shareAnswer(answers[position]);

}

Thanks so much for your help Daniel! It really helped me in solving the problem. For some reason the onListItemClick method didn't work for me but I managed to solve the problem using your idea of getting the shareAnswer() method to accept a variable and then using the setOnItemClickListener() method (shown below for anyone who's interested) instead of the onListItemClick().

list.setOnItemClickListener(

                new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        shareAnswer(answers[i]);
                    }

                }

        );

Great stuff!

Glad I could help, not entirely sure put perhaps extending ListActivity instead of a regular Activity would of allowed the onListItemClicked() method call. Never mind glad you've got it all working

Daniel

Hmm, good point. I'll try that another time & see. Thanks!