Note that there are some explanatory texts on larger screens.

plurals
  1. POUse on "Adapter" for two (or more) parent views?
    primarykey
    data
    text
    <p>Is it possible to use one adapter class (and it's instance) (e.g. own sub-class of <code>ArrayAdapter&lt;ownDataType&gt;</code> for two (or more) different views?</p> <p>In my <code>Activity</code>, I have two <code>Spinner</code> objects that are used to show (and select) related data: A list of parent categories and a list of sub-categories of the selected parent category. Of course, the list in the sub-category-<code>Spinner</code> is updated (setting a new <code>ArrayAdapter</code>), when the parent category is changed. However, I also want to be able to set a sub-category directly (and choose the parent category automatically) when the Activity is created. This makes things complicated.</p> <p>My idea for a simple and clean solution now is to create ONE own Adapter-Class (e.g. derived from <code>ArrayAdapter&lt;CategoryPair&gt;</code>, where <code>CategoryPair</code> is a simple class that holds a pair of parent- and sub-category) and this own adapter class handles all the logic to keep data consistent (much simpler there).</p> <p>In the <code>getView(int position, View convertView, ViewGroup parent)</code> method of the <code>Adapter</code>-class I need to recognize reliably for which of the two <code>Spinner</code> objects in my activity I need to create a view. </p> <p>Can I rely on the <code>parent</code> parameter?</p> <p>Do you think my idea is a good one?</p> <p><em>Edit:</em> Code example:</p> <p>My adapter class:</p> <pre><code>public class CategoryPair { Category parent; Category sub; } public class CategoriesAdapter extends ArrayAdapter&lt;CategoryPair&gt; { private Spinner parentSpinner; private Spinner subSpinner; public CategoriesAdapter(Context context, int textViewResourceId, Spinner par, Spinner sub) { super(context, textViewResourceId); parentSpinner = par; subSpinner = sub; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (parent == parentSpinner) { // } else if (parent == subSpinner) { // } else { Log.e(TAG, "Invalid parent in CategoriesAdapter"); } return super.getView(position, convertView, parent); } </code></pre>
    singulars
    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