【发布时间】:2016-09-05 20:06:58
【问题描述】:
我正在学习有关 Udacity 的教程,但有些东西我不明白。下面是一个自定义类适配器:
public class WordAdapter extends ArrayAdapter<Word> {
private static final String LOG_TAG = WordAdapter.class.getSimpleName();
public WordAdapter(Activity context, ArrayList<Word> Word) {
super(context, 0, Word);
}
@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);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.text_view_one);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
defaultTextView.setText(currentWord.getDefaultTranslation());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.text_view_lutti);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
miwokTextView.setText(currentWord.getMiwokTranslation());
ImageView numberImageView = (ImageView) listItemView.findViewById(R.id.image_view_id);
numberImageView.setImageResource(currentWord.getImageResourceID());
// Return the whole list item layout (containing 2 TextViews)
// so that it can be shown in the ListView
return listItemView;
}
}
我们声明了一个名为currentWord的变量,其数据类型为Word(自定义类),然后我们在2个TextViews和1个ImageView中使用了这个变量,我们用它来获取单词。我不明白的部分是我们如何以 3 种方式而不是一种方式使用等于单个值 (getitem(position)) 的单个变量?
【问题讨论】:
-
文本视图和图像视图不直接使用 Word。 Word 首先获取一个称为
currentWord.getMiwokTranslation()的方法,然后将一个值返回给setText()。这些方法不必返回 Word,它们可以返回任何内容。 -
您认为
getDefaultTranslation()、getMiwokTranslation()和getImageResourceID()会怎样?由于Word是自定义类,请看源码。 -
@JosephYoung 啊,所以你的意思是因为 currentWord 没有返回值,所以它可以以其他方式使用?
-
不能。它有成员和方法。下一个?
标签: java android class properties