gpt4 book ai didi

java - "this"关键字不返回上下文

转载 作者:行者123 更新时间:2023-11-29 08:43:07 24 4
gpt4 key购买 nike

我定义了自己的自定义 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) cannot be applied to 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'
}

最佳答案

我可以看出您的代码没有问题。 AppCompatActivityContext 本身。因此,传递 "this" 无论如何都应该有效。

正如我所见,存在依赖冲突。如果你已经添加了 appcompat 依赖,你不必再添加 support V4 依赖,因为 supportv4 包已经自带了 appcompat 依赖。

我的建议是删除 support v4 依赖项并再次与 gradle 同步。到时候应该没问题即使它不起作用,也只需再执行一步即可。单击 文件 -> 无效/缓存重新启动 -> 单击使缓存无效并重新启动。

现在我很确定,那个问题不会存在。

关于java - "this"关键字不返回上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com