returning data from a dialog to the main Activity

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

returning data from a dialog to the main Activity

Postby Lenea » Sat Jan 02, 2010 2:25 am

so i have a dialog called up when a button is pressed. Inside the dialog there is a EditText and my goal is, changing a TextView that's in the initial Activity to what the user inputs in the EditText from the Dialog.

here's my java file :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. case R.id.PlayerOneName:
  3.  
  4.                        
  5.  
  6.                        
  7.  
  8.                         AlertDialog.Builder builder;
  9.  
  10.                         AlertDialog alertDialog;
  11.  
  12.  
  13.  
  14.                        
  15.  
  16.                         LayoutInflater inflater = (LayoutInflater) LifeCounterActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
  17.  
  18.                         View layout = inflater.inflate(R.layout.player_name_dialog, null);
  19.  
  20.  
  21.  
  22.                        
  23.  
  24.                        
  25.  
  26.  
  27.  
  28.                         builder = new AlertDialog.Builder(LifeCounterActivity.this);
  29.  
  30.                         builder.setView(layout);
  31.  
  32.                         builder.setCancelable(false);
  33.  
  34.                         builder.setTitle("Player One Name:");
  35.  
  36.                         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  37.  
  38.                         EditText editPlayerNames = (EditText)findViewById(R.id.editPlayerNames);
  39.  
  40.                        
  41.  
  42.                                
  43.  
  44.                                 public void onClick(DialogInterface alertDialog, int v) {
  45.  
  46.                                         // TODO Auto-generated method stub
  47.  
  48.                                        
  49.  
  50.                                 //Returning the EditText data should be inserted here...       
  51.  
  52.  
  53.  
  54. alertDialog.dismiss();
  55.  
  56.                                        
  57.  
  58.                                        
  59.  
  60.                                        
  61.  
  62.                                 }
  63.  
  64.                         });
  65.  
  66.                        
  67.  
  68.                        
  69.  
  70.                        
  71.  
  72.                         alertDialog = builder.create();
  73.  
  74.                         alertDialog.show();
  75.  
  76.                        
  77.  
  78.                        
  79.  
  80.                        
  81.  
  82.                
  83.  
  84.                        
  85.  
  86.                        
  87.  
  88.                    
  89.  
  90.                                
  91.  
  92.                        
  93.  
  94.                  
  95.  
  96.                        
  97.  
  98.                        
  99.  
  100.                         break;
  101.  
  102.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


and my Xml defining the dialog is as follows

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <LinearLayout android:layout_width="fill_parent"
  3.  
  4.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:id="@+id/root_view">
  7.  
  8.         <EditText android:layout_width="wrap_content"
  9.  
  10.                 android:layout_height="wrap_content" android:padding="20dp"
  11.  
  12.                 android:id="@+id/editPlayerNames" android:hint="Player One Name"></EditText>
  13.  
  14.        
  15.  
  16. </LinearLayout>
  17.  
  18.  
  19.  
  20.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



Sorry if the questions seem noobish the i'm just starting out.

Cheers,
Lenea
Lenea
Freshman
Freshman
 
Posts: 6
Joined: Mon Dec 28, 2009 12:49 am

Top

Postby jwei512 » Sat Jan 02, 2010 7:30 pm

Couldn't you just declare your TextView as a global variable, and then in your dialog's OnClickListener do something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.  
  3. public void onClick(DialogInterface alertDialog, int v) {
  4.  
  5.       EditText editPlayerNames = (EditText)findViewById(R.id.editPlayerNames);
  6.  
  7.  
  8.  
  9.       String text = editPlayerNames.getText().toString();
  10.  
  11.  
  12.  
  13.       // yourTextView is a global variable defined in your activity
  14.  
  15.       yourTextView.setText(text);
  16.  
  17.  
  18.  
  19.       alertDialog.dismiss();
  20.  
  21. }
  22.  
  23. });
  24.  
  25.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests