Problems with OnClickListener when switching views

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

Problems with OnClickListener when switching views

Postby pslloo » Wed Jun 17, 2009 10:50 am

Can anyone help on this problem? Could be due to my lack of understanding. I have 2 views both with some buttons and with OnClickListener activated. From the first view ( setContentView(R.layout.main_image) ) the user clicks the button createNewDbButton and the second view ( setContentView(R.layout.create_db)) is displayed. When the user clicks cancelButton from the second view I want to return to the first view ( setContentView(R.layout.main_image) ). The first view seems to be displayed correctly but when I click the createDbButton on the first view nothing seems to happen. Why have I lost the OnClickListener? Do I have to set the listener again? Won't it work by just calling setContentView(R.layout.main_image)?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle savedInstanceState) {
  2.  
  3.       setContentView(R.layout.main_image);
  4.  
  5.       createNewDbButton = (Button)findViewById(R.id.createNewDbButton);
  6.  
  7.       createNewDbButton.setOnClickListener(new Button.OnClickListener() {
  8.  
  9.             public void onClick (View v) {
  10.  
  11.                         createNewDatabase();
  12.  
  13.             }
  14.  
  15.        });
  16.  
  17. }
  18.  
  19.  
  20.  
  21. public void createNewDatabase () {
  22.  
  23.         setContentView(R.layout.create_db);
  24.  
  25.         final Button cancelButton = (Button)findViewById(R.id.cancelButton);
  26.  
  27.         cancelButton.setOnClickListener(new Button.OnClickListener() {
  28.  
  29.                 public void onClick (View v) {
  30.  
  31.                         setContentView(R.layout.main_image);
  32.  
  33.                 }
  34.  
  35.         });
  36.  
  37. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4




Thanks
Peter
pslloo
pslloo
Freshman
Freshman
 
Posts: 4
Joined: Tue Jun 09, 2009 10:56 am

Top

Postby virtual » Wed Jun 17, 2009 11:09 am

Just create two diffrent activity for each view. say A and B;

from Activity A use something like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent(this, B.class);
  2.  
  3. startActivityForResult(intent, 1); // this will take u to second view i.e B .
  4.  
  5.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

in the second view just call finish() , in your case in buttons onClick() method. This will take you back to the view A.

Enjoy.
Regards,
Virtual
virtual
Developer
Developer
 
Posts: 26
Joined: Tue Mar 24, 2009 11:20 am

Postby pslloo » Fri Jun 19, 2009 10:15 am

Thanks Virtual. Your solution works very well.



Regards
pslloo
pslloo
pslloo
Freshman
Freshman
 
Posts: 4
Joined: Tue Jun 09, 2009 10:56 am

Postby ansh.chauhan » Fri Jun 19, 2009 12:43 pm

you can also do this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ActivtyClassName activity;
  3.  
  4.    public void onCreate(Bundle savedInstanceState) {
  5.  
  6.         super.onCreate(savedInstanceState);
  7.  
  8.         activity=this;
  9.  
  10.  
  11.  
  12.         setContentView(new SampleView(this));
  13.  
  14.     }
  15.  
  16.  
  17.  
  18. public boolean onKeyDown(int keyCode, KeyEvent event) {
  19.  
  20.  
  21.  
  22.                 activity.setContentView(view2);
  23.  
  24.        
  25.  
  26.         }
  27.  
  28.  
  29.  
  30.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


where view2 is other view
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests