1
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;
}
}

How can i call getItem method on a class if I did not declare it in that particular class. Please also tell me how it works and also for what purpose it is used for.

The above given code is a basic adapter class the allows a class Word containing two strings and one image resource integer variable and their getter methods as well. My intention is to display two text views and one image view in a single list item. The code is fine and the app is also running good.

1 Answers1

0

getItem is defined in parent class ArrayAdapter. You can override it if you need to.

AlexMok
  • 724
  • 5
  • 18