mercredi 22 avril 2015

memory exhausted when trying to get data from database (android)

I'm trying to get some information from my database. I'm a beginner in android. I have a database's create class, called "Database", and a database access class, called "Database_Acesso". They look like that:

Database.java: package workshopee.ct.ufrn.br.ssmonitor;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database extends SQLiteOpenHelper{
    private static final int versao_db = 1;

    private static final String nome_db = "ssmonitor_db";

    private static final String table1 = "phone";

    private static final String id = "_id";
    private static final String longitude = "longitude";
    private static final String latitude = "latitude";
    private static final String forca_torres = "qtdtorres";
    private static final String forca_dbm = "dbm";
    private static final String mcc = "mcc";
    private static final String mnc = "mnc";
    private static final String phone_type = "phone_type";
    private static final String operadora = "operadora";
    private static final String network_type = "networkType";
    private static final String cid = "cid";
    private static final String lac = "lac";

    public Database(Context context) {
        super(context, nome_db, null, versao_db);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String criarTabela = "CREATE TABLE " + table1 + "("
                + id + " INTEGER PRIMARY KEY AUTOINCREMENT," + longitude + " REAL,"
                + latitude + " REAL," + forca_torres + " INTEGER," + forca_dbm + " REAL," + mcc + " INTEGER,"
                + mnc + " INTEGER," + phone_type + " TEXT," + operadora + " TEXT," + network_type + " INTEGER," + cid + " INTEGER,"
                + lac + " INTEGER )";
        db.execSQL(criarTabela);

               }
    @Override
    public void onUpgrade(SQLiteDatabase db, int versao_ant, int versao_nv) {
        Log.w(Database.class.getName(),
                "Atualizando o banco de dados da versão " + versao_ant + " para "
                        + versao_nv + ", isso apagará os dados antigos.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);

    }

    public void clear (SQLiteDatabase db) {
        Log.w(Database.class.getName(),
                "Apagando informações salvas anteriormente.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);
    }

}

Database_Acesso.java:

package workshopee.ct.ufrn.br.ssmonitor;

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

import java.util.ArrayList;
import java.util.List;

public class Database_Acesso {
    private SQLiteDatabase db;

public Database_Acesso(Context context) {
    Database aux_db = new Database(context);
    db = aux_db.getWritableDatabase();
}
public void inserir_phone (Phone ph) {

    ContentValues valores = new ContentValues();
    valores.put("longitude",ph.getLongitude());
    valores.put("latitude", ph.getLatitude());
    valores.put("qtdtorres", ph.getTorres());
    valores.put("dbm", ph.getDbm());
    valores.put("mcc", ph.getMcc());
    valores.put("mnc", ph.getMnc());
    valores.put("phone_type", ph.getPhoneType());
    valores.put("operadora", ph.getOperadora());
    valores.put("cid",ph.getCid());
    valores.put("lac",ph.getLac());

    db.insert("phone", null, valores);
}


public List<Phone> buscar_phone () {
    List<Phone> lista = new ArrayList<Phone>();

    String[] colunas = new String[]{"_id", "longitude", "latitude", "qtdtorres", "dbm",
                        "mcc", "mnc", "phone_type", "operadora", "networkType", "cid", "lac"};

    Cursor cursor = db.query("phone", colunas, null, null, null, null,"_id ASC");
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
    }

   do {
       Phone p = new Phone();
       p.setId(cursor.getLong(0));
       p.setLongitude(cursor.getDouble(1));
       p.setLatitude(cursor.getDouble(2));
       p.setTorres(cursor.getInt(3));
       p.setDbm(cursor.getInt(4));
       p.setMcc(cursor.getInt(5));
       p.setMnc(cursor.getInt(6));
       p.setPhoneType(cursor.getString(7));
       p.setOperadora(cursor.getString(8));
       p.setNetWorkType(cursor.getString(9));
       p.setCid(cursor.getInt(10));
       p.setLac(cursor.getInt(11));
       lista.add(p);

   } while (!cursor.isLast());

    return lista;
  }

}

Here is the part of my MainActivity that inserts data:

    database_acesso.inserir_phone(cell);

Where database_acesso is an instance of Database_acesso and cell is an instance of Phone.

And here is how I'm trying to get information: TextView list_text_view = (TextView) rootView.findViewById(R.id.list_text_view); List list = main.database_acesso.buscar_phone(); list_text_view.append(" - " + list.size());

I'm using fragments, so "main" is a instance on MainActivity. When I try to execute it, I get the following error:

java.lang.OutOfMemoryError: [memory exhausted]
        at dalvik.system.NativeStart.main(Native Method)

It is the full stack trace.

Any ideia for solving that?

Thx.

Aucun commentaire:

Enregistrer un commentaire