数组适配器构造函数不适用

时间:2017-10-06 06:50:30

标签: java android android-arrayadapter android-context

我收到此错误:

Error:(31, 9) error: no suitable constructor found for ArrayAdapter(Activity,int,List<UserTreeData>)
    constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
    (argument mismatch; List<UserTreeData> cannot be converted to int)
    constructor ArrayAdapter.ArrayAdapter(Context,int,UserTreeInputData[]) is not applicable
    (argument mismatch; List<UserTreeData> cannot be converted to UserTreeInputData[])
    constructor ArrayAdapter.ArrayAdapter(Context,int,List<UserTreeInputData>) is not applicable
    (argument mismatch; List<UserTreeData> cannot be converted to List<UserTreeInputData>)    

这是我的班级

package com.example.pradnyanand.thetree;

import android.app.Activity;
import android.content.Context;
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.List;

import static com.example.pradnyanand.thetree.R.layout.list_layout;

public class TreeWeekDataList extends ArrayAdapter<UserTreeInputData> {

  private Activity context;
  private List<UserTreeData> treeDatasList;

  public TreeWeekDataList(Activity context, List<UserTreeData> treeDatasList ) {

    super(context, R.layout.list_layout, treeDatasList);

    this.context = context;
    this.treeDatasList =  treeDatasList;

  }

  @NonNull
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(list_layout, null, true);

    TextView dateOfPlantation = (TextView) listViewItem.findViewById(R.id.displayPlanatationDate);
    TextView placeOfPlantation = (TextView) listViewItem.findViewById(R.id.displayPlantationPlace);
    TextView typeOfTree = (TextView) listViewItem.findViewById(R.id.displayTreeType);

    UserTreeData trees = treeDatasList.get(position);

    dateOfPlantation.setText(trees.getDateOfPlanation());
    placeOfPlantation.setText(trees.getPlaceOfPlanation());
    typeOfTree.setText(trees.getTypeOfTree());

    return listViewItem;
  }
}

2 个答案:

答案 0 :(得分:0)

试试这个,super方法将数组作为参数而不是列表。

private UserTreeData[] treeDatasList;

public TreeWeekDataList(Activity context, UserTreeData[] treeDatasList ) {
  super(context, R.layout.list_layout, treeDatasList);

  this.context = context;
  this.treeDatasList =  treeDatasList;

}

答案 1 :(得分:0)

从错误:

no suitable constructor found for ArrayAdapter(Activity,int,List<UserTreeData>)

您需要创建ArrayAdapter所需的匹配构造函数,如下所示:

public TreeWeekDataList(Context context, int resourceId, List<UserTreeData> treeDatasList) {
  super(context, resourceId, treeDatasList);

  this.context = context;
  this.treeDatasList =  treeDatasList;
}

public TreeWeekDataList(Context context, List<UserTreeData> treeDatasList) {
    super(context, 0, treeDatasList);
}