2

I have done coding for selected multiple listview items to be shared but the problem is when i select the multiple items it shares only one item, the remaining items are not selected.Please help me how to share the multiple listview items .

public class MainActivity extends AppCompatActivity {

TextView textView;
ListView listView;
List<String> myList;
String MEDIA_PATHS;
Uri uri;

int count = 0;
ArrayList<String> arrayList2 = new ArrayList<>();

String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/CallLogs" ); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.list_items);
    textView = (TextView)findViewById(R.id.textView);

    myList = new ArrayList<String>();

    final File files = new File(MEDIA_PATH);

    File list[] = files.listFiles();
    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }

    final ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_layout,R.id.textView,myList);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {

            count = count+1;
            actionMode.setTitle(count + " items selected");
            MEDIA_PATHS = new String(Environment.getExternalStorageDirectory() + "/CallLogs/"  + myList.get(position));
        //    arrayList2.add(myList.get(position));
            arrayList2.add(MEDIA_PATH);

        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.list_menu,menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {

            switch (menuItem.getItemId())
            {

                case R.id.delete_id:

                    for(String msg : arrayList2) {
                        adapter.remove(msg);

                    }
                    Toast.makeText(getApplicationContext(),"deleted",Toast.LENGTH_SHORT).show();
                    count=0;
                    actionMode.finish();
                    return true;
                //  break;

                case R.id.share_id:

                    for(String msg : arrayList2) {
                        uri = Uri.parse(msg);
                    }
              //      Uri uri = Uri.parse(MEDIA_PATHS);

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.putExtra(Intent.EXTRA_STREAM, uri);
                    share.setType("audio/*");

                    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(share, "Share audio File"));

                    return true;

                default:
                    Toast.makeText(getApplicationContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
                    break;
            }

            return false;
        }


        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

          //  myList.get(position);
            adapterView.setSelected(true);
        }
    });

}

}

Shubham
  • 165
  • 2
  • 11
Prabha Karan
  • 1,309
  • 3
  • 17
  • 35

1 Answers1

0

Please refer this link for more information about sharing the files.I tried it,works for me.

https://developer.android.com/training/sharing/send.html

Prabha Karan
  • 1,309
  • 3
  • 17
  • 35