1. Download our Official Android App: Forums for Android!

Add multiple columns to arraylist

Discussion in 'Android Development' started by Ken Gordon, Jul 12, 2018.

  1. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #751
    Points:
    16
    Posts:
    23
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    23
    2
    16
    Male
    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.

    Code (Java):
    1. public void ViewCar()
    2.     {
    3.         Cursor result= db.ViewCar();
    4.         if(result.getCount()==0)
    5.         {
    6.             ShowMessage("ERROR!", "No Car Was Found!");
    7.             return;
    8.         }
    9.         else
    10.         {
    11.             while(result.moveToNext())
    12.             {
    13.                 carList.add(result.getString(0));
    14.                 carList.add(result.getString(1));     Right Here- What do I need?
    15.                 carList.add(result.getString(2));
    16.                 carList.add(result.getString(3));
    17.  
    18.             }
    19.  
    20.             adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, carList);
    21.         }
    Thanks!

     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,311
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,311
    10,432
    1,988
    Male
    Software developer
    South West of England
  3. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #751
    Points:
    16
    Posts:
    23
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    23
    2
    16
    Male
    Yes, it's the same project. What about the add part?
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,311
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,311
    10,432
    1,988
    Male
    Software developer
    South West of England
    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.
     
  5. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #751
    Points:
    16
    Posts:
    23
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    23
    2
    16
    Male
    So how do I reference the class rather than strings
    Code (Java):
    1. carList.add(result.getString(0));
    2.                 carList.add(result.getString(1));    
    3.                 carList.add(result.getString(2));
    4.                 carList.add(result.getString(3));
    5.  
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,311
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,311
    10,432
    1,988
    Male
    Software developer
    South West of England
    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.

    Code (Java):
    1.  
    2. public List<Car> ViewCar()  
    3.     {
    4.         try
    5.         {
    6.             List<Car> carlist=new ArrayList<Car>();
    7.             SQLiteDatabase db=this.getWritableDatabase();
    8.             Cursor result=db.rawQuery("select * from "+CAR_TABLE, null);
    9.             if(result.moveToFirst())
    10.             {
    11.                 do{
    12.                     Car car=new Car();    
    13.                     car.setMake(result.getString(0));
    14.                     car.setModel((result.getString(1)));
    15.                     car.setYear(result.getString(2));
    16.                     car.setMileage(result.getString(3));
    17.                     carlist.add(car);   // <----------- Adding a new car to the list
    18.                 }
    19.                 while (result.moveToNext());
    20.             }
    21.             db.close();
    22.             return  carlist;
    23.         }
    24.         catch(Exception e)
    25.         {
    26.             return null;
    27.         }
    28.    
    29.     }
     
    #6 LV426, Jul 13, 2018
    Last edited: Jul 13, 2018
    Ken Gordon likes this.
  7. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #751
    Points:
    16
    Posts:
    23
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    23
    2
    16
    Male
    D'OH! Thanks, I should have seen it.
     

Share This Page

Loading...