How to initialize sqlite from AsyncTask (asynchronous task)?

0

Greetings. I am a newbie in the development of applications in android and I would like to support me to solve my doubt of how to initialize SQLite from a class, in this case I am in an AsyncTask but I mark an error in the context I have seen some similar questions here but none has worked for me. In my code I have my MainActivity, a class for SQLite and my class AsyncTask.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _btn1=(Button)findViewById(R.id.btn1);
    _txtname1=(EditText)findViewById(R.id.txtname1);
    _txtname2=(EditText)findViewById(R.id.txtname2);

    _btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            _csqlite.insertar1(String.valueOf(_txtname1.getText()),String.valueOf(_txtname2.getText()));
            Toast.makeText(MainActivity.this, "Datos agregados con exito", Toast.LENGTH_SHORT).show();
        }
    });
}

This is my SQLite class

public class csqlite extends SQLiteOpenHelper {
public csqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}
SQLiteDatabase db;

@Override
public void onCreate(SQLiteDatabase db) {
    String query1="create table tbclientes(_ID integer primary key, User1 text, User2 text);";
    db.execSQL(query1);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void insertar1(String user1, String user2){
    this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("User1",user1);
    values.put("User2", user2);
    this.getWritableDatabase().insert("tbuser",null, values);
    this.getWritableDatabase().close();
}
public void  consulta1(){
    this.getReadableDatabase();
    String [] campos = new String[]{"User1","User2"};
    String [] args = new String[]{"1"};
    Cursor c = db.query("tbclientes",campos,"_ID=?",args,null,null,null);
    if(c.moveToFirst()){
        do{
            String usera=c.getString(0);
            String userb=c.getString(1);
        }while (c.moveToNext());
    }
}
}

And this is my AsyncTask class from which I want to initialize SQLite and be able to invoke the query1 method.

public class background extends AsyncTask <String, Integer, Boolean>  {
csqlite _csqlite = new csqlite(this, "DB1.db", null, 1);

@Override
protected Boolean doInBackground(String... params) {
    return null;
}
}

Thank you.

    
asked by Charlie S 14.09.2017 в 16:39
source

1 answer

0

The class SyncTask<T,T,T> is not a context for you to send the this parameter:

public class background extends AsyncTask <String, Integer, Boolean>  {

   // esto es invalido, la clase no esun contexto por lo que te muestra un error
   csqlite _csqlite = new csqlite(this, "DB1.db", null, 1);

   //..
}

Specify a constructor to the background class that accepts the Contex as a parameter and pass it to the instance of the% csqlite :

public class background extends AsyncTask <String, Integer, Boolean>  {

    csqlite _csqlite

    public background(Context context)
    {
      _csqlite = new csqlite(context, "DB1.db", null, 1);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        return null;
    }

    //..
}

So, whenever you are going to initialize the background class, you have to send the context as a parameter. For example

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        _btn1=(Button)findViewById(R.id.btn1);

        _btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // ejecutamos la clase background enviando el contexto 
                background background = new background(MainActivity.this);
                background.execute();
            }
        });
    }
}
    
answered by 14.09.2017 / 19:55
source