dimanche 28 décembre 2014

Android :bundle passing from two activities to another activity

I have mainly 4 activities in my app(Login,signup,buy_nw and buy_ltr).The Person wish to login should signup first.The details entered in signup page will saved to a sqLite database.I need to automatically fill all the details of member asked in buy_nw and buy_ltr directly from database on clicking login button.I tried using bundle passing.It not working properly.Please help me out.


Login.java



public class login extends Activity{
EditText edt1,edt2;
Button btn1,btn2;
SQLiteDatabase mydb;
CheckBox chkbx;
SharedPreferences loginPreferences;
SharedPreferences.Editor loginPrefsEditor;
Boolean saveLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btn1=(Button)findViewById(R.id.loginbtn);
btn2=(Button)findViewById(R.id.signupbtn);
edt1=(EditText)findViewById(R.id.useredit);
edt2=(EditText)findViewById(R.id.passedit);
chkbx=(CheckBox)findViewById(R.id.checkbox);
mydb=this.openOrCreateDatabase("shopping", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS contacts(name varchar,adrs varchar,city varchar,pin varchar,uname varchar,pass varchar,mob varchar,eid varchar)");


loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
edt1.setText(loginPreferences.getString("username", ""));
edt2.setText(loginPreferences.getString("password", ""));
chkbx.setChecked(true);
}

btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


// TODO Auto-generated method stub

String usr=edt1.getText().toString();
String pwd=edt2.getText().toString();
Cursor cur=mydb.rawQuery("select * from contacts where uname='"+usr+"'",null);

final String[] usrnme = new String[cur.getCount()];

int j = 0;
while(cur.moveToNext())
{
String user = cur.getString(cur.getColumnIndex("uname"));
usrnme[j] = user;
j++;
}

if (v==btn1) {
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt1.getWindowToken(),0);

if (chkbx.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin",true);
loginPrefsEditor.putString("username",usr);
loginPrefsEditor.putString("password",pwd);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
}
while (cur.moveToNext())
{
String pas=cur.getString(cur.getColumnIndex("pass"));

if (pwd.equals(pas))
{
String topas = usrnme[j];
Intent in=new Intent(getApplicationContext(),product_display.class);
Bundle bn = new Bundle();
bn.putString("key",topas);
in.putExtras(bn);
startActivity(in);
}
else
{

Toast.makeText(login.this,"invalid username or password",Toast.LENGTH_SHORT).show();
}

}

}
});
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(login.this,signup.class);
startActivity(in);

}
});
}


}


}


buy_nw.java



public class buy_nw extends Activity {
EditText edt1,edt2,edt3,edt4,edt5;
Button btn;
SQLiteDatabase mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_nw);
edt1=(EditText)findViewById(R.id.nameedit);
edt2=(EditText)findViewById(R.id.addrsedit);
edt3=(EditText)findViewById(R.id.cityedit);
edt4=(EditText)findViewById(R.id.pincodedit);
edt5=(EditText)findViewById(R.id.quantityedit);
btn=(Button)findViewById(R.id.cnfm_ordr);

Intent in=getIntent();
Bundle bn=in.getExtras();
String list=bn.getString("key");

mydb=this.openOrCreateDatabase("reg", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS contacts(name varchar,adrs varchar,city varchar,pin varchar,uname varchar,pass varchar,mob varchar,eid varchar)");

Cursor cur=mydb.rawQuery("SELECT * FROM contacts WHERE uname='"+list+"' ", null);
while (cur.moveToNext()) {



String nm=cur.getString(cur.getColumnIndex("name"));
edt1.setText(nm);
String ad=cur.getString(cur.getColumnIndex("adrs"));
edt2.setText(ad);
String cty=cur.getString(cur.getColumnIndex("city"));
edt3.setText(cty);
String pc=cur.getString(cur.getColumnIndex("pin"));
edt4.setText(pc);
String qty=cur.getString(cur.getColumnIndex("name"));
edt5.setText(qty);

}
btn.setOnClickListener(new OnClickListener() {

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

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"my@email.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();

}
});
}

}

Aucun commentaire:

Enregistrer un commentaire