how to get the declared value from other java file and use it to another java file? like I want to show the user's real name using the textView when he/she successfuly login, from the main activity to the welcome page. example "Welcome User". I have tried this but it's not working: Cursor cursor = dataBase.select("SELECT real_name FROM " + TABLE_NAME + " WHERE u_id = '" + COLUMN_NAME1 + "';");
please help
this the code of my homepage.java:
package com.example.howtos;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
//import android.database.Cursor;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class homepage extends Activity {
private TextView textView;
private DatabaseManager dataBase;
public static final String TABLE_NAME = "users";
public static final String COLUMN_NAME = "real_name";
public static final String COLUMN_NAME1 = "u_id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
//get the connection on the
dataBase = DatabaseManager.instance();
//get the id of the textview to show text
textView = (TextView) findViewById(R.id.textView2);
Button a = (Button) findViewById (R.id.next1);
a.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent(homepage.this,need.class));
}
});
Button out = (Button) findViewById (R.id.lo);
out.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent (homepage.this,MainActivity.class));
Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show();
}
});
updateTextView();
}
public void updateTextView() {
//get all the available values from the database
//Cursor cursor = dataBase.select("SELECT real_name FROM " + TABLE_NAME);
Cursor cursor = dataBase.select("SELECT real_name FROM " + TABLE_NAME + " WHERE u_id = '" + COLUMN_NAME1 + "';");
textView.setText("");
while(cursor.moveToNext()) {
//put the values fetched from the database to the textview
String s = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
textView.append(" " + s);
}
cursor.close();
//----->
}
}
and the MainActivity.java code:
package com.example.howtos;
import com.example.howtos.DatabaseManager;
import com.example.howtos.MainActivity;
import com.example.howtos.R;
import com.example.howtos.reg;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseManager dataBase;
dataBase = DatabaseManager.instance();
Button b = (Button) findViewById (R.id.btn12);
b.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try
{
final EditText username = (EditText) findViewById(R.id.editText1);
final EditText password = (EditText) findViewById(R.id.editText2);
final String COLUMN_PW = "password";
Cursor cursor = dataBase.select("SELECT password FROM users WHERE username = '" + username.getText ().toString() + "';");
cursor.moveToFirst();
String s = cursor.getString (cursor.getColumnIndex(COLUMN_PW));
if(password.getText().toString().equals(s))
//if(password.getText().toString() == s)
{
startActivity(new Intent(MainActivity.this,homepage.class));
Toast.makeText(getApplicationContext(), "Successfully Login", Toast.LENGTH_SHORT).show();
cursor.close();
}
else
{
Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show ();
}
}
catch(CursorIndexOutOfBoundsException e1)
{
Toast.makeText(getApplicationContext(), "Error accessing database", Toast.LENGTH_SHORT).show();
}
}
});
TextView r = (TextView) findViewById (R.id.textView7);
r.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent (MainActivity.this,reg.class));
}
});
}
}