1

I'm creating a custom list view that is able to add or remove items. I try this code to add a new row item to list view but it says "The method add(String) is undefined for the type ListView".

This is my code:

public class AddActivityCustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;

    public AddActivityCustomList(Activity context, String[] web) {
        super(context, R.layout.add_activity_single_list, web);
        this.context = context;
        this.web = web;

    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.add_activity_single_list,
                null, true);
        TextView txtTitle = (TextView) rowView
                .findViewById(R.id.act_title_single);

        txtTitle.setTypeface(myface);
        txtTitle.setText(web[position]);

        return rowView;

    }

And my Add Button code is this:

add_activity.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                lv.add(add_act_title.getText().toString());
                add_act_title.setText("");
                adapter.notifyDataSetChanged();
                add_act_title.getText();
            }
        });

that has problem in lv.add(...part.What should I do?

Mohammad
  • 23
  • 1
  • 6

4 Answers4

1

The listview displays data from the adapter. Add the new string to the Adapter via it's add function.

bryan
  • 798
  • 7
  • 18
  • Change "lv.add(add_act_title.getText().toString());" to "adapter.add(add_act_title.getText().toString());" – bryan Feb 02 '15 at 19:23
1
    add_activity.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // get the adapter from the listview
            AddActivityCustomList adapter = (AddActivityCustomList) lv.getAdapter();
            // call ArrayAdapter.add, no need to call notifyDataSetChanged as add does this
            adapter.add(add_act_title.getText().toString());
            // clear old title
            add_act_title.setText("");
        }
    });
petey
  • 16,914
  • 6
  • 65
  • 97
  • still has error on this line: adapter.add(add_act_title.getText().toString()); . I think I should change my AddActivityCustomList Shouldn't I? – Mohammad Feb 03 '15 at 16:47
0

You can't add data directly to the listview. You need to add the data in the adapter and call notifyDataSetChanged()

Use this

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    adapter.add(add_act_title.getText().toString());
    adapter.notifyDataSetChanged();
    add_act_title.setText("");
    add_act_title.getText();
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

"The method add(String) is undefined for the type ListView"

Because you are calling add method on ListView instead of Adapter.

How to add a new row item to list view

To add/delete item from ListView use ArrayList as data-source instead of Array because if you use Array and call adapter add method then you will get UnsupportedOperationException.

So make following change in AddActivityCustomList Adapter :

1. Use ArrayList<String> instead of String[] Array.

2. Add new item to adapter as:

AddActivityCustomList adapter=listView.getAdapter();
adapter.add(add_act_title.getText().toString());
adapter.notifyDataSetChanged();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I just use ArrayAdapter in AddActivityCustomList . You say I should use ArrayList insted? – Mohammad Feb 02 '15 at 19:34
  • @Mohammad: yes reason is as you no when an Array is created then it's not possible to modify it's size so when we call Adapter.add means we are trying to add new item in Array but array size is already fixed. so you need to some data-structure which grow dynamically like ArrayList – ρяσѕρєя K Feb 02 '15 at 19:36
  • 1
    calling `.add` on an ArrayAdapter will also call `notifyDataSetChanged`. No need to call it yourself, – petey Feb 02 '15 at 19:47
  • 1
    "To add/delete item from ListView use ArrayList as data-source instead of Array because if you use Array and call adapter add method then you will get UnsupportedOperationException." [**This is false, the constructor that uses an Array will convert the array to a List for you**](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ArrayAdapter.java#140) – petey Feb 02 '15 at 19:50
  • @petey: What you think about this post [Why can't one add/remove items from an ArrayAdapter?](http://stackoverflow.com/questions/3476723/why-cant-one-add-remove-items-from-an-arrayadapter),[Unable to modify ArrayAdapter in ListView: UnsupportedOperationException](http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception) – ρяσѕρєя K Feb 02 '15 at 19:54
  • @petey: and if you just google for `UnsupportedOperationException` then you will get lots of post on stackoverflow related to ArrayAdapter – ρяσѕρєя K Feb 02 '15 at 19:55
  • in that example (that is missing a good deal of code), the OP uses CharSequence to back their data. however it is completely unknown if they are adding a `String` or `CharSequence` object. Using a add(StringObject) will give UnsupportedOperationException in this case as Strings and CharSequence are different. – petey Feb 02 '15 at 19:59
  • @petey: Where OP is using `CharSequence` ? i'm not able to see – ρяσѕρєя K Feb 02 '15 at 20:01
  • in your first link question id = 3476723 – petey Feb 02 '15 at 20:02
  • @petey: Hmm it's just example in second like OP using also using Array of String type – ρяσѕρєя K Feb 02 '15 at 20:03
  • @petey : i'm not saying your answer is wrong because you have pointed another point which will avoid one unnecessary method call (which is not related to OP question which OP is asking)but why you downvote my answer – ρяσѕρєя K Feb 02 '15 at 20:04
  • fair enough, please edit your answer slightly so I can remove a -1, & ty – petey Feb 02 '15 at 20:05
  • @petey: i'm not saying you remove -1 . point is that when someone is answering then first understand what he want to say instead of just start down-voting.Thanks – ρяσѕρєя K Feb 02 '15 at 20:07