我定义了自己的自定义ArrayAdapter
,名为WordAdapter
,如下所示:
public class WordAdapter extends ArrayAdapter<Word> {
Context context;
public WordAdapter(Context context, ArrayList<Word> words) {
super(context, 0, words);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Word currentWord = getItem(position);
TextView miwok = (TextView) listItemView.findViewById(R.id.miwok_text_view);
miwok.setText(currentWord.getMiwokTranslation());
TextView english = (TextView) listItemView.findViewById(R.id.english_text_view);
english.setText(currentWord.getEnglishTranslation());
return listItemView;
}
}
我在一个类中需要它,它的用法如下:
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
ArrayList<Word> words = new ArrayList<>();
words.add(new Word("lutti","one"));
words.add(new Word("otiiko","two"));
words.add(new Word("toldokosu","three"));
words.add(new Word("oyyisa","four"));
words.add(new Word("massoka","five"));
words.add(new Word("temmoka","six"));
words.add(new Word("kenekaku","seven"));
words.add(new Word("kawinta","eight"));
words.add(new Word("wo'e","nine"));
words.add(new Word("na'aacha","ten"));
WordAdapter itemsAdapter = new WordAdapter(this, words);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
但是它在
WordAdapter itemsAdapter = new WordAdapter(this, words);
行中给出了错误,说WordAdapter(android.content.Context ArrayList)无法应用于com.example.android.miwok.NumbersActivity ArrayList
依存关系:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
}
最佳答案
我可以说您的代码没有问题。 AppCompatActivity
是Context
本身。因此,传递"this"
应该仍然有效。
如我所见,存在依赖冲突。如果添加了appcompat依赖项,则也不必添加support V4
依赖项,因为supportv4
软件包已经附带了appcompat
依赖项。
我的建议是删除support v4
依赖项并再次与gradle同步。那应该没问题
即使它不起作用,也只需再执行一个步骤即可。
点击File -> Invalidate/Cache Restart -> Click on Invalidate cahe and Restart.
现在我很确定,这个问题不会出现。