66666

How to pass data from a selected item in a listview to another activity?

Question:

I have the following code. I have the data in the object o. There are three set of values in o (description, name, image url). I need these data to be initialized to a string and pass it to other activity using intent. How do I get each value in the object. Each list item has a image, item name and item description.

fp_list.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parentView, View v, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "pos"+position, Toast.LENGTH_LONG).show(); Object o = parentView.getItemAtPosition(position); Intent i = new Intent(FeaturedProductsActivity.this, ShowProduct.class); } }

Answer1:

Better thing is to make you Custom Object Parcelable

the Other option is to pass these values one by one as:

Intent i=new Intent(FeaturedProductsActivity.this,ShowProduct.class); i.putString("desc", o.getDescription()); i.putString("name", o.getName()); // rest of your values

ang get these values in ShowProduct Activity as:

Intent in = this.getIntent(); String name = in.getStringExtra("name"); String desc = in.getStringExtra("desc"); //rest of you values

Answer2:

Bundle bundle =new Bundle(); bundle.putString("memId",o.getId() ); Intent newIntent=new Intent(friendsOffrinends.this,RestaurantDetails.class); newIntent.putExtras(bundle); startActivityForResult(newIntent, 0);

Answer3:

You can't pass object from One activity to another using Intent. Yo have to use Parcelable interface . see this.

<a href="http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html" rel="nofollow">http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html</a>

Answer4:

simple way :

1-make a class which extend Application. 2-make an object of what you want to transfer. 3-make its set and get. 4-in onitemclick set the value. 5-get the value wherever you want.

Example:

1-make a class which extend Application. 2-make an object of what you want to transfer. 3-make its set and get.

public class App extends Application { private static yourObject obj; public yourObject getobj() { return obj; } public void setobj(yourObject obj) { this.obj= obj; } }

in your class

fp_list.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parentView, View v, int position, long id) { // TODO Auto-generated method stub yourObject o =(yourObject) parentView.getItemAtPosition(position); App.setobj(o); }

}

in any other where all over the application classes

//here you will get your object which you have set on item click yourObject obj=App.getobj;

hope this help.

Answer5:

When you have data you can use Bundle or you can directly attach data with Intent e.g. in.putExtra(...).

Answer6:

You can do it like this:

Intent intent = new Intent(YourActivity.this, NewActivity.class);

intent.putExtra("description", o.get(position).getdescription()) startActivity(descriptionIntent);

And in the second activity:

Intent descriptionIntent = getIntent();

String description = descriptionIntent.getExtras().getString("description");

Recommend

  • Volley library NetworkDispatcher.run error
  • Retrieve RSS feed and display it in text view
  • broadcast receiver inside activity
  • Android VideoView Quality is so bad on TV Box using Vitamio Library
  • Unable to retrieve number before incoming call in marshmallow
  • Java thread for JDBC
  • IllegalStateException “System services not available to Activities before onCreate()”
  • Give wx.StaticBitmap a transparent background? wxpython
  • Recycler View Inside Recycler View not Scrolling
  • android.app.PendingIntent cannot be accessed ouside the package
  • Conversion of VB6 HTTP request to VB.Net 2.0
  • C++ how to get substring after get position of its index
  • FCM Data messages are not working properly
  • how can I key rotate for google cloud storage service account?
  • How to get the url of a file of google drive to download in android
  • Fragment gives me an error while inflating 1
  • event.getSource() returns null Accessibility in android
  • Loading Bitmap to ImageView from URL in android
  • How to Make a Spinner (In a Fragment) That Changes the App's Language?
  • Correct implementation of List Iterator methods
  • How to get data from **Realm database** using **date object**?
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • close() was never explicitly called on database
  • How VBA declared Volatility works
  • print() is showing quotation marks in results
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to access EntityManager inside Entity class in EJB3
  • Sending data from AppleScript to FileMaker records
  • Trying to switch camera back to front but getting exception
  • vba code to select only visible cells in specific column except heading
  • align graphs with different xlab
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How to stop GridView from loading again when I press back button?