Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing data on Callback onListItemClick to a Fragment
    text
    copied!<p>I'm trying to learn fragment, I'm using the Multiform project template created in eclipse. I run into a problem when passing data in the <code>onListItemClick.</code> I have data loaded in to a SQL database this is a list of names and addresses. in the list view i have the names listed and I want to list the name and address when a name is click in the <code>ListView</code>. There maybe a better way to do this but I only have this to start with.</p> <pre><code>public class CustomerListFragment extends ListFragment { private Callbacks mCallbacks = sDummyCallbacks; public interface Callbacks { public void onItemSelected(String id); } private static Callbacks sDummyCallbacks = new Callbacks() { public void onItemSelected(String id) { } }; @Override public void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); long myLong = values.get(position).getId(); mCallbacks.onItemSelected(String.valueOf(myLong)); } </code></pre> <p>In the Detail fragment I have the following. Even I pass the Id from the data the ARG_ITEM_ID always have the value of the id for the component.</p> <pre><code>public class CustomerDetailFragment extends Fragment { public static final String ARG_ITEM_ID = "_id"; DataSource cusdatasource; sqlCustomer selectedCustomer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); datasource = new DataSource(getActivity()); datasource.open(); if (getArguments().containsKey(ARG_ITEM_ID)) { mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID)); } selectedCustomer = datasource.getCustomer("here I need the _id from the list item clicked"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_detail_customer, container, false); if (selectedCustomer != null) { ((TextView) rootView.findViewById(R.id.editText1)).setText(selectedCustomer.getName()); ((TextView) rootView.findViewById(R.id.editText2)).setText(selectedCustomer.getStreet()); ((TextView) rootView.findViewById(R.id.editText3)).setText(selectedCustomer.getCitySZ()); } return rootView; } </code></pre> <p>On the main activity I have this:</p> <pre><code> public void onItemSelected(String id) { Bundle arguments = new Bundle(); arguments.putString(CustomerDetailFragment.ARG_ITEM_ID, id); CustomerDetailFragment fragment = new CustomerDetailFragment(); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction() .replace(R.id.customer_detail_container, fragment) .commit(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload