如果我没有在那个特定的 class 中声明它,我该如何调用 class 上的 getItem 方法
How can i call getItem method on a class if I did not declare it in that particular class
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Word currentWord = getItem(position);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
miwokTextView.setText(currentWord.getmMiwokTranslation());
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.Default_text_view);
defaultTextView.setText(currentWord.getmDefaultTranslation());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.image);
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentWord.getmImageResourceId());
return listItemView;
}
}
如果我没有在那个特定的 class 中声明它,我如何在 class 上调用它的 getItem 方法。还请告诉我它是如何工作的,以及它的用途。
上面给出的代码是一个基本适配器class,它允许一个class Word 包含两个字符串和一个图像资源整型变量及其getter 方法。我的意图是在单个列表项中显示两个文本视图和一个图像视图。代码很好,应用程序也 运行 不错。
getItem 在父项中定义 class ArrayAdapter. 如果需要,您可以覆盖它。
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Word currentWord = getItem(position);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
miwokTextView.setText(currentWord.getmMiwokTranslation());
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.Default_text_view);
defaultTextView.setText(currentWord.getmDefaultTranslation());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.image);
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentWord.getmImageResourceId());
return listItemView;
}
}
如果我没有在那个特定的 class 中声明它,我如何在 class 上调用它的 getItem 方法。还请告诉我它是如何工作的,以及它的用途。
上面给出的代码是一个基本适配器class,它允许一个class Word 包含两个字符串和一个图像资源整型变量及其getter 方法。我的意图是在单个列表项中显示两个文本视图和一个图像视图。代码很好,应用程序也 运行 不错。
getItem 在父项中定义 class ArrayAdapter. 如果需要,您可以覆盖它。