Q:

Contextual ActionBar copy text from custom ListView

I have a custom ListView adapter which contains 2 Textviews for each item , On long click on the item I want to have the option to copy the text from 1 of these Textviews to the clipboard, the question is how can i get this Textview's text?
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.copy:
                Toast.makeText(getActivity(),"Text copied to clipboard.", Toast.LENGTH_SHORT).show();
    //HOW CAN I GET THE TEXT?
                mode.finish();
                return true;
            case R.id.share:
                return false;
            default:
                return false;
        }
    }

A:

ListView list = (ListView) findViewById(R.id.yourList);    
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public void onItemLongClick(AdapterView<?> a, View v, int position,long id) {
                TextView yourFirstTextView = (TextView) v.findViewById(R.id.yourFirstTextViewId);
                TextView yourSecondTextView = (TextView) v.findViewById(R.id.yourSecondTextView);
                copyTextToClipboard(yourFirstTextView);//if you want to copy your first textview
                copyTextToClipboard(yourSecondTextView);//if you want to copy your second textview
});

public void copyTextToClipboard(TextView txtView){
     int sdk = android.os.Build.VERSION.SDK_INT;
     if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
         android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
         clipboard.setText(txtView.getText().toString());
     } else {
         android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
         android.content.ClipData clip = android.content.ClipData.newPlainText("text label",txtView.getText().toString());
         clipboard.setPrimaryClip(clip);
     }
}

I don't test this code but it may work.

