0

How to get selected item of list view for sharing perticular text?

here is my code,I have tried this but it's not working

public class MainActivity extends ListActivity {

TextView content;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // list = (ListView) findViewById(R.id.list1);
    content = (TextView) findViewById(R.id.output);

    // listView = (ListView) findViewById(R.id.list);
    String[] values = new String[] { "Android Example ListActivity",
            "Adapter implementation", "Simple List View With ListActivity",
            "ListActivity Android", "Android Example",
            "ListActivity Source Code",
            "ListView ListActivity Array Adapter",
            "Android Example ListActivity" };


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

    // Assign adapter to List
    setListAdapter(adapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);

    // ListView Clicked item index
    int itemPosition = position;

    // ListView Clicked item value
    String itemValue = (String) l.getItemAtPosition(position);

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, itemValue);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

}

poojagupta
  • 982
  • 2
  • 12
  • 26

1 Answers1

1

You need to implement list.setOnItemClickListener(new OnItemClickListener()) and fetch values from your ArrayList or Array for particular ListItem as per ListItem position like:

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

             String txt = text.getText().toString();
            Intent sharingIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "Here is the share content body";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, yourarraylist.get(position));
            startActivity(Intent.createChooser(sharingIntent, "Share via"));

        }

    });

Update: go to this link for Create_Listview_With_ListActivity_-_Android_Example

M D
  • 47,665
  • 9
  • 93
  • 114
  • @M D I already tried this but for facebook it is not working for gmail and all it is working – poojagupta Feb 27 '14 at 11:50
  • @poojagupta then it's not fault about ListView item wait i show you something for facebook and for twitter you need to set their Intent class go to this:[http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654](http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654) – M D Feb 27 '14 at 11:53
  • @poojagupta go to this post:[http://stackoverflow.com/questions/8308666/how-to-force-share-intent-to-open-a-specific-app?rq=1](http://stackoverflow.com/questions/8308666/how-to-force-share-intent-to-open-a-specific-app?rq=1) and also i have been worked on this and got that Facebook official app not provide Intent direct support – M D Feb 27 '14 at 11:56
  • @poojagupta first of all tell how many items you want to show in your listView? or post one snap into question – M D Feb 27 '14 at 12:07
  • @poojagupta why are you comment this **setContentView(R.layout.list_fruit);** tell me? – M D Feb 27 '14 at 12:20
  • @poojagupta and where you set adapter like: `setListAdapter(adapter);` – M D Feb 27 '14 at 12:22
  • @M D i am still facing that issue not agetting text for facebook while sharing but getting in gmail.Could you guide me what to do now – poojagupta Feb 27 '14 at 13:48