-2

I've got following method in a class, which is a custom adapter for an ListView.

convertView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String test = availableDevices.get(position).getAddress().toString();   
        }
    });

The String test contains a mac-address of a device. My question is: How can I pass this variable to a new activity. It seems to me that I cant do it the usual way like this:

Intent intent = new Intent();
            intent.setClass(Activity1.this, Activity2.class);
            startActivityForResult(**,**); 
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • Have a look at http://stackoverflow.com/questions/11243976/getting-data-from-first-activity-to-fourth-activity-in-android/11244047#11244047 – RPB Jun 28 '12 at 12:49

7 Answers7

1

You can do it by passing the string in the intent like this:

intent.putExtra("Test", test);

Where the first parameter is a key, and the second is the value.
Then in the other activity do:

Intent launchingIntent = getIntent();  
launchingIntent.getStringExtra("Test");
Nermeen
  • 15,883
  • 5
  • 59
  • 72
1

pass test as to new Activity2:

Intent intent = new Intent();
intent.setClass(Activity1.this, Activity2.class);
intent.putExtra("Test", test);
startActivityForResult(intent,0); // 0 is as requestCode here 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You have to use the Context object for classes other than those which extend Activity.

Intent intent = new Intent();
            intent.setClass(contextObj, Activity2.class);
           intent.putExtra("macadd", //value goes here);
           contextObj.startActivityForResult(intent,requestcode goes here); 
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • I only got these error messages: The method setClass(Context, Class>) in the type Intent is not applicable for the arguments (DeviceAdapter, Class) and The method startActivityForResult(DeviceAdapter, Class) is undefined for the type Context – Tobias Moe Thorstensen Jun 28 '12 at 12:49
1

set public static String test. then you able to access it hold application.

Thanks.

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
0

SOLVED IT THIS WAY

Intent intent = new Intent(c, Connection.class);
            intent.putExtra("mac", test);
            c.startActivity(intent);

Thanks guys!

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
0

use the SharedPreferences like this

 String username = CarName.getText().toString();
         SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         SharedPreferences.Editor editor = settings.edit();
         editor.putString("Shareusername",username);
         editor.commit();

and at the receiving end use something like this

//TextView textView = (TextView)findViewById(R.id.lblnames); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); String shareUsername = settings .getString("Shareusername","null"); Vehicle.setText(shareUsername);

Mr Mashabela
  • 112
  • 2
  • 10
0

while setting adpater like pass in it YourCurrentActivity.this

 Adapter adapter=new Adapter(YourCurrentActivity.this); 

while in your Adpater class made changes as shown below

 public Activity activity;
 public Adapter(Activity act) {
    activity = act;    
 }

after this use code as shown below in your method

  Intent intent = new Intent();
  intent.setClass(activity.this, Activity2.class);
  intent.putExtra("Test", test);
  startActivityForResult(intent,0);
Khan
  • 7,585
  • 3
  • 27
  • 44