Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 31st, 2012, 09:54 PM   #1 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default declare global variable

Hi Good Morning,

How is declare global variable in android...some link send me...am searched various post..but am not clear about dis concept...send me valuable link..

krishnaveni is offline  
Reply With Quote
Sponsors
Old January 31st, 2012, 10:26 PM   #2 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Because of java's true OOP nature, there are no true global varibales. The best you can do is create static variables that can be referenced w/o an instance and whose values are preserved for all occurrences of the class.

For instance you might have something like this:

Code:
public class MyClass
{
    public static int myStaticIntVariable = 0;
    //...More code here...
}
Then, to access this data member, you can do something like:

Code:
//in another class somewhere
int before = MyClass.myStaticIntVariable;
++MyClass.myStaticIntVariable;
int after = MyClass.myStaticIntVariable;
In the above example, assuming that the static data member has not been previously altered, before = 0 and after = 1.
}
jonbonazza is online now  
Reply With Quote
Old February 1st, 2012, 12:56 AM   #3 (permalink)
Junior Member
 
Join Date: Dec 2011
Location: chennai
Posts: 61
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi..
am developed one login form..dis is my coding part:
LoginLayoutActivity.java
package com.example.login;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class LoginLayoutActivity extends Activity {

EditText un,pw;
TextView error;
Button ok;
protected static String username;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(EditText)findViewById(R.id.et_un);
pw=(EditText)findViewById(R.id.et_pw);
ok=(Button)findViewById(R.id.btn_login);
error=(TextView)findViewById(R.id.tv_error);

ok.setOnClickListener(new View.OnClickListener() {



@Override
public void onClick(View v) {
// TODO Auto-generated method stub

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

String response = null;
try {

response = CustomHttpClient.executeHttpPost("xxx/login3.php", postParameters);
String res=response.toString();
res= res.replaceAll("\\s+","");

if(res.equals("1"))
{

error.setText("Correct Username or Password");
Intent i = new Intent(getApplicationContext(),registerActivity.cl ass);
startActivity(i);


}
else
{
error.setText("Sorry!! Incorrect Username or Password");
}
} catch (Exception e) {
un.setText(e.toString());
}

}
});
}
}

registerActivity.java:
package com.example.login;

import java.io.BufferedReader;



import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;


public class registerActivity extends Activity {
/** Called when the activity is first created. */

TextView txt;
JSONObject json;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);

// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));



}
public static final String KEY_121 = "xxx/lo.php"; //i use my real ip here



private String getServerData(String returnString) {

InputStream is = null;

String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usertype","A"));

//http post
try{


HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.i("log_tag","Line reads: " + line);

}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);



for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag",
"firstname: "+json_data.getString("firstname")+
"lastname: "+json_data.getString("lastname")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);

}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}

}

login3.php:

<?php
// store session data
$un=$_POST['login'];
$pw=$_POST['password'];
//connect to the db
$user = 'root';
$pswd = '';
$db = 'xxx';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = "SELECT firstname,lastname FROM customers WHERE login = '$un' AND password = '$pw' OR usertype ='A'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
{

echo 1;
}
else
{
echo 0;

}
?>

lo.php:

<?php


$r=mysql_connect("localhost","root","");
mysql_select_db("xxx",$r);

$q=mysql_query("SELECT firstname,lastname FROM xcart_customers WHERE (usertype ='A')");

while($e=mysql_fetch_assoc($q))
{
$row[]=$e;
}
print(json_encode($row));



mysql_close()
?>

here lo.php am set usertype=A ...bez all admin user r logged in login page...that means admin users having permission... so here all admin user firstname and lastname displayed...but i need which admin person loged in login page that particular person detail only displayed...so plz give me some solutions...(i.e)krishnaveni,kannan,hariprasad r admin user means when krishnaveni loged means krishnaveni detail ly displayed...kannan logged means kannan details only displayed..i dono how it is do???? plz give me solutions....thanks in advance...
here easy to save the logged in username in a global variable .Then get it in the next activity and query again the database with this username..but i don't know how is to do...i checked these links for setting the global variable and for passing the extras in the intent.
but how is to do in my coding...plz help me...
krishnaveni is offline  
Reply With Quote
Old February 2nd, 2012, 02:44 PM   #4 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Please put your code inside of CODE tags. Otherwise, it is too annoying to read.
jonbonazza is online now  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:11 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo