mardi 5 janvier 2016

using SQLiteAssetHelper

MyDatabase class

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Mydatabase extends SQLiteAssetHelper{

private static final String DATABASE_NAME = "databases/mydb.db";
private static final int DATABASE_VERSION = 1;

public Mydatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Cursor getEmployees() {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder  qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"_id", "name", "email"};
    String sqlTables = "contacts";

    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null,
            null, null, null);

    c.moveToFirst();
    return c;

}

MainActivity class

public class MainActivity extends ListActivity {

private Cursor employees;
private MyDatabase db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new MyDatabase(this);
    employees = db.getEmployees(); // you would not typically call this on the main thread

    ListAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_1, 
            employees, 
            new String[] {"name"}, 
            new int[] {android.R.id.**text1**});

    getListView().setAdapter(adapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    employees.close();
    db.close();
}

}

I want to connect a database in assets folder I try this example from GitHub but the application is stopped working , and I don't know what is "text1" ?

any help please thanks

Aucun commentaire:

Enregistrer un commentaire