这可能是一个简单的问题,我已经看到了类似的问题,但我不知道他们的回答是如何适用于我的,所以我提前道歉但是我收到了这个错误:
不兼容的类型:ArrayList无法转换为int
这是作业,我不确定我做错了什么。
这是我的代码:
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);
}
我收到了这一行的错误:
WordAdapter adapter = new WordAdapter(this, words);
这是我的适配器代码:
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;
}
}
答案 0 :(得分:6)
问题是你的第二个构造函数。 ArrayAdapter
没有超级构造函数只接受上下文和ArrayList<Word>
。所以super正在调用(Context, int)
并尝试将单词转换为整数资源。
public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
super(context, words);
}
你的构造函数应该是
public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
super(context, resource, words);
}
答案 1 :(得分:3)
public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
super(context, words);
}
尝试使用对象&#34;单词&#34;设置ressource-ID。
使用以下构造函数:
ArrayAdapter(Context context, int resource, T[] objects)
答案 2 :(得分:0)
你的构造函数
maj_ax_le
正在调用public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
super(context, words);
}
,它在Super类中没有直接匹配。所以它与super(context, words);
相匹配。因此分配和ArrayAdapter(Context context, int resource)
到ArrayList
问题的错误。您应该使用Super class
int
希望这有帮助
修改强>
所以你需要用
替换上面提到的代码ArrayAdapter(Context context, int resource, T[] objects)
并称之为:
public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
super(context, resource, words);
}
答案 3 :(得分:0)
只需将适配器更改为:
public class WordAdapter extends BaseAdapter {
ArrayList<Word> items=new ArrayList();
Context mContext;
public WordAdapter(Context context, ArrayList<Word> words) {
items=words;
mContext=context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Word mWord=items.get(position);
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(mContext).inflate(
R.layout.list_item, parent, false);
}
return listItemView;
}
}