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?
is "values" an ArrayList? if so use
dodaj.setText( (String)values.get( arg2 ) );
您将必须使用arraylist.toarray()
将arraylist转换为数组
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().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.