2012年8月18日 星期六

Android chat room copy/delete text

// long press click
@Override
public boolean onItemLongClick(AdapterView arg0, View arg1,
     final int pos, long id) {

                // AlertDialog can try to move to global variable, can avoid to multiple creating.
                AlertDialog.Builder dialog = new AlertDialog.Builder(ChatActivity.this);
                dialog.setTitle(getResources().getString(R.string.selection));
                
                String strCopy = getResources().getString(R.string.copy);
                final String strDel = getResources().getString(R.string.delete);
                
                String[] options = { strCopy, strDel };  // copy and delete items
                
                DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      switch (which) {
                      case 0: // Copy
                       String copyTxt = _list.get( pos ).getText();
                       
                       //place your TextView's text in clipboard
                       ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
                       clipboard.setText(copyTxt);
                       
                        break;
                        
                      case 1: // Delete
                       AlertDialog.Builder delete = new AlertDialog.Builder(ChatRoomActivity.this);
                       delete.setTitle( strDel );
                       
                          // if confirm to del item, remove it from list and update adapter
                       delete.setPositiveButton( R.string.confirm,  new DialogInterface.OnClickListener() {  
                              public void onClick(DialogInterface dialog, int whichButton) {  
                               _list.remove( pos );
                               _chatList.setAdapter(_adapter);  
                              }  
                          });  
                       
                       delete.setNeutralButton( R.string.cancel, null );
                       delete.show(); 
                      
                        break;
                      
                      default:
                       Toast.makeText( ChatActivity.this, "undefine this type in ChatActivity", Toast.LENGTH_LONG ).show();
                        break;
                      }
                    }
                  };
                  
                dialog.setItems(options, actionListener);
                
                // end AlertDialog create
                  
                dialog.show();

       return true;
         
  }

沒有留言:

張貼留言