• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Add multiple columns to arraylist

Ken Gordon

Newbie
Feb 12, 2017
27
2
I'm working on a CRUD system for my app. I want to use a listview to show all the items from the tables.
When it comes to adding the items to the arraylist, I don't know if I need multiple lines or a single.

Java:
public void ViewCar()
    {
        Cursor result= db.ViewCar();
        if(result.getCount()==0)
        {
            ShowMessage("ERROR!", "No Car Was Found!");
            return;
        }
        else
        {
            while(result.moveToNext())
            {
                carList.add(result.getString(0));
                carList.add(result.getString(1));     Right Here- What do I need?
                carList.add(result.getString(2));
                carList.add(result.getString(3));

            }

            adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, carList);
        }

Thanks!
 
Is this related to your other thread?

https://androidforums.com/threads/unable-to-create-an-instance-of-a-class.1279942/#post-7777266

If so you should use an ArrayList containing Car objects, not Strings

Code:
List<Car> carlist=new ArrayList<Car>();
Is this related to your other thread?

https://androidforums.com/threads/unable-to-create-an-instance-of-a-class.1279942/#post-7777266

If so you should use an ArrayList containing Car objects, not Strings

Code:
List<Car> carlist=new ArrayList<Car>();

Yes, it's the same project. What about the add part?
 
Upvote 0
See the code in post #1 of the original thread. That's what you need. I don't understand why you've changed that code to now use a list of Strings.
You need to use a list of Car objects.
So how do I reference the class rather than strings
Java:
carList.add(result.getString(0));
                carList.add(result.getString(1));     
                carList.add(result.getString(2));
                carList.add(result.getString(3));
 
Upvote 0
So how do I reference the class rather than strings

I'm not sure what you mean by this. Which class are you talking about?
You're using 'result', which is a database cursor. The getString() methods are returning specific columns from your database table, which are of type String.
There's no 'Car' object stored in your database, you have to create it.

To quote the code from your original post in the link above. All I've done here is add the line to add the newly created car to the carlist.

Java:
public List<Car> ViewCar()  
    {
        try
        {
            List<Car> carlist=new ArrayList<Car>();
            SQLiteDatabase db=this.getWritableDatabase();
            Cursor result=db.rawQuery("select * from "+CAR_TABLE, null);
            if(result.moveToFirst())
            {
                do{
                    Car car=new Car();    
                    car.setMake(result.getString(0));
                    car.setModel((result.getString(1)));
                    car.setYear(result.getString(2));
                    car.setMileage(result.getString(3));
                    carlist.add(car);   // <----------- Adding a new car to the list
                }
                while (result.moveToNext());
            }
            db.close();
            return  carlist;
        }
        catch(Exception e)
        {
            return null;
        }
    
    }
 
Last edited by a moderator:
  • Like
Reactions: Ken Gordon
Upvote 0
I'm not sure what you mean by this. Which class are you talking about?
You're using 'result', which is a database cursor. The getString() methods are returning specific columns from your database table, which are of type String.
There's no 'Car' object stored in your database, you have to create it.

To quote the code from your original post in the link above. All I've done here is add the line to add the newly created car to the carlist.

Java:
public List<Car> ViewCar() 
    {
        try
        {
            List<Car> carlist=new ArrayList<Car>();
            SQLiteDatabase db=this.getWritableDatabase();
            Cursor result=db.rawQuery("select * from "+CAR_TABLE, null);
            if(result.moveToFirst())
            {
                do{
                    Car car=new Car();   
                    car.setMake(result.getString(0));
                    car.setModel((result.getString(1)));
                    car.setYear(result.getString(2));
                    car.setMileage(result.getString(3));
                    carlist.add(car);   // <----------- Adding a new car to the list
                }
                while (result.moveToNext());
            }
            db.close();
            return  carlist;
        }
        catch(Exception e)
        {
            return null;
        }
   
    }
D'OH! Thanks, I should have seen it.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones