preforming class after button click

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

preforming class after button click

Postby Rolf Smit » Thu Dec 30, 2010 4:32 pm

Oke guyz,

i made a xml parser thanks to this forum, but i have a new problem.



it makes 3 spinners and after that it reads the values out of it and prints the right text. it all happens when the app starts up. but now i want to add a button and when you press the button this all happens. so i tried to make a class into my code and the class holds the xml parser and other stuf. In the public void oncreate i want to make the button code that calls the xml class. but when i make this my Strings wont work any more and i have to add final, but final means that you can't edit the String. so how can i get this working?



this is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.neotech.data_file_based;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.xmlpull.v1.XmlPullParserException;
  6.  
  7. import android.app.Activity;
  8. import android.content.res.XmlResourceParser;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Spinner;
  13. import android.widget.TextView;
  14.  
  15.  
  16.  
  17. public class data_file_based extends Activity {
  18.  
  19.         /** Called when the activity is first created. */
  20.  
  21.        
  22.         final static String TAG = "CompXML";
  23.         String boek = "";
  24.         String text = "";
  25.         int hoofdstuk = 0;
  26.         int vers = 0;
  27.        
  28.        
  29.        
  30.        
  31.        
  32.     @Override
  33.     public void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.main);
  36.  
  37.  
  38.         Spinner spinner_boeken = (Spinner) findViewById(R.id.spinner_boeken);
  39.         ArrayAdapter adapter_boeken = ArrayAdapter.createFromResource(
  40.                 this, R.array.boeken, android.R.layout.simple_spinner_item);
  41.         adapter_boeken.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  42.         spinner_boeken.setAdapter(adapter_boeken);
  43.  
  44.         Spinner spinner_hoofdstukken = (Spinner) findViewById(R.id.spinner_hoofdstukken);
  45.         ArrayAdapter adapter_hoofdstukken = ArrayAdapter.createFromResource(
  46.                 this, R.array.hoofdstukken, android.R.layout.simple_spinner_item);
  47.         adapter_hoofdstukken.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  48.         spinner_hoofdstukken.setAdapter(adapter_hoofdstukken);
  49.  
  50.         Spinner spinner_versen = (Spinner) findViewById(R.id.spinner_versen);
  51.         ArrayAdapter adapter_versen = ArrayAdapter.createFromResource(
  52.                 this, R.array.versen, android.R.layout.simple_spinner_item);
  53.         adapter_versen.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  54.         spinner_versen.setAdapter(adapter_versen);
  55.        
  56.  
  57.         TextView tv = (TextView) findViewById(R.id.generated_text);
  58.         tv.setText("");
  59.    
  60.    
  61.        
  62.                
  63.                  String s_zoek_boek = spinner_bijbelboeken.getSelectedItem().toString();
  64.              String s_zoek_hoofdstuk = spinner_hoofdstukken.getSelectedItem().toString();
  65.              String s_zoek_vers = spinner_versen.getSelectedItem().toString();;
  66.              int i_zoek_hoofdstuk = Integer.parseInt(s_zoek_hoofdstuk);
  67.              int i_zoek_vers = Integer.parseInt(s_zoek_vers);
  68.                
  69.  
  70.             try
  71.             {
  72.  
  73.                 XmlResourceParser xrp = this.getResources().getXml(R.xml.data);
  74.  
  75.                 while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
  76.                        
  77.  
  78.                         if (xrp.getEventType() == XmlResourceParser.START_TAG) {
  79.  
  80.                                         String s = xrp.getName();
  81.                        
  82.                                         if (s.equals("boek")) {
  83.                                                 boek = xrp.getAttributeValue(null, "name");
  84.                                                 int i = xrp.getAttributeIntValue(null, "boek", 0);
  85.                          
  86.                                         } else if (s.equals("hoofdstuk")) {
  87.                                                 hoofdstuk = xrp.getAttributeIntValue(null, "name", 0);
  88.                          
  89.                                         } else if (s.equals("vers")) {
  90.                                                 vers = xrp.getAttributeIntValue(null, "name", 0);
  91.                                         }
  92.  
  93.                                        
  94.                         } else if (xrp.getEventType() == XmlResourceParser.END_TAG) {
  95.  
  96.                             String s = xrp.getName();
  97.                            
  98.                             if(s.equals("vers")){
  99.                                 //set text
  100.                                 if(vers == i_zoek_vers && hoofdstuk == i_zoek_hoofdstuk && boek.equals(s_zoek_boek)){
  101.                                 tv.append(boek + " " + hoofdstuk + ":" + vers + "\n" + text + "\n\n");
  102.                                 }
  103.                             }
  104.                                        
  105.                     } else if (xrp.getEventType() == XmlResourceParser.TEXT) {
  106.                                 text = xrp.getText();
  107.                     }
  108.                     xrp.next();    
  109.                 }
  110.                  //einde while
  111.                     xrp.close();
  112.  
  113.  
  114.             //try fails
  115.             } catch (XmlPullParserException xppe) {
  116.                     Log.e(TAG, "Failure of .getEventType or .next, probably bad file format");
  117.                     xppe.toString();
  118.             } catch (IOException ioe) {
  119.                     Log.e(TAG, "Unable to read resource file");
  120.                     ioe.printStackTrace();
  121.             }
  122.        
  123.                
  124. }
  125. }
  126.  
  127.  
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
Rolf Smit
Freshman
Freshman
 
Posts: 9
Joined: Sun Dec 26, 2010 1:17 am

Top

Re: preforming class after button click

Postby hatsuharu333 » Wed Jul 20, 2011 1:49 am

I'm not sure if I understand your question 100%, but what I would do is create a completely seperate class which extends activity, and in that class, you have your button declaration.

Maybe a concrete example will help. Say you have a java class menu, and a java class action. The menu has the button that will start action. So what you do is you have to have a different xml for each.

Code: Select all
public class MainMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
    Button actionButton = (Button)findViewById(R.id.button1);
        actionButton.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
              startActivity(new Intent("<packagename>.ACTION")); // replace package name with your package name
           }
        });


then in the action class yuo have everything you already have.

(in main.xml)
in the xml for main, you have your button named button1 -
Code: Select all
    <Button
         android:id = "@+id/button1"
         android:layout_height = "wrap_content"
         android:layout_width = "wrap_content"
         android:background = "@drawable/directory_button"
         />    


and then make sure you declare the intent in the manifest by creating a new activity

Code: Select all
        <activity android:name=".Action" //replace this with .<name of your java class>
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">>
            <intent-filter>
                <action android:name= "<packagename>.ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


hope that helps somewhat!
hatsuharu333
Freshman
Freshman
 
Posts: 7
Joined: Mon May 16, 2011 6:50 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests