title: setting the textview on item list select
body: <p>I am trying to use a dialog to display a list of strings.... then on click of any of the item, I need to set the text in the activity</p>
  
  <p><strong><code>What have i tried ?</code></strong></p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void popUp(View v){
  
          //declare the collection to add people into them
          ArrayList&lt;String&gt; NamesOfPeople=new ArrayList&lt;String&gt;();
          NamesOfPeople.add("USA");//add person
          NamesOfPeople.add("CANADA");//add person
          NamesOfPeople.add("RUSSIA");//add person
  
          // set the dialog to be displayed on the present activity ... so context is MainActivity.this
          final Dialog dialog=new Dialog(MainActivity.this);
          dialog.setContentView(R.layout.list_of_names);//set the liststructure
          dialog.setTitle("List Of People");// Set the title of the dialog
  
          //find the listview using id ...remember listview is on dialog now so
          ListView LV=(ListView) dialog.findViewById(R.id.listView);
  
          // Now i need  to put the data from the collection to the listview
          // We shall use an arrayadapter for this
          ArrayAdapter&lt;String&gt; adapter=new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_list_item_1, NamesOfPeople);
          //we always set the adapter using the listview
          LV.setAdapter(adapter);
  
          //Dont forget to show the dialog
          dialog.show();
  
          LV.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                      long arg3) {
                  // TODO Auto-generated method stub
  
                  //dialog.dismiss();// used to cancel the dialog on click of it
                  TextView TV=(TextView) dialog.findViewById(R.id.textView);
  
                  //TV.setText(arg1);
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="154dp"
          android:text="SelectString" 
          android:onClick="popUp"/&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button1"
          android:layout_alignParentTop="true"
          android:layout_marginTop="72dp"
          android:text="NAME"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>list_of_names.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;ListView
          android:id="@+id/listView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Where i am stuck</strong>::</p>
  
  <p><em>I am able to show the pop-up dialog, but how to set the text on select of one of the strings from the list</em></p>
  
  <hr>
  
  <h2><strong>{Edit}</strong></h2>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void popUp(View v){
  
          //declare the collection to add people into them
          final ArrayList&lt;String&gt; NamesOfPeople=new ArrayList&lt;String&gt;();
          NamesOfPeople.add("USA");//add person
          NamesOfPeople.add("CANADA");//add person
          NamesOfPeople.add("RUSSIA");//add person
  
          // set the dialog to be displayed on the present activity ... so context is MainActivity.this
          final Dialog dialog=new Dialog(MainActivity.this);
          dialog.setContentView(R.layout.list_of_names);//set the liststructure
          dialog.setTitle("List Of People");// Set the title of the dialog
  
          //find the listview using id ...remember listview is on dialog now so
          ListView LV=(ListView) dialog.findViewById(R.id.listView);
  
          // Now i need  to put the data from the collection to the listview
          // We shall use an arrayadapter for this
          ArrayAdapter&lt;String&gt; adapter=new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_list_item_1, NamesOfPeople);
          //we always set the adapter using the listview
          LV.setAdapter(adapter);
  
          //Dont forget to show the dialog
          dialog.show();
  
          LV.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                      long arg3) {
                  // TODO Auto-generated method stub
  
                  //dialog.dismiss();// used to cancel the dialog on click of it
                  TextView TV=(TextView) dialog.findViewById(R.id.textView);
  
                  TV.setText(NamesOfPeople.get(arg2).toString());
              }
          });
      }
  
  
  
  
  }
  </code></pre>
  
  <p><strong>log</strong></p>
  
  <pre><code>12-18 16:41:53.757: E/AndroidRuntime(902): Uncaught handler: thread main exiting due to uncaught exception
  12-18 16:41:53.816: E/AndroidRuntime(902): java.lang.NullPointerException
  12-18 16:41:53.816: E/AndroidRuntime(902):  at com.example.popupofstrings.MainActivity$1.onItemClick(MainActivity.java:59)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.widget.ListView.performItemClick(ListView.java:3285)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.os.Handler.handleCallback(Handler.java:587)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.os.Handler.dispatchMessage(Handler.java:92)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.os.Looper.loop(Looper.java:123)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at android.app.ActivityThread.main(ActivityThread.java:4363)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at java.lang.reflect.Method.invokeNative(Native Method)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at java.lang.reflect.Method.invoke(Method.java:521)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  12-18 16:41:53.816: E/AndroidRuntime(902):  at dalvik.system.NativeStart.main(Native Method)
  12-18 16:41:53.836: I/dalvikvm(902): threadid=7: reacting to signal 3
  12-18 16:41:53.836: E/dalvikvm(902): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
  </code></pre>