Jasma
Jasma

Reputation: 133

Android: basic java + array

This is a basic question - but i am new to Android and Java, so please help. Am trying to retrive all the rows from a DB (SQLite) and show it on screen. So far it is working fine for a simple retreive and show.

But now, i want to add logic. If a certain field in the array is equal to a value, then i want to perform some operation and then display that field on the screen.

In more detail Bank table - has Bank Name, Bank Balance, Bank Currency. I want to read Bank Currency in a variable, if it is USD, then display Bank Balance as it is. If it is INR, then i want to convert it Bank Balance * 55 and then display the new Balance.

Here is my current code

DB Helper

    public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
    // create an ArrayList that will hold all of the data collected from
    // the database.
    ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();

    Cursor cursor;

    try
    {
        // ask the database object to create the cursor.
        cursor = db.query(
                BANKTABLE_NAME,
                new String[]{BANKTABLE_ROW_ID, BANKTABLE_BANKNAME, BANKTABLE_BALAMT, BANKTABLE_CURRENCY},
                null, null, null, null, null
        );

        // move the cursor's pointer to position zero.
        cursor.moveToFirst();

        // if there is data after the current cursor position, add it
        // to the ArrayList.
        if (!cursor.isAfterLast())
        {
            do
            {
                ArrayList<Object> dataList = new ArrayList<Object>();

                dataList.add(cursor.getLong(0));
                dataList.add(cursor.getString(1));
                dataList.add(cursor.getInt(2));
                dataList.add(cursor.getString(3));

                dataArrays.add(dataList);
            }
            // move the cursor's pointer up one position.
            while (cursor.moveToNext());
        }
    }
    catch (SQLException e)
    {
        Log.e("DB Error in Retreive all", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList that holds the data collected from
    // the database.
    return dataArrays;
}

and this is the java class where i am calling this array

    private void updateTable()
{


    // collect the current row information from the database

    ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();

    // iterate the ArrayList, create new rows each time and add them
    // to the table widget.

    for (int position=0; position < data.size(); position++)
    {
        TableRow tableRow= new TableRow(this);

        ArrayList<Object> row = data.get(position);

        TextView bankNameN = new TextView(this);
        bankNameN.setText(row.get(1).toString());
        tableRow.addView(bankNameN);


        TextView balINRA = new TextView(this);
        balINRA.setText(row.get(2).toString());
        tableRow.addView(balINRA);


        dataTable.addView(tableRow);
    }
}

this is the simple code to extact the Bank Name and Amount as it is. I want to add logic for the IF ELSE. Please guide me with the code. Thanks!

Upvotes: 1

Views: 141

Answers (1)

Mehul Joisar
Mehul Joisar

Reputation: 15358

use this code to display amount in balINRA

    TextView balINRA = new TextView(this);
    if(row.get(3).toString.equals("USD"))
    {
    balINRA.setText(String.valueOf((Integer.parseInt(row.get(2).toString()))*55));
    }
    tableRow.addView(balINRA);

Upvotes: 1

Related Questions