【问题标题】:Fitting Constructor for a Custom adapter自定义适配器的装配构造函数
【发布时间】:2016-02-17 14:08:01
【问题描述】:

我正在尝试创建一个自定义适配器,我有一个错误说没有可用的默认构造函数

public class GuessAdapter extends ArrayAdapter <Game> {

    Context context;
    int resource;
    Peg[] guess;
    LayoutInflater inflater;

    public void PegArrayAdapter(Peg[] array, Context ctxt){
        guess= array;
        context = ctxt;
        inflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return guess.length;
    }

    @Override
    public Game getItem(int arg0){
        return guess[arg0];
    }

    public long getItemId(int arg0){
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2){

        View view = arg1;

        if (arg1==null){
            arg1 = inflater.inflate(android.R.layout.simple_list_item_1, arg2, false);
        }

        ImageView imageView =(ImageView)arg1.findViewById(R.id.imageView);
        ImageView imageView2=(ImageView)arg1.findViewById(R.id.imageView2);
        ImageView imageView3=(ImageView)arg1.findViewById(R.id.imageView3);
        ImageView imageView4=(ImageView)arg1.findViewById(R.id.imageView4);

        return view;
    }
}

这个适配器的合适构造函数是什么?

【问题讨论】:

    标签: java android constructor adapter android-adapter


    【解决方案1】:

    这个

    public void GuessAdapter(Peg[] array, Context ctxt){
         super(ctxt, 0, array);
    }
    

    【讨论】:

      【解决方案2】:

      您尚未向自定义适配器添加任何构造函数。你需要创建一个构造函数来调用super

      在您的自定义适配器中添加此方法

      public GuessAdapter(Peg[] array, Context ctxt) {
          super(ctxt, 0, array);
          guess= array;
          context = ctxt;
          inflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
      

      另外,PegArrayAdapter 方法没有用,你可以在之后删除它。

      【讨论】:

        【解决方案3】:

        制作自定义适配器的最佳方法是使用 BASEADAPTER。 不会有任何问题。 只需将您的 CustomAdapter 类扩展到 BaseAdapter。 我希望它工作正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-22
          • 2015-09-26
          • 1970-01-01
          • 2012-04-29
          • 2013-06-23
          • 1970-01-01
          • 1970-01-01
          • 2018-09-24
          相关资源
          最近更新 更多