This is probably a simple question and I have seen similar questions asked but I don't see how their responses apply to mine, so I apologize in advance but I am getting this error:
Incompatible types: ArrayList cannot be converted to int
This is for an assignment and I am not sure what I am doing wrong.
Here's my code:
package com.example.android.miwok;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
// Create a list of words
ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word("one", "lutti"));
words.add(new Word("two", "otiiko"));
words.add(new Word("three", "tolookosu"));
words.add(new Word("four", "oyyisa"));
words.add(new Word("five", "massokka"));
words.add(new Word("six", "temmokka"));
words.add(new Word("seven", "kenekaku"));
words.add(new Word("eight", "kawinta"));
words.add(new Word("nine", "wo’e"));
words.add(new Word("ten", "na’aacha"));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdapter adapter = new WordAdapter(this, words);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// activity_numbers.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
}
I am getting the error on this line:
WordAdapter adapter = new WordAdapter(this, words);
Here is my adapter code:
package com.example.android.miwok;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
/**
* Created by danle on 7/25/2016.
*/
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Context context, int resource) {
super(context, resource);
}
public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
super(context, resource, words);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
return listItemView;
}
}
The problem is your second constructor. There is no super constructor for ArrayAdapter that takes just a context and an ArrayList<Word>. So that super is calling (Context, int) and trying convert words to an integer resource.
public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
super(context, words);
}
your constructor should be
public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
super(context, resource, words);
}
public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
super(context, words);
}
Tries to set the ressource-ID with the object "words".
Use the following constructor:
ArrayAdapter(Context context, int resource, T[] objects)
See Also
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With