2014-02-07 22 views
-1

它像有很多像類Login.java,Updatedetails.java,Result.java等我想打電話從一個Java類不同的Java類變量的Android

這裏是登錄的Java代碼。 java的

package com.example.catxam; 


import java.util.ArrayList; 
import java.util.List; 



import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 



import com.example.catxam.JSONParser; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.widget.EditText; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.TextView; 

public class Login extends Activity { 

private EditText inputUserid, inputPassword, server; 
TextView forgotPassword; 
private Button b1; 
public static String serve; 



// Progress Dialog 
    private ProgressDialog pDialog; 

// JSON parser class 
    JSONParser jsonParser = new JSONParser(); 

    // JSON Node names 
    private static final String resp = "success"; 
    private static final String Flag = "flag"; 


protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.login); 

    inputUserid = (EditText) findViewById(R.id.Username_edit); 
    inputPassword = (EditText) findViewById(R.id.User_password); 
    server = (EditText) findViewById(R.id.serverSelection); 

    forgotPassword = (TextView) findViewById(R.id.forgotPassword); 
    forgotPassword.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent passForget = new Intent(getApplicationContext(), 
        ForgotPassword.class); 
      startActivity(passForget); 
     } 
    }); 
    b1 = (Button) findViewById(R.id.loginbutton); // login button 


    b1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

       new CreateNewUser().execute(); 

      } 
    }); 

} 



    // this class is for the checking of the user login and password 
    //i.e. of first login and the next consecutive logins 
    class CreateNewUser extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 

    protected void shradu() { 
     //super.onPreExecute(); 
     pDialog = new ProgressDialog(Login.this); 
     pDialog.setMessage("Failed Login"); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 


    /** 
    * Checking creditenials 
    * */ 
    protected String doInBackground(String... args) { 

     String user = inputUserid.getText().toString(); 
     String pswrd = inputPassword.getText().toString(); 
     serve = server.getText().toString(); 

    if (serve.equals("web")) 
     serve = "glydenlewis.esy.es"; 
    else if (serve.equals("g")) 
     serve = "192.168.1.4"; 
    else if (serve.equals("r")) 
     serve = "192.168.0.100"; 
    else if (serve.equals("v")) 
     serve = "192.168.0.100"; 
    else 
     serve = "glydenlewis.esy.es"; 



     // URL to check username & password 
final String url_check_user = "http://" + serve + "/catxam/android_check.php"; 

// Building Parameters 
List<NameValuePair> params = new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("uname", user)); 
params.add(new BasicNameValuePair("psd", pswrd)); 


// getting JSON Object 
// Note that create product url accepts POST method 

// this try is to catch if server address is wrong 
JSONObject json = jsonParser.makeHttpRequest(url_check_user,"POST", params); 

// check log cat from response 
Log.d("Create Response", json.toString()); 

// check for success tag 
try { 
    int success = json.getInt(resp); 
    int flag_ck = json.getInt(Flag); 
    if (success == 1) { 
     if (flag_ck == 0) 
     { 
      //First Time Login By User 
      Intent i = new Intent(getApplicationContext(), UpdateDetails.class); 
      Toast.makeText(getApplicationContext(), "First Login, Please Update Your Details",Toast.LENGTH_SHORT).show(); 
      startActivity(i); 
      finish(); // closing this screen 
     } 
     else 
     { 
     // successfully login 
     Intent i = new Intent(getApplicationContext(), MainActivity.class); 

     startActivity(i); 
     finish(); // closing this screen 
     } 
    } else { 
     // successfully login 
        Intent i = new Intent(getApplicationContext(), LogoStart.class); 
        startActivity(i); 
        finish(); // closing this screen 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

return null; 

    } 

} 

}

現在

爲UpdateDetails.java

package com.example.catxam; 

import java.util.ArrayList; 
import java.util.List; 


    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.app.Activity; 
    import android.app.ProgressDialog; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.Toast; 
    import android.view.Window; 
    import android.view.WindowManager; 

public class UpdateDetails extends Activity { 

     EditText inputName; 
     EditText inputAddress; 
     EditText inputPassword; 
     EditText confirmPassword; 
     EditText inputEmailId, inputMobileno; 
     // Progress Dialog 
     private ProgressDialog pDialog; 

    // JSON parser class 
     JSONParser jsonParser = new JSONParser(); 

     // JSON Node names 
     private static final String resp = "success"; 
     //private static final String Flag = "flag"; 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.updatedetails); 

     // Edit Text 
     inputName = (EditText) findViewById(R.id.editName); 
     inputAddress = (EditText) findViewById(R.id.editAddress); 
     inputPassword = (EditText) findViewById(R.id.editPassword); 
     confirmPassword=(EditText) findViewById(R.id.editConfirmPassword); 
     inputEmailId=(EditText) findViewById(R.id.editText2_emailid); 
     inputMobileno=(EditText) findViewById(R.id.editText1_mobile); 

     // Create button 
     Button update_details = (Button) findViewById(R.id.buttonUpdate); 

     // button click event 
     update_details.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       if (inputPassword == confirmPassword){ 
       // updating user in background thread 
       new UpdateUserDetails().execute(); 
       } 
       else { 
        //Some Sort Of Alert Box 
        Toast.makeText(getApplicationContext(), "Please Enter Valid Details", Toast.LENGTH_SHORT).show(); 

       } 
      } 
     }); 
    } 

    /** 
    * Background Async Task to Create new product 
    * */ 
    class UpdateUserDetails extends AsyncTask<String, String, String> { 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(UpdateDetails.this); 
      pDialog.setMessage("Updating User Details.. Please wait"); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * Updating User 
     * */ 
     protected String doInBackground(String... args) { 
      String name = inputName.getText().toString(); 
      String address = inputAddress.getText().toString(); 
      String password = inputPassword.getText().toString(); 
      String mobileno = inputMobileno.getText().toString(); 
      String emailid = inputEmailId.getText().toString(); 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("name", name)); 
      params.add(new BasicNameValuePair("address", address)); 
      params.add(new BasicNameValuePair("password", password)); 
      params.add(new BasicNameValuePair("mobile_no", mobileno)); 
      params.add(new BasicNameValuePair("email_id",emailid)); 

      final String url_user = "http://"+ Login.serve +"/catxam/android_update.php"; 

      // getting JSON Object 
      JSONObject json = jsonParser.makeHttpRequest(url_user,"POST", params); 

      // check log cat from response 
      Log.d("Update Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(resp); 

       if (success == 1) { 
        // successfully update user 
        Intent i = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(i); 

        // closing this screen 
        finish(); 
       } else { 
        Toast.makeText(getApplicationContext(), "Unsuccessful Updation", Toast.LENGTH_SHORT).show(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
     } 

    } 
} 

現在我的問題1 on是因爲我已經聲明所有服務器選項只在login.java中,可以由其他類作爲Login.serve訪問。

,所以我只是想簡單和保存所有的地址GlobalVariableCall.java。然後這些地址將被不同的類訪問。

回答

0

可以直通一個活動變量到其他的意圖方法至極很簡單...

Intent myIntent = new Intent(firstActivity.this, secondActivity.class); 
        myIntent.putExtra("data", data); //Optional parameters or data to be send to the next activity 
        firstActivity.this.startActivity(myIntent); 
在這種情況下

您在firstActivity聲明全局變量...你可以putExtras只要你想這些都是您要發送到下一個活動

,或者你可以實例化的全局變量類,但路過的時候它通一類到另一個你就失去了變量的值變量...

0

我要讓稱爲GlobalVariableCall我在哪裏 聲明幾個變量的單一類如公共靜態斯汀服務器。

所以,你必須聲明:

class GlobalVariableCall 
{ 
    public static String server="http://stackoverflow.com/questions/21634112/i-want-to-call-variables-from-one-java-class-to-different-java-class-in-android"; 
} 

那麼您還可以從其他活動這個服務器變量說Login.java這樣的:

OnCreate() 
{ 
    ............ 
.............. 

    String abc = GlobalVariableCall.server; // In this way abc will get the value of server. 
} 
+0

我的問題,仍然是相同的。它只是我有多個服務器地址。像 –

+0

我只是不明白你什麼你真的想要嗎? –

+0

其實Login.java擁有的EditText它需要用戶輸入的服務器地址。爲了使這個全球可訪問我聲明作爲公共靜態服務GlobalVariableCall服務。 但問題是,如果有什麼有太多的服務器上運行。所以我想讓這部分代碼全球化,以便每個服務器地址都可以被訪問。 如果(serve.equals( 「網絡」)) \t \t \t服務= 「glydenlewis.esy.es」; \t \t否則如果(serve.equals( 「G」)) \t \t \t服務= 「192.168.1.4」; \t \t否則如果(serve.equals( 「R」)) \t \t \t服務= 「192.168.0.100」; \t \t else if(serve.equals(「v」)) \t \t \t serve =「192.168.0。100" ; \t \t其他 \t \t \t服務=‘glydenlewis.esy.es’; –

相關問題