1

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!

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

or you can also skip the conversion by using this line,balINRA.setText(String.valueOf(row.get(2)*55));
can you help me with another simple Java question. Now instead of hard coding the 55, i want to pull it out of another table. So i have called currencyTable(); inside the updateTable() above. How do i pass variables in between these two? Currency table has two field - CURRENCY and RATE. So if CURRENCY is USD, i want to populate variable "rate" with 55 or if it is GBP, it should be "rate" = 80 etc.
can u let me know the structure of another table which you have created.so that i can give u exact answer
cursor = db.query( CURTABLE_NAME, new String[]{CURTABLE_ROW_ID, CURTABLE_TYPE, CURTABLE_AMOUNT}, null, null, null, null, null ); I am fetching this as an array as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.