84451

passing metadata from one activity to the next in an android listview

Question:

I understand that there's a set of methods used to pass data from one activity to the next using, but how do I model it from within the activity with the list view?

Example: I have a json object which i use to populate a listview. Each list item has an id. I want to pass the id to the next activity on click. How do I know that listview's item id? I wasn't sure how to attach arbitrary metadata to a listview item and immediately grab it and pass it along.

public class StuffListActivity extends ListActivity { private MyApplication application = null; /** * Called when the activity is first created. * * @param savedInstanceState the saved instance state */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); application = ((Application) getApplication()); Requestor req = new Requestor(); Application.log(listType.toString()); ArrayList<String> list = req.getStuff(application.getId(), listType); adapter = new ArrayAdapter<String>(this, R.layout.list, R.id.stuff, list); setListAdapter(adapter); } protected void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); } }

Answer1:

Use <strong>setTag()</strong> and <strong>getTag()</strong> property for list view in Adapter getView() code.. to store and retrieve the data.

Recommend

  • collision detection doesn't work inside scene:show
  • How to sort a alphanumeric string value?
  • Locate element which contains visible text (ignoring formatting tags)
  • Sort a list in python
  • PyInstaller OS X app runs from command line, but not Finder window
  • Android repeating alarm not repeating correctly
  • Which one is correct approach for assembly reference in csproj file?
  • Javascript - drag letters in word (Rearrange)
  • Dagger and mvp - should presenter use dagger for injection
  • Do I need app.config?
  • Google Custom Search with transparent background
  • Spring Data JPA custom method causing PropertyReferenceException
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • Can I display google adwords (AdView) in javafx on android
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • C# - Serializing and deserializing static member
  • output of program is not same as passed argument
  • Sending data from AppleScript to FileMaker records
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to include full .NET prerequisite for Wix Burn installer
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • LevelDB C iterator
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?