neo
neo

Reputation: 177

Store Listview into SQLite table

I have a listview that returns carmodels, with a radiogroup which allows a user to select the condition of the car. I'm trying to save this info into a sqlite table.

  void addListItem(List<Car> listItem) {
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  for (int i = 0; i < listItem.size(); i++) {

  Log.e("value inserting==", "" + listItem.get(i));
  values.put(DBHelper.CARID, listItem.get(i));
  values.put(DBHelper.CARMAKE, listItem.get(i));
  values.put(DBHelper.CONDITION, listItem.get(i));
  db.insert(TABLE_CARCONDITION, null, values);

  }

  db.close(); // Closing database connection

}

In my main activity I also have added the new code below. I'm passing my Listview, Condition (which is the radio button selection) and a timestamp. I fixed my null reference pointer issue. Now I need to make sure I'm looping through all listview items to pull the selected radiobutton. Because as it is now only brings back the first hit.

  View.OnClickListener onSave=new View.OnClickListener() {
  public void onClick(View v) {

  DBHelper db = new DBHelper(getApplicationContext());

  String Condition=null;

  switch (rvalue.getCheckedRadioButtonId()) {
     case R.id.a1:
        Condition="r1";
        break;
     case R.id.a2:
        Condition="r2";
        break;
     case R.id.a3:
        Condition="r3";
        break;
     case R.id.a4:
        Condition="r4";
        break;
  }

  db.addListItem(listCars,Condition,getCurrentTimeStamp());
  }
  };
  save.setOnClickListener(onSave);

Upvotes: 0

Views: 404

Answers (3)

Gatunox
Gatunox

Reputation: 532

Since you used List listItem, every time you call listItem.get(i), you are getting one Car object, and Put does't know anything about Car Class.

Just create a toString method withing your Car Class.

@Override
public String toString() {
    return this.<someproperty of Car>;
}

last but not least, add .toString() when you call listItem.get(i)

values.put(DBHelper.CARID, listItem.get(i).toString());

Upvotes: 0

Karakuri
Karakuri

Reputation: 38595

You have a List<Car>. listItem.get(i) returns a Car object. You need information about the Car object, not the Car object itself (and you cannot store a Car object in ContentValues).

void addList(List<Car> cars) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    for (Car car : cars) {
        Log.e("value inserting==", "" + car.toString());
        values.put(DBHelper.CARID, car.getId());
        values.put(DBHelper.CARMAKE, car.getMake());
        values.put(DBHelper.CONDITION, car.getModel());
        db.insert(TABLE_CARCONDITION, null, values);
    }
    db.close();
}

If your Car doesn't have methods to access its data, add them.

Upvotes: 2

Sinh Phan
Sinh Phan

Reputation: 1266

When store an element in ListView by listItem.get(i) is storing a memory address. I think this cannot.

Upvotes: 0

Related Questions