Getting values in String from ArrayListObject

0

I get all my values from the database and I put it in array list<object> I would like to know how to access the value inside that emu arraylist, for example TesteLinhas.get(0).toString() or something like this returns me to where it is allocated in memory, I believe I. I wish I could get her value. Is there any way?

ArrayList linhas = new ArrayList();

        String sql = "SELECT * FROM tb_zerados ORDER BY id ASC";

        conexao.pstm  = conn.prepareStatement(sql);
        conexao.rs = conexao.pstm.executeQuery();

        while(conexao.rs.next()){
            linhas.add(new Object[]{conexao.rs.getInt("id"),
                                    conexao.rs.getInt("numero"),
                                    conexao.rs.getString("ano"),
                                    conexao.rs.getString("nome"),
                                    conexao.rs.getString("estilo"),
                                    conexao.rs.getString("plataforma"),
                                    conexao.rs.getInt("nota")});

        }

        System.out.println(linhas.get(1));

It returns me this: [Ljava.lang.Object;@28a8f402 , and I'd like it to return a String or a Integer .

    
asked by anonymous 29.01.2017 / 14:37

3 answers

2

Each line created in this list is of type array of Object[] , when it will retrieve, it must give a cast of type (which in this case is Object[] ) and to recover the positions that in its case are 7 (from 0 to 6 ) to return to the desired position.

Example with all positions:

System.out.println(((Object[])linhas.get(0))[0]);
System.out.println(((Object[])linhas.get(0))[1]);
System.out.println(((Object[])linhas.get(0))[2]);
System.out.println(((Object[])linhas.get(0))[3]);
System.out.println(((Object[])linhas.get(0))[4]);
System.out.println(((Object[])linhas.get(0))[5]);
System.out.println(((Object[])linhas.get(0))[6]);

here is a minimal example , with fictitious data, but which can help you understand how you have to recover

This will probably solve your problem, but ideally you always want to create a tipado with a array using this type , it's much easier the recovery.

Reading:

29.01.2017 / 15:18
2

You are putting an array inside a ArrayList . I do not recommend doing this, but since you did, the solution is to access the array element within ArrayList . It's like another dimension. This works:

System.out.println(linhas.get(1)[0]);

get(1) is picking up the line and [0] is picking up the column.

You have to do this for each field, that is, it must go from 0 to 6 since it has 7 fields.

You can make a loop:

for (Object obj : linhas.get(1)) {
    System.out.println(obj);
}
    
29.01.2017 / 15:17
0

I do something similar in my application and this may help you.

I have a dbHelper class, where all database manipulation methods are defined.

This is the method that reads the database and returns a list (as an array) of objects that reflect the table:

//Table Name
private static final String TABLE_RUNS = "runs";
//Table Columns
private static final String _ID = "_id";
private static final String COL_ADDRESS = "address"; 
private static final String COL_NAME = "name"; 
private static final String COL_DATE = "date"; 


List<RunData> getAllRuns() {

    List<RunData> runsList = new ArrayList<>();

    String RUN_SELECT_QUERY = "SELECT * FROM " + TABLE_RUNS;

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(RUN_SELECT_QUERY, null);

    try {
        if (cursor.moveToFirst()) {
            do {
                RunData runData = new RunData();
                runData.run_id = cursor.getString(cursor.getColumnIndex(_ID));

                runData.name = cursor.getString(cursor.getColumnIndex(COL_NAME));
                runData.address = cursor.getString(cursor.getColumnIndex(COL_RADDRESS));
                runData.date = cursor.getString(cursor.getColumnIndex(COL_DATE));

                runsList.add(runData);

            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        //Log.d(TAG, "Error while trying to get posts from database");
    } finally {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    }
    return runsList;
}

Use this:

// Defina os objetos
RunDbHelper runDbHelper;
List<RunData> runList = new ArrayList<>();

// Instancie a classe dbHelper
runDbHelper = RunDbHelper.getInstance(getContext());
// call para ler o banco de dados e retornar uma lista de objetos runData
runList = runDbHelper.getAllRuns();
for (RunData runData : runList) { // percorre a lista de dados, usando runData como container
    // Agora, cada linha da lista (é manipulada como objeto de dados 
    doWhatwEver(runData.name, runData.address, runData.date); 
}

RunData is a POJO or JavaBeans object that has the same fields as the table. It is easier to manipulate than indexes in an array.

    
29.01.2017 / 15:19