首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:基础java +数组

Android:基础java +数组
EN

Stack Overflow用户
提问于 2013-01-02 16:23:39
回答 1查看 129关注 0票数 1

这是一个基本的问题--但我是Android和Java的新手,所以请帮助我。我试图从一个数据库(SQLite)中检索所有行,并将其显示在屏幕上。到目前为止,对于简单的检索和显示,它工作得很好。

但现在,我想添加一些逻辑。如果数组中的某个字段等于某个值,那么我想执行一些操作,然后在屏幕上显示该字段。

更详细的银行表-有银行名称,银行余额,银行币种。我想在一个变量中读取Bank Currency,如果它是USD,则按原样显示Bank Balance。如果它是INR,那么我想将它转换为Bank Balance * 55,然后显示新的余额。

以下是我当前的代码

DB Helper

代码语言:javascript
运行
复制
    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;
}

这就是我要调用这个数组的java类

代码语言:javascript
运行
复制
    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);
    }
}

这是一段简单的代码,用于显示银行名称和金额。我想为IF ELSE添加逻辑。请用代码指导我。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-02 16:44:08

使用此代码以balINRA格式显示金额

代码语言:javascript
运行
复制
    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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14118862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档