Array insert sqlite helper problem insert Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Array insert sqlite helper problem insert without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I need help , for insert data in sqllite from ArrayList

My code:

@Override
public void onResponse(Call<List<Campos>> call, Response<List<Campos>> response) {
    if (!response.isSuccessful()){
        mJsonTxtView.setText("Codigo: " + response.code());
        return;
    }
    List<Campos> camposList = response.body();
    for (Campos campos: camposList){
        SQLiteDatabase db = null;
        String content = "";
        String Proyecto= campos.getsProyecto();
        String Nombre = campos.getsNombre();
        String Estado = campos.getsEstado();
        Integer Activo= campos.getnActivo();
        MyHelper helper = new MyHelper(this);
        helper.saveCampos(Proyecto, Nombre, Estado, Activo);
    }

}

Error in MyHelper helper = new MyHelper(this);

error: incompatible types: <anonymous Callback<List<Campos>>> cannot be converted to Context
                    MyHelper helper = new MyHelper(this);

My helper create db

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE Proyecto (ProyectoId INTEGER PRIMARY KEY AUTOINCREMENT, ZoneName integer,ProyectoName text, ZoneStateId integer, EstadoName text, Codigo text)");
}

My helper insert function

public void saveCampos(String proyecto, String nombre, String estado,Integer activo) {
    SQLiteDatabase helper = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(proyecto, proyecto );
    values.put(nombre, nombre );
    values.put(estado, estado );
    values.put(estado, activo );

    helper.insert(proyecto,null, values);
    helper.close();
}

Could you help me, how can I solve it to insert.

MyHelper Classs , Here I leave the helper class of sqlite, where if the data from the array reaches the mentioned class savecampos:

public class MyHelper extends SQLiteOpenHelper {
private Cursor cursorLogs;
private String query = "";
SQLiteDatabase db;
Context context;
public static final String dbname ="datauser.db";
        private static final int version = 1;
public MyHelper(Context context){
    super(context, dbname, null, version);
   }
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE Proyecto (ProyectoId INTEGER PRIMARY KEY AUTOINCREMENT, ZoneName integer,ProyectoName text, ZoneStateId integer, EstadoName text, Codigo text)");}

public void saveCampos(String proyecto, String nombre, String estado, Integer activo) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("proyecto", proyecto );
    values.put("nombre", nombre );
    values.put("estado", estado );
    values.put("activo", activo );
    db.insert(proyecto,null, values);
    db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

My class call code to retrofit:

public class SplashActivity extends MainActivity {
Context mContext;
private static Context context;
public void onCreate() {
    super.onCreate();
    SplashActivity.context = getApplicationContext();
}
public static Context getAppContext() {
    return SplashActivity.context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
    setContentView(R.layout.activity_splash);
    MyHelper helper = new MyHelper(this);
    SQLiteDatabase database = helper.getReadableDatabase();

    retrofit retrofit = new retrofit();
    retrofit.getCampos();


    // 5 seconds pause on splash page
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            if(isLoggedIn()){
                //Redirect to home page
                intent = new Intent(context,HomeActivity.class);
                startActivity(intent);
                finish();
            }else{
                //Redirect to Login Page
                intent = new Intent(context,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }
    },5000);
}
public void init() {
    context = this;
    sharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME,MODE_PRIVATE);
}

can you help me

Answer

Retrofit onResponse() takes an anonymous inner class arugment, so any code inside refers to this inner class; therefore this refers to an instance other than the context, to get a context, just juse getApplicationContext() or use MyActivity.this.

So, change this

MyHelper helper = new MyHelper(this);

to:

MyHelper helper = new MyHelper(getApplicationContext());

Or to:

MyHelper helper = new MyHelper(MyActivity.this);

Change MyActivity with your activity name that surrounds your code snippet.

We are here to answer your question about Array insert sqlite helper problem insert - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji