Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking a new specific object from a class generic
    primarykey
    data
    text
    <p>Okay, so I've done some looking around and I think I might be going about this wrong. I've taken classes in college for C++ as electives and I like to tinker, but I'm still learning Java.</p> <p>So I'm using a ListAdapter to put a list of different fragments that the user can pick from, then the fragment they chose sits where the ListAdapter was. It only takes arrays, no biggie.</p> <pre><code>/** * An array of POJOs used to hold the info about the fragments we'll be * swapping between This should be inserted into an array adapter of some * sort before being passed onto ListAdapter */ private static final FragmentDetails[] FRAGMENT_DETAILS = { new FragmentDetails(R.string.action_extraInfo, R.string.extraInfo_description, ExtraInfoFragment.class), new FragmentDetails(R.string.action_largeTach, R.string.largeTach_description, LargeTachFragment.class), ... }; /** * @author PyleC1 * * A POJO that holds a class object and its resource info */ public static class FragmentDetails { private final Class&lt;? extends Fragment&gt; fragmentClass; private int titleId; private int descriptionId; /** * @param titleId * The resource ID of the string for the title * @param descriptionId * The resource ID of the string for the description * @param fragmentClass * The fragment's class associated with this list position */ FragmentDetails(int titleId, int descriptionId, Class&lt;? extends Fragment&gt; fragmentClass) { super(); this.titleId = titleId; this.descriptionId = descriptionId; this.fragmentClass = fragmentClass; } ... } </code></pre> <p>At any rate, I run this through a simple get/set class called CustomArrayAdapter and send it off to ListAdapter when the fragment is attached.</p> <pre><code>public void onAttach(Activity activity) { super.onAttach(activity); ListAdapter listAdapter = new CustomArrayAdapter(getActivity(), FRAGMENT_DETAILS); setListAdapter(listAdapter); </code></pre> <p>}</p> <p>So far so good. The problem comes in when I try to program the onListItemClick listener. I can't seem to find a way to create a real object from the class info. I looked around and found the .getClass().newInstance() function was supposed to be roughly similar to new so I tried this.</p> <pre><code>public void onListItemClick(ListView l, View v, int position, long id) { FragmentDetails details = (FragmentDetails) getListAdapter().getItem(position); FragmentManager fragmentManager = ... FragmentTransaction fragmentTransaction = ... fragmentTransaction.add(R.id.fragment_container, details.fragmentClass.newInstance()); } </code></pre> <p>Doing that throws an Illegal Access Exception in the compiler. I know something similar was acceptable in C++. A class pointer maybe? But that might be the complete wrong way in Java. Not typesafe perhaps?</p> <p>My only other thought is to remove the </p> <pre><code>Class&lt;? extends Fragment&gt; fragmentClass </code></pre> <p>generic from the array and just use a switch statement (from the title id perhaps) to hard code the fragment transactions, although that seems a bit inelegant. It works fine if you want to launch new activites, you can just pass the generic class to the intent like this:</p> <pre><code>Class&lt;? extends Foo&gt; bar = someFooClass.getClass(); new Intent(this, bar); </code></pre> <p>But fragments don't accept this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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