Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3315608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:22:13+00:00 2026-05-17T22:22:13+00:00

Hi is it possible to cast a List? i have an abstract class that

  • 0

Hi is it possible to cast a List?

i have an abstract class that has a method that takes some sort of List<>, iterate through it in a for loop getting each object in that list and calling the insertItem abstract method that is implemented by the sub class to basically pull out the proper data in the items and then finaly inserting them into a database table.

here is the super class method:

protected void insertAllItemsToDb(List<Object> items, String table) {
        // open db and table

        database().beginTransaction();
        // clear all data from table
        clearTable(table);
        // call a insert statement to insert each column from an item
        for (Object object : items) {
            insertItem(object, table);
        }
        // close db
        database().endTransaction();
        database().close();
    }

In the subclass here is one of the override methods: Which i am able to cast the object fine here.

  @Override
    protected void insertItem(Object object, String table) {

        CalendarEventItem item = (CalendarEventItem) object;
        eventItemValue = new ContentValues();

        eventItemValue.put(LABEL_EVENTS_TITLE, item.getEventTitle());
        eventItemValue.put(LABEL_EVENTS_LOCATION, item.getEventLocation());
        eventItemValue.put(LABEL_EVENTS_DATE, item.getEventStartTime()
                .getDate());
        eventItemValue.put(LABEL_EVENTS_TIME, item.getEventStartTime()
                .getTime());
        eventItemValue.put(LABEL_EVENTS_TIMEZONE, item.getEventStartTime()
                .getTimeZone());

        database.insert(TABLE_NAME_EVENTS, null, eventItemValue);

    }

i then call that method from the superclass using this:

events =  (List<CalendarEventItem>) items;
        insertAllItemsToDb(events, TABLE_NAME_EVENTS);

But i recieve a compile error saying you cant cast it. Any ideas on how i can achieve this without having to duplicate the same steps and code you see inside the insertAllItemsToDb()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:22:13+00:00Added an answer on May 17, 2026 at 10:22 pm

    Use a Type parameter

    Add a generic Parameter to the abstract class:

    public abstract class BaseClass<T>{
    
        protected abstract void insertItem(T object, String table);
    
        protected void insertAllItemsToDb(List<T> items, String table) {
            //...
            for (T object : items) {
                insertItem(object, table);
            }
            //...
        }
    
    }
    

    Now you don’t need any casting, a child class just has to use the correct type:

    public class FooBar extends BaseClass<Phleem>{
        protected void insertItem(Phleem object, String table){
            // ...
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that returns an instance of Map<String, List<Foo>> x(); and another
I have a method that uses a list which I need to pass into
In C# it's possible to cast to List<T> - so if you have: List<Activity>
I have some code that creates a list of lists. The list is of
Is it possible to cast a custom class to a value type? Here's an
I was wondering if its possible to directly cast objects within a foreach loop.
Say you have an API that is not accessible to change: List<LegacyObject> getImportantThingFromDatabase(Criteria c);
I wonder whether it is possible to cast a non-Comparable to something so that
The question is whether built-in sorting of a Generic.List(Of T) is possible. We have
I have an IList<T> that I need to sort, and I would rather not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.