-3

I have an ArrayList coming from an API and my POJO (Plain Old Java Object) has the required getters and setters required.

@SerializedName("staff")
@Expose
private List<String> staff = new ArrayList<String>();
@SerializedName("departments")
@Expose
private List<String> departments = new ArrayList<String>();

I want to ad the staff and department into an SQLite Table. My existing table has the rest of the strings values. But i am not able to add the ArrayList into the table.

public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + "" +
            " (" + ID + " TEXT PRIMARY KEY not null, " +
            //need to change product id into String or TEXT
            CLIENTS_NAME + " TEXT not null," +
            CLIENTS_ADDRESS1 + " TEXT not null," +
            CLIENTS_ADDRESS2 + " TEXT not null," +
            CLIENTS_ADDRESS3 + " TEXT not null," +
            CLIENTS_ADDRESS4 + " TEXT not null," +
            TYPE + " TEXT not null," +
            CLIENTS_CONTACT + " TEXT not null)" ;

How do i add the Arraylist ? I have tried this but its not working. Any Links or Hints will be appreciated.

This is the insertion of the rest of the string values into the database.Note, i have still not added the staff into the database table yet as i dont know how to insert a Arraylist in the table.

 public void addProducts(Clients_POJO products) {
    //CRUD , adding Products

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Constants.ClientsDATABASE.ID, products.getId());
    values.put(Constants.ClientsDATABASE.CLIENTS_NAME, products.getName());
    values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS1, products.getAddress1());
    values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS2, products.getAddress2());
    values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS3, products.getAddress3());
    values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS4, products.getAddress4());
    values.put(Constants.ClientsDATABASE.TYPE, products.getType());
    values.put(Constants.ClientsDATABASE.CLIENTS_CONTACT, products.getContact());

    try {

        db.insert(Constants.ClientsDATABASE.TABLE_NAME, null, values);
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }
7
  • What is your issue? Show your database insertion code. Commented Oct 6, 2016 at 14:46
  • 2
    define it's not working Commented Oct 6, 2016 at 14:47
  • Can we get an error message for how your SO link isn't working? Commented Oct 6, 2016 at 14:50
  • I wanna know how to Insert the staff - public static final List<String> CLIENTS_STAFF = new ArrayList<>(); into the table.Should i just insert it as a TEXT ? Like CLIENTS_CONTACT + " TEXT not null)" + CLIENTS_STAFF + " TEXT not null)" ; Commented Oct 6, 2016 at 14:54
  • @Neeraj - i still have not added the staff to the database. So not insertion of Arraylist . I will edit my question to add the insertion of the String values. Commented Oct 6, 2016 at 15:00

1 Answer 1

0

I found the solution without converting it using GSON. Let the POJO value for the department be an ArrayList.

@SerializedName("departments")
@Expose
private List<String> departments = new ArrayList<String>();

To create the database we will store the column as a string in the table.

public static final String INPUT_DEPARTMENTS = "departments";
public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + "" +
            " (" + ID + " TEXT PRIMARY KEY not null, " +
            INPUT_DEPARTMENTS + " TEXT not null)" ;

Now while storing and retrieving data to and from the SQLite database.Storing ->

public void addProducts(Clients_POJO products) {
    //CRUD , adding Products
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Constants.ClientsDATABASE.INPUT_DEPARTMENTS, String.valueOf(products.getDepartments()));
    try {

        db.insert(Constants.ClientsDATABASE.TABLE_NAME, null, values);
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }

For retrieving data, we will convert it back to an arraylist to store the value in the POJO.

SQLiteDatabase db = this.getWritableDatabase();
    final ProductFetchListener mListener = listener;
    Cursor cursor = db.rawQuery(Constants.ClientsDATABASE.GET_PRODUCTS_QUERY,null);
    final List<Clients_POJO> productsListDB = new ArrayList<>();
    if (cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            do {
                Clients_POJO products = new Clients_POJO();
                products.setFromDatabase(true);

Now we convert it to an arraylist.

String s =  cursor.getString(cursor.getColumnIndex(Constants.ClientsDATABASE.INPUT_DEPARTMENTS));
List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));
products.setDepartments(myList);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.