ListAdapter是抽象的,无法实例化

时间:2018-06-13 18:15:11

标签: java android

我想创建一个列表,我得到这些错误我已经尝试了一切我看到的一些帖子说我不应该将ListAdapter命名为ListAdapter但是它也没有用。 这是我得到错误的活动

public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_list);
    final ArrayList<List> detail1 = new ArrayList<>();


    detail1.add(new List(R.string.text1, R.string.text11, R.drawable.image1));


    ListAdapter adapter = new ListAdapter(this,detail1, R.color.main);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);

}}

当我尝试添加项目时 “列表是抽象的,无法实例化” 这是我的列表适配器

public class ListAdapter extends ArrayAdapter<List> {

// Resource ID for the background color for this list of detail
private int mColorResourceId;

// context is the current context (i.e. Activity) that the adapter is being created in
// detail is the list of detail to be displayed.
// colorResourceId is the resource ID for the background color for this list of detail
public ListAdapter(Activity context, ArrayList<List> detail, int colorResourceId) {
    super(context, 0, detail);
    mColorResourceId = colorResourceId;
}

@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);
    }

    List currentDetails = getItem(position);

    TextView detailsTextView = (TextView) listItemView.findViewById(R.id.listName);

    detailsTextView.setText(currentDetails.getDetailName());

    TextView moreTextView = (TextView) listItemView.findViewById(R.id.listDesc);

    moreTextView.setText(currentDetails.getMoreInfo());

    // Find the ImageView in the list_item.xml layout with the ID image.

    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);

    // Set the ImageView to the image resource specified in the current Details

    imageView.setImageResource(currentDetails.getImageResourceId());

    // Check if an image is provided for this word or not

    if (currentDetails.hasImage()) {

        // If an image is available, display the provided image based on the resource ID

        imageView.setImageResource(currentDetails.getImageResourceId());

        // Make sure the view is visible

        imageView.setVisibility(View.VISIBLE);

    } else {

        // Otherwise hide the ImageView (set visibility to GONE)

        imageView.setVisibility(View.GONE);
    }

    //Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);

    //find the color that the resource ID maps to
    int color = ContextCompat.getColor(getContext(), mColorResourceId);

    //set the background color of the text container view
    textContainer.setBackgroundColor(color);

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.

    return listItemView;
}

} 这是我的清单

public class List {

//Default details
private int mDetailName;

//More Information about the tab
private int mMoreInfo;

// Image resource ID
private int mImageResourceId = NO_IMAGE_PROVIDED;

// Constant value that represents no image was provided for this word
private static final int NO_IMAGE_PROVIDED = 0;

public List(int detailName, int moreInfo, int imageResourceId) {
    mDetailName = detailName;
    mMoreInfo = moreInfo;
    mImageResourceId = imageResourceId;
}
// Get the details
public int getDetailName() {
    return mDetailName;
}
// Get more info of the tab
public int getMoreInfo() {
    return mMoreInfo;
}
// Create a new object
public int getImageResourceId() { return mImageResourceId; }

// Returns whether or not there is an image for this word.
public boolean hasImage() { return mImageResourceId != NO_IMAGE_PROVIDED; }

} 我无法理解我做错了什么

1 个答案:

答案 0 :(得分:1)

final ArrayList<List>

List是一个内置界面 https://docs.oracle.com/javase/6/docs/api/java/util/List.html

您的代码中是否有“java.util.List”导入?