Dimitris gs
Dimitris gs

Reputation: 91

ListAdapter is abstract cannot be instantiated

I want to create a list and i get these errors i have tried everything i saw some posts that were saying that i shouldnt name the ListAdapter as ListAdapter but that didnt work either. this is the activity i get the errors

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

}}

and when i try to add an item "list is abstract cannot be instantiated" here is my list 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;
}

} here is my list

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

} i cant understand what im doing wrong

Upvotes: 1

Views: 2200

Answers (1)

Jorge Matricali
Jorge Matricali

Reputation: 334

final ArrayList<List>

List is an built-in interface https://docs.oracle.com/javase/6/docs/api/java/util/List.html

Do you have an import for "java.util.List" in your code?

Upvotes: 1

Related Questions