java - 如果从父类(super class)继承,为什么我会在 list_item 中收到错误

标签 java android-arrayadapter android-studio-2.2

为什么我收到错误“无法解析 list_item” 我正在扩展父类(super class),但无法访问 list_item。为什么我失踪了?这是 udacity 类(class)构建 miwok 应用程序的一部分。在 Android Studio 中。谢谢

    package com.example.android.miwok;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by george on 1/3/17.
 */

public class WordAdapter extends ArrayAdapter<Word> {

    /**
     * This is our own custom constructor (it doesn't mirror a superclass constructor).
     * The context is used to inflate the layout file, and the list is the data we want
     * to populate into the lists.
     *
     * @param context        The current context. Used to inflate the layout file.
     * @param WordAdapter A List of AndroidFlavor objects to display in a list
     */
    public WordAdapter(Activity context, ArrayList<Word> Word) {
        // 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, 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 miwokTextView = (TextView) listItemView.findViewById(R.id.list_items);
        // Get the version name from the current AndroidFlavor object and
        // set this text on the name TextView
        miwokTextView.setText(currentWord.getMiwokTranslation());

        // Find the TextView in the list_item.xml layout with the ID version_number
        TextView defaultTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
        // Get the version number from the current AndroidFlavor object and
        // set this text on the number TextView
        defaultTextView.setText(currentWord.getmDefaultTranslation());

        // Return the whole list item layout (containing 2 TextViews and an ImageView)
        // so that it can be shown in the ListView
        return listItemView;
    }
}

最佳答案

我认为您有与 this one 类似的问题.

您应该做的是在布局文件夹中创建 list_item.xml。

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/miwok_text_view"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:textSize="15sp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:textColor="#000000"/>

关于java - 如果从父类(super class)继承,为什么我会在 list_item 中收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452007/

相关文章:

Java多重继承问题

java - Python 的未签名 CRC 32 与 Java 的 CRC 32 相匹配?

java - Android - 如何异步更新ArrayAdapter?

java - 如何为您的 Android 应用程序添加更新通知,然后用户将被引导至 Google Playstore?

android - Android Studio 2.2.1 Gradle同步NullPointerException

java - 添加列时的 Jtable 问题

android - 在 Activity 中单击按钮时使 View 在 ListView 行中可见

android - 滚动 ProgressBars 的 ListView

android-studio - Android Studio 对 Linux 的 HiDPI 支持

java - 如果列表为空/空,则 IN 子句选择所有元素