Note that there are some explanatory texts on larger screens.

plurals
  1. POArray adapter with custom subclass objects
    primarykey
    data
    text
    <p>The solution is in the bottom:</p> <p>I made some classes that i want to use in a listview. They look like this:</p> <pre><code>public class Equipment{ ....code.... } </code></pre> <p>and</p> <pre><code>public class Weapon extends Equipment{ ...More code... } </code></pre> <p>and</p> <pre><code>public class Armor extends Equipment{ ...More code... } </code></pre> <p>As you can see weapon and armor extends equipment. Now i want to use theese classes in the same ArrayAdapter. In my imagination it would be simple as this:</p> <pre><code>public class ShopItemAdapter extends ArrayAdapter&lt;Equipment&gt;{ private List&lt;Equipment&gt; itemList; public ShopItemAdapter(Context context, List&lt;Equipment&gt; itemList) { super(context, android.R.layout.simple_list_item_1, itemList); this.itemList = itemList; this.context = context; } .... CODE .... } </code></pre> <p>But for some reason I may not create an ArrayAdapter Equipment with List Weapon or List Armor.</p> <pre><code>ShopItemAdapter adapter = new ShopItemAdapter(getApplicationContext(), weaponList); </code></pre> <p>This codes gives me the error that ShopItemAdapter(Context, List) is undefined </p> <p>Am I using subclasses in the wrong way or what?</p> <p>My goal is to show a list of armors when clicking on a button and viseversa.</p> <p>Hope my problem is clear. Thanks!</p> <p><em><strong></em> SOLUTION **</strong> So i just needed to change the constructor to receive then cast to List in the super constructor.</p> <pre><code>public class ShopItemAdapter extends ArrayAdapter&lt;Equipment&gt;{ private List&lt;Equipment&gt; itemList; public ShopItemAdapter(Context context, List&lt;? extends Equipment&gt; itemList) { super(context, android.R.layout.simple_list_item_1, (List&lt;Equipment&gt;)itemList); this.itemList = (List&lt;Equipment&gt;)itemList; this.context = context; } .... CODE .... } </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