-3

i m using two activities which has category_id=1 and category_id=2.i used two intent with different objects.i sent the category id in that intents to the third activity and check the category id there using if else in onClick.But its not checking properly.if i go from first to third activity i want to display toast of "1" and from second to third activity ,i want to display toast of "2".But i am getting "null" as toast for first to third activity. and and i m getting "2" correctly for second to third activity. I am new to android,doing my first project.help me to solve this..today is the last day for project...

PopBatteryList activity

public class PopBatteryList extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        private ProgressDialog dialog;
      //tis s first activity file
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.anim_popup);
    inilizeToolbar();
    getValusesFromIntent();
}

private void getValusesFromIntent() {
            Button button = (Button) findViewById(R.id.bt_get_quote);
            YoYo.with(Techniques.SlideInRight).duration(2000).playOn(button);
            button.setVisibility(View.VISIBLE);
            final String caty="1";
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                Intent int1=new Intent(PopBatteryList.this,Getquote.class);
                    int1.putExtra("battery_featuer_idc",bf_if);
                    int1.putExtra("category_id",caty);
                   int1.putExtra("batterynamee",btry);
                    startActivity(int1);
                    }
            });  } 

Pop_other_battery activity

public class Pop_other_battery extends AppCompatActivity implement NavigationView.OnNavigationItemSelectedListener{
                private ProgressDialog dialog;
                   TextView  tvbt,b_name,tv_bt_type,capacity,application,dry,filled,electrolite,dim,btyyfilwei,btyyyelecvol,btyyyappl,bi9,bi8,btyyydime;
     \\tis s second activity

 protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.anim_other_popup);
  Button button = (Button) findViewById(R.id.bt_get_oquote_othr);
            YoYo.with(Techniques.SlideInRight).duration(2000).playOn(button);
            final String cat="2";
 private void getValusesFromIntent() {
            button.setVisibility(View.VISIBLE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent int2=new Intent(getApplicationContext(),Getquote.class);
                    int2.putExtra("Feature_idc",bf_if);
                    int2.putExtra("Category_id",cat);
                    startActivity(int2);
                }
            });}

Getquote activity

public class Getquote extends AppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener{

private ProgressDialog loadDialog;

       private String Feature_id_itt;
       String category_id;
       @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_getquote2);
        inilizeToolbar();
        inilizeProgressDialog();

        final Intent int2=getIntent();
        batteryFeatureid_itt= int2.getStringExtra("battery_featuer_idc");
        category_id=int2.getStringExtra("category_id");
        Feature_id_itt=int2.getStringExtra("Feature_idc");

        final Intent int1=getIntent();
        Feature_id_itt=int1.getStringExtra("Feature_idc");
        category_id=int1.getStringExtra("Category_id");

        name_c = (EditText) findViewById(R.id.name);
        mobile_c = (EditText) findViewById(R.id.mobile);
        emailid_c = (EditText) findViewById(R.id.email_id);
        state_c = (EditText) findViewById(R.id.state);
        city_c = (EditText) findViewById(R.id.city);
        street_c = (EditText) findViewById(R.id.street);
        pin_c = (EditText) findViewById(R.id.pin);
        subject_c = (EditText) findViewById(R.id.subject);
        btsub = (Button) findViewById(R.id.bt_sub);

        btsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                      oncheck();
             }
        });
    }
           private void oncheck() {
        if(category_id==("1")){
     Toast.makeText(Getquote.this, ""+category_id, Toast.LENGTH_SHORT).show();
            }
        else{
            Toast.makeText(Getquote.this, ""+category_id, Toast.LENGTH_SHORT).show();
            }
       }
     }
shyam
  • 1
  • 4

2 Answers2

0

Change your code like this will help

if(category_id.equalsIgnoreCase("1")){
 Toast.makeText(Getquote.this, ""+category_id, Toast.LENGTH_SHORT).show();
        }
    else{
        Toast.makeText(Getquote.this, ""+category_id, Toast.LENGTH_SHORT).show();
        }
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
0

it just because String in java are compared using:

if(StringA.equals(StringB)){}

and not

if(StringA == StringB)
Piyush
  • 18,895
  • 5
  • 32
  • 63
Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62