0

So I have this list which uses values from an ArrayList (and I can't use String[] because the user must add items to the list, and I can't do .add to the String[]). But now I have problems with onItemClick method. I want to get the text from item selected, and my book says the following should work:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    dodaj.setText(values[arg2]);

}

But I get the following error: The type of the expression must be an array type but it resolved to ArrayList What should I do to fix it?

user1477107
  • 37
  • 1
  • 8

3 Answers3

1

is "values" an ArrayList? if so use

dodaj.setText( (String)values.get( arg2 ) );
Iain_b
  • 1,043
  • 5
  • 13
  • (Upvote for lain_b) You cannot use the `[]` to access data in a ArrayList, this only works for basic arrays like `String[]`. Also if `values` is declared as `ArrayList` you can simply use: `dodaj.setText(values.get(arg2));`. – Sam Aug 12 '12 at 17:19
  • Yes this works. Now could you explain what is that index (arg2), what does it mean and what is it for? – user1477107 Aug 12 '12 at 17:28
  • arg2 is the position of the item in the listview. I usually rename it position ( or pos ). – Iain_b Aug 12 '12 at 17:49
  • and if you could also explain the other values (arg1, arg3) that would be great – user1477107 Aug 12 '12 at 18:00
  • arg1 is the view representing the list item clicked and arg3 I had to look up http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html – Iain_b Aug 12 '12 at 18:08
0

you will have to convert arraylist to array using arraylist.toarray()

nandeesh
  • 24,740
  • 6
  • 69
  • 79
0

With ArrayList you always use

arrayList.get(index);

Recommended way is to add a toString() method to your items/Objects so that it returns the preferred field.

public Student()
{
    ...
    ...
    public String toString()
    {
        return firstName + " " + lastName;
    }
}

Now you can use arrayList.get(int index). It will print our the output of toString().

SSG
  • 192
  • 2
  • 12