0

I have IconicAdapter extending ArrayAdapter:

class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
            super(MyService.this, R.layout.activity_listview, R.id.text1, entries);
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
            View row=super.getView(position, convertView, parent);
            ImageView icon=(ImageView)row.findViewById(R.id.img1);
            icon.setImageResource(R.drawable.ic_launcher);
            return(row);
    }
}

When I am using add method, it is adding twice:

IconicAdapter ia=new IconicAdapter();
            lv.setAdapter(ia);
            ia.add("1000");

Any reason why so?

2 Answers 2

2

I had the same problem, but it turns out it was a very stupid mistake on my part. Maybe this will help some people though. The problem was that I was adding the items twice, both in the ArrayAdapter and in the list with the items.

Basically, if you use the constructor:

ArrayAdapter(Context context, int resource, List<T> objects)

or

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

then you should avoid keeping a reference to the objects list that you passed, because the adapter just keeps a reference to it without copying its values. If you need to know the value of these objects from somewhere else then either make a copy of them or simply use the getCount() and getItem(int position) methods to access them. You should definitely avoid trying to modify the array or the list that is referenced by the ArrayAdapter.

Hope this helps someone :D

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is this.

IconicAdapter ia=new IconicAdapter();`

you are adding adapter to list, here you already the adapter.

 lv.setAdapter(ia);

then here what you wanted to do .

    iv.add("1000"); here add something in listview not in adapter

2 Comments

No Cobra.. by doing ia.add("1000"); I am just adding one more item to the ListView..
I don't understand. There is no method such as "add(String str)" in ListView. How can this be an accepted answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.