Note that there are some explanatory texts on larger screens.

plurals
  1. POListView: How to add data on custom ArrayAdapter?
    primarykey
    data
    text
    <p>To have a colorful ListView I have created my own ArrayAdapter, but when I want to use <code>adapter.clear()</code> or <code>adapter.add()</code> it gives me errors. Here is the code and errors.</p> <p>XML file:</p> <pre><code>&lt;ListView android:id="@+id/android:list" android:layout_width="0dip" android:layout_height="wrap_content" android:padding="10dip" android:layout_margin="4dip" android:layout_weight="1"&gt; &lt;/ListView&gt; </code></pre> <p>Adapter:</p> <pre><code>class stableArrayAdapter extends ArrayAdapter&lt;String&gt; { HashMap&lt;String, Integer&gt; mIdMap = new HashMap&lt;String, Integer&gt;(); public stableArrayAdapter(Context context, int textViewResourceId, String [] objects) { super(context, textViewResourceId, objects); for (int i = 0; i &lt; objects.length; ++i) { mIdMap.put(objects[i], i); } } @Override public long getItemId(int position) { String item = getItem(position); return mIdMap.get(item); } @Override public boolean hasStableIds() { return true; } } </code></pre> <p>Java code:</p> <pre><code>public class createtarget extends ListActivity { stableArrayAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.createtarget); lstView = getListView(); lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setTextFilterEnabled(true); Target=new String []{""}; adapter = new stableArrayAdapter(this,android.R.layout.simple_list_item_checked, Target); setListAdapter(adapter); } } </code></pre> <p>Add code:</p> <pre><code>public void submit(View view) { adapter.add("Hello"); } </code></pre> <p>Errors:</p> <pre><code>06-08 05:21:11.785: E/AndroidRuntime(3584): FATAL EXCEPTION: main 06-08 05:21:11.785: E/AndroidRuntime(3584): java.lang.IllegalStateException: Could not execute method of the activity 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.view.View$1.onClick(View.java:3599) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.view.View.performClick(View.java:4204) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.view.View$PerformClick.run(View.java:17355) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.os.Handler.handleCallback(Handler.java:725) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.os.Handler.dispatchMessage(Handler.java:92) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.os.Looper.loop(Looper.java:137) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.lang.reflect.Method.invokeNative(Native Method) 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.lang.reflect.Method.invoke(Method.java:511) 06-08 05:21:11.785: E/AndroidRuntime(3584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-08 05:21:11.785: E/AndroidRuntime(3584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-08 05:21:11.785: E/AndroidRuntime(3584): at dalvik.system.NativeStart.main(Native Method) 06-08 05:21:11.785: E/AndroidRuntime(3584): Caused by: java.lang.reflect.InvocationTargetException 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.lang.reflect.Method.invokeNative(Native Method) 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.lang.reflect.Method.invoke(Method.java:511) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.view.View$1.onClick(View.java:3594) 06-08 05:21:11.785: E/AndroidRuntime(3584): ... 11 more 06-08 05:21:11.785: E/AndroidRuntime(3584): Caused by: java.lang.UnsupportedOperationException 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.util.AbstractList.add(AbstractList.java:404) 06-08 05:21:11.785: E/AndroidRuntime(3584): at java.util.AbstractList.add(AbstractList.java:425) 06-08 05:21:11.785: E/AndroidRuntime(3584): at android.widget.ArrayAdapter.add(ArrayAdapter.java:179) 06-08 05:21:11.785: E/AndroidRuntime(3584): at net.learn2develop.UsingIntent.createtarget.submit(createtarget.java:136) 06-08 05:21:11.785: E/AndroidRuntime(3584): ... 14 more 06-08 05:21:16.206: E/Trace(3614): error opening trace file: No such file or directory (2) </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.
 

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