5

To have a colorful ListView I have created my own ArrayAdapter, but when I want to use adapter.clear() or adapter.add() it gives me errors. Here is the code and errors.

XML file:

<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">
</ListView>

Adapter:

class stableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public stableArrayAdapter(Context context, int textViewResourceId, String [] objects) 
    {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < 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;
    }
}

Java 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);
    }   
}

Add code:

public void submit(View view) 
{

    adapter.add("Hello");
    } 

Errors:

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)
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • This is why you are getting this error: (http://stackoverflow.com/a/5125428/833647) – Ken Wolf Jun 08 '13 at 05:46
  • Your object array is empty by the way: `Target=new String []{""};` – Ken Wolf Jun 08 '13 at 05:56
  • Yes i know, in the beginning it's empty, but i can add data to this – Sal-laS Jun 08 '13 at 05:59
  • Also, please try to follow Android conventions when coding. Class names are supposed to start with Uppercase and follow CamelCase. It helps other coders to read your code if we all do the same even when we might not like it or think it's the best. ;) http://source.android.com/source/code-style.html – Martin Marconcini Oct 01 '13 at 22:24

5 Answers5

7

Try the below. have a arraylist. add string to arraylist and call notifiydatasetchanged() on your adapter

public class MainActivity extends ListActivity
{
stableArrayAdapter adapter;
ListView lstView; 
ArrayList<String> Target = new ArrayList<String>(); 
@Override
public void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
    lstView = getListView();
    lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
    lstView.setTextFilterEnabled(true);
    Target.add("hi");
    Target.add("hello");
    adapter = new stableArrayAdapter(this,android.R.layout.simple_list_item_checked, Target);
    setListAdapter(adapter);
    Target.add("myname");
    adapter.notifyDataSetChanged();
}   
class stableArrayAdapter extends ArrayAdapter<String> {


    public stableArrayAdapter(Context context, int textViewResourceId, ArrayList<String> target) 
    {
      super(context, textViewResourceId, target);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Target.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return Target.get(position);
    }

    @Override
    public int getPosition(String item) {
        // TODO Auto-generated method stub
        return super.getPosition(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

} 
}

As ken suggested.

public class MainActivity extends ListActivity
   {

    ListView lstView; 
    ArrayList<String> Target = new ArrayList<String>(); 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
        lstView = getListView();
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
        lstView.setTextFilterEnabled(true);
        Target.add("hi");
        Target.add("hello");
       ;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, Target);
        setListAdapter(adapter);
        Target.add("myname");
        adapter.notifyDataSetChanged();
    }   
    }

enter image description here

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 2
    Dear @Raghunandan doubtlessly you are a great programmer. I will be appreciate if i can be your friend in Facebook is it possible? – Sal-laS Jun 08 '13 at 06:39
3

I dont know these may help you or not..

As per my opinion You should check your xml file.. When you use getListView() method you should declare your Object of Listview like

android:id = @android:id/list

Or

use these line with your ArrayAdpater

arrAdapt.setNotifyOnChange(true);

May these help you..

Karan Mavadhiya
  • 1,042
  • 8
  • 23
0

you forgot to intialize your listview like below:

lstView = (ListView) findViewById(R.id.yourlistview);

and then

lstView = getListView();
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

You are encountering this problem.

You don't actually need to extend ArrayAdapter - currently it's not doing anything for you.

Instead do this:

 List<String> target = new ArrayList<String>();
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, target);

Then later your .add() call will work.

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Well, it will show an empty list with your current code. That's what you want right? Or does it error? – Ken Wolf Jun 08 '13 at 06:11
  • You need to show us what you are doing here: `net.learn2develop.UsingIntent.createtarget.submit(createtarget.java:136)` – Ken Wolf Jun 08 '13 at 06:15
  • The way you are adding is wrong, that's what the above error says. I can't help more without more information about what else you are doing. – Ken Wolf Jun 08 '13 at 06:15
  • I add the submit method in my question – Sal-laS Jun 08 '13 at 06:20
  • Dear @Ken Wolf, **ArrayAdapter ** lets me have colerful ListView and is a useful class – Sal-laS Jun 08 '13 at 06:27
  • I agree - follow this tutorial to learn how to extend it properly (http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/) – Ken Wolf Jun 08 '13 at 06:28
0

To use ListActiivity you have to use id of Listview as - android:id="@android:id/list" .

Then only you can get the instance of ListView by calling - getListView(); method .

Shailendra
  • 325
  • 2
  • 10