Richmahnn
Richmahnn

Reputation: 87

getting data from one activity to another

i have two activities that is friends and details, the friends activity is a list view with the list data populated from a database i already created, when a list item is clicked, the details activity should be launched and the list item data carried to the details activity and put into the edit text box in the details class

package com.rich.myfinal;

public class FriendsActivity extends Activity {

     private CustomCursorAdapter customAdapter;
     private PersonDataHelper databaseHelper;
     private ListView listView;

     private static final String TAG = FriendsActivity.class.getSimpleName();
     /**
     * Called when the activity is first created.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends);

        databaseHelper = new PersonDataHelper(this);

        listView = (ListView) findViewById(R.id.list_data);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "clicked on item: " + position);
                Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
                startActivity(i);
            }
         });

         // Database query can be a time consuming task ..
         // so its safe to call database query in another thread
         // Handler, will handle this stuff for you <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley">

         new Handler().post(new Runnable() {
             @Override
             public void run() {
                 customAdapter = new CustomCursorAdapter(FriendsActivity.this, databaseHelper.getAllData());
                 listView.setAdapter(customAdapter);
             }
         });
     }
}

The details activity is below

package com.rich.myfinal;

public class DetailsActivity extends Activity {
    EditText details;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        details = (EditText) findViewById (R.id.details);
    }
}

Upvotes: 2

Views: 148

Answers (4)

URAndroid
URAndroid

Reputation: 6277

just call String value=YourList.get(position); And pass this in Intent using intent.putExtra("Key","Value");

And get this value using Bundle bundle=getIntent.getExtra(); ,

String value=bundle.getExtra("Key");

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

use like that

 @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Log.d(TAG, "clicked on item: " + position);
                      String value = listView .getItemAtPosition(position).toString();
                        Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
                       i.putExtra("data", value);
                        startActivity(i);
                    }
                });

use in detail Activity

Bundle bundle = getIntent().getExtras();
String value= bundle.getString("data");
details = (EditText) findViewById (R.id.details);
details.setText(value)

Upvotes: 0

Aerrow
Aerrow

Reputation: 12134

You can do this in two ways, one is using Bundle and the another one throught Intent.

Using Bundle,

To Send:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
passIntent.putExtras(bundle);
startActivity(passIntent);

To Receive:

Bundle bundle = getIntent().getExtras();
String recText = bundle.getString("Key");

Using Through Intent:

To Send:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", "Value");
startActivity(passIntent);

To Receive:

String recText = getIntent().getExtras().getString("Key"); 

For your Code, in your FirstActivity

listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d(TAG, "clicked on item: " + position);
                    Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
                    passIntent.putExtras("Key", position);
                    startActivity(passIntent);
                }
            });



In SecondActivity,


details.setText(getIntent().getExtras().getString("Key"));

Upvotes: 1

baldguy
baldguy

Reputation: 2082

Pass values you want from FriendsActivity to DetailsActivity activity with Intent like below. Pass values from FriendsActivity when you start another activity by calling startActivity()

intent.putExtra("pos", v.getId()) ;
intent.putExtra("TranObject",(Serializable) data.get(v.getId()).getTranse()) ;

And then receive values in DetailsActivity as below in onCreate()

    try {
        pos = getIntent().getExtras().getInt("pos");
        blog =(clsTranscation) getIntent().getExtras().getSerializable("TranObject");

    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

Related Questions