0

I want to keep the value which is entered in the Edit text by the users. then I want to use that entered value in a different class.basically i want the user to enter their username in this class

public class Loginpage extends Activity implements OnClickListener{

    EditText usern,passw,dis;
    Button ulogin;
    String k;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage);
        usern=(EditText) findViewById(R.id.usern);
        passw=(EditText) findViewById(R.id.passw);
        dis=(EditText) findViewById(R.id.dis);
        ulogin=(Button) findViewById(R.id.ulogin);
        ulogin.setOnClickListener(this);
        k=usern.getText().toString();
    }
    @Override
    public void onClick(View p) {
        // TODO Auto-generated method stub
        switch(p.getId()){
        case R.id.ulogin:
            k=usern.getText().toString();
            String passwd=passw.getText().toString();
            Process cat = new Process(this);
            cat.open();
            String returnpass = cat.getpass(k);
            cat.close();
            if(passwd.equals(returnpass))
            {
                Intent i=new Intent("com.example.billpay.USEROPTION");
                startActivity(i);
                Dialog r=new Dialog(this);
                r.setTitle("Login Successfull");
                TextView wg= new TextView(this);
                wg.setText("Success");
                r.setContentView(wg);
                r.show();

            }
            else
            {
                Dialog r=new Dialog(this);
                r.setTitle("Wrong Username or Password");
                TextView po= new TextView(this);
                po.setText("Failure");
                r.setContentView(po);
                r.show();
            }


        }

    }


}

then i want to use their value which they have entered in the edit text in another class which is this.I want to display their username in this page.

 public class Userprofile extends Loginpage {


    TextView pra;
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userprofile);
        pra=(TextView)findViewById(R.id.pra);
        EditText p=usern;
        String op=p.getText().toString();
        pra.setText(k);

    }

}

but i am not being able to display their value

Fustigador
  • 6,339
  • 12
  • 59
  • 115
ronal007
  • 1
  • 3
  • 1
    Inheritance is not needed here, what you want is just pass a value.... http://stackoverflow.com/q/2091465/2001247 – ElDuderino Apr 24 '14 at 11:28

3 Answers3

0

simply use -Shared Preference

to store value and retrive value in other class

Dhwanik Gandhi
  • 685
  • 6
  • 12
0

Please use -Shared Preference:

here an easy example how to do it:

link

Community
  • 1
  • 1
SacreDeveloper
  • 1,253
  • 1
  • 9
  • 16
0

In the first class:

Context c=getApplicationContext();
String username;

Once you have your String with your username:

Intent intent=new Intent(c, YourSecondClass.class);
intent.putExtra("username", username);
startActivity(intent);

In your second class:

String username;
Bundle bundle=new Bundle();
bundle=getIntent.getExtras();
username=bundle.getString("username");

EDIT:

In case the second class is not called directly from the frist, you should use SharedPreferences:

First, create a folder called "shared_prefs" in the root of your project. Inside that folder, create a xml file, with the name you want. Inside that xml:

<map>
<string name="mail">user@mail.com</string>
<tring name="other">othervariable</tring>
</map>

In code, to reach that variable, you do this:

public class MainActivity extends Activity {

TextView mail;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    c=getApplicationContext();

    //Putting values in preferences.xml
    SharedPreferences prefs =
             getSharedPreferences("preferences",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();     
    editor.putString("mail", "correo@mail.com");
    editor.commit();

            //Getting things from preferences.xml
    String correo=prefs.getString("mail", null);
    mail=(TextView)findViewById(R.id.mail);
    mail.setText(correo);

}



}
Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • what is Yoursecondclass.class – ronal007 Apr 24 '14 at 12:04
  • I thought you called the second class, where you need the username, directly from frist class. In that case, Bundle is simpler. In your case, you need to put a variable reachable to every class of your app, you must use SharedPreferences. Ill edit the answer with a simple how-to. – Fustigador Apr 24 '14 at 12:10
  • what code i should write in my first class from where i need to store my username and what code should i write where i need to retrieve that stored username – ronal007 Apr 24 '14 at 12:30
  • I think you shold start by reading something if you need to ask that... You want to store a value in preferences.xml, so if you have a String named "username", should be editor.putString("thenameofthetaginthexmlfile", username); – Fustigador Apr 24 '14 at 12:33