【问题标题】:ArrayList <Word> cannot be converted to intArrayList <Word> 无法转换为 int
【发布时间】:2016-07-27 12:16:54
【问题描述】:

这可能是一个简单的问题,我看到过类似的问题,但我看不出他们的回答如何适用于我的问题,所以我提前道歉,但我收到了这个错误:
不兼容的类型:ArrayList 无法转换为 int

这是一个作业,我不确定我做错了什么。

这是我的代码:

package com.example.android.miwok;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);

        // Create a list of words
        ArrayList<Word> words = new ArrayList<Word>();
        words.add(new Word("one", "lutti"));
        words.add(new Word("two", "otiiko"));
        words.add(new Word("three", "tolookosu"));
        words.add(new Word("four", "oyyisa"));
        words.add(new Word("five", "massokka"));
        words.add(new Word("six", "temmokka"));
        words.add(new Word("seven", "kenekaku"));
        words.add(new Word("eight", "kawinta"));
        words.add(new Word("nine", "wo’e"));
        words.add(new Word("ten", "na’aacha"));

        // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
        // adapter knows how to create list items for each item in the list.
        WordAdapter adapter = new WordAdapter(this, words);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // activity_numbers.xml layout file.
        ListView listView = (ListView) findViewById(R.id.list);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);
    }

我在这一行收到错误:

WordAdapter adapter = new WordAdapter(this, words);

这是我的适配器代码:

package com.example.android.miwok;

        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ArrayAdapter;

        import java.util.ArrayList;

/**
 * Created by danle on 7/25/2016.
 */
public class WordAdapter extends ArrayAdapter<Word> {
    public WordAdapter(Context context, int resource) {
        super(context, resource);
    }

    public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
        super(context, resource, words);
    }




    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }
        return listItemView;
    }
}

【问题讨论】:

  • int 是原始类型,您不能将ArrayList &lt;Word&gt; 类型的对象分配给它。
  • 你在哪里得到错误?
  • public WordAdapter(NumbersActivity context, ArrayList words) { super(context, words); }
  • 专门针对 (context,words) 中的单词。
  • 能否请您发布我们的适配器代码。 @丹尼希金斯

标签: java android arraylist


【解决方案1】:

问题是你的第二个构造函数。 ArrayAdapter 没有超级构造函数,它只需要一个上下文和一个 ArrayList&lt;Word&gt;。所以那个 super 正在调用(Context, int) 并尝试将单词转换为整数资源。

public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
    super(context, words);
}

你的构造函数应该是

public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
    super(context, resource, words);
}

【讨论】:

  • 可能它还应该引用Context 而不是具体活动。
  • 很高兴有帮助,请将答案标记为已接受,以防其他人有同样的问题。
  • 解决了该问题,但立即在我的 numbersactivity 中产生了相同的错误代码。我已将 numbersactivity 中的代码编辑到我的原始问题中。你能帮我看看吗?
  • 您需要将列表行的布局传递给您的适配器。 WordsAdapter(this, R.layout.something, words)
  • 我很抱歉,但这超出了我的想象。我把这个放在哪里?
【解决方案2】:
public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
        super(context, words);
    }

尝试使用对象“words”设置资源ID。

使用以下构造函数:

ArrayAdapter(Context context, int resource, T[] objects)

See Also

【讨论】:

    【解决方案3】:

    你的构造函数

    public WordAdapter(NumbersActivity context, ArrayList<Word> words) {
            super(context, words);
    }
    

    正在调用在 Super 类中没有直接匹配的 super(context, words);。所以它与ArrayAdapter(Context context, int resource) 匹配。因此分配错误并将ArrayList 分配给int 问题。您应该使用 Super 类中的以下构造函数

    ArrayAdapter(Context context, int resource, T[] objects)

    希望对你有帮助

    编辑

    所以你需要将上面提到的代码替换为

     public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
            super(context, resource, words);
     }
    

    然后这样称呼它:

    WordAdapter adapter = new WordAdapter(this,<resource id for list item android.R.layout.xxx>, words);
    

    【讨论】:

    • 我是否使用您建议的构造函数粘贴到我当前的构造函数上?
    • 是的,您需要使用建议的 3 参数超级构造函数来修改现有构造函数
    【解决方案4】:

    只需将您的适配器更改为:

    public class WordAdapter extends BaseAdapter {
    
        ArrayList<Word> items=new ArrayList();
    
        Context mContext;
    
        public WordAdapter(Context context,  ArrayList<Word> words) {
            items=words;
            mContext=context;
        }
    
        @Override
        public int getCount() {
            return items.size();
        }
    
        @Override
        public Object getItem(int position) {
            return items.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Word mWord=items.get(position);
            View listItemView = convertView;
            if (listItemView == null) {
                listItemView = LayoutInflater.from(mContext).inflate(
                        R.layout.list_item, parent, false);
            }
            return listItemView;
        }
    }
    

    【讨论】:

      【解决方案5】:

      花一整天时间找出我的错误,但你的文件 wordAdapter 中有你的错误:

      应该是这样的:

      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);
          }
      
          @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.simple_list_item_3, 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 mongolTextView = (TextView) listItemView.findViewById(R.id.mongol_text_view);
              // Get the version name from the current AndroidFlavor object and
              // set this text on the name TextView
              mongolTextView.setText(currentWord.getmMongolTranslation());
      
              // Find the TextView in the list_item.xml layout with the ID version_number
              TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-30
        • 2013-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多