0

I am getting started to Android. I want to get data from another class. I writed some codes but they didn't work. Please help me.

First Activity :

Button b1=(Button) findViewById(R.id.search);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
        Intent a=new Intent(CActivity.this,Db.class);
        a.putExtra("data",bcode);
         startActivity(a);
        }});

Second Activity:

Bundle extra=getIntent().getExtras();
    {
    dContents=extra.getString("data");
    }
Adaichi
  • 45
  • 1
  • 2
  • 10

6 Answers6

0

In Second Activity,remove all the codes to get the data from the intent and try this

dContents = getIntent().getStringExtra("data");

I assume data is string.

vinothp
  • 9,939
  • 19
  • 61
  • 103
0

Try

dContents=getIntent().getStringExtra("data");

instead of

Bundle extra=getIntent().getExtras();
    {
    dContents=extra.getString("data");
    }

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

Do this in you second class file.

Intent i = getIntent();

String str = i.getExtra("data"); 
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
0

This Is worked to me

First Activity (Set data and start Activity)

           String shocode="0003";
           String shopname="CCT";
           Intent i = new  Intent(getApplicationContext(),Frame_unproductuv.class);       
                    i.putExtra("shopcode", shocode);
                    i.putExtra("shopname", shopname);
                    startActivity(i);

Second Activity (Get Data)

              Intent intent = getIntent();
              Bundle b = intent.getExtras();
              route_name = b.getString("shopname");
chathun
  • 169
  • 1
  • 4
  • 12
0

Try:

// store data to be pass to next activity
Intent mIntent = new Intent(this, SecondActivity.class);
mIntent.putExtra("key", yourString);
startActivity(mIntent);


// extract data from previous activity
if (getIntent() != null) {
String mString = getIntent().getExtras().getString("key");
    // do what you going to do with string.
} else {
    Toast.makeText(getApplicationContext(), "getIntent() = null", Toast.LENGTH_LONG).show();
}

I believe that if you using Bundle to retrieve the string you got to use getStringExtra() and if you using Intent to retrieve the data you use getString().

chr.solr
  • 576
  • 1
  • 7
  • 19
0

Why not save the data as a string in your first class then simply retrieve it from your second

String shopname = "enteredshopname";

then from your second class

String getShopName = MainActiviy.shopname;

you can then do whatever setText etc that you need to

Master Zangetsu
  • 262
  • 7
  • 20