这是一个基本的问题--但我是Android和Java的新手,所以请帮助我。我试图从一个数据库(SQLite)中检索所有行,并将其显示在屏幕上。到目前为止,对于简单的检索和显示,它工作得很好。
但现在,我想添加一些逻辑。如果数组中的某个字段等于某个值,那么我想执行一些操作,然后在屏幕上显示该字段。
更详细的银行表-有银行名称,银行余额,银行币种。我想在一个变量中读取Bank Currency,如果它是USD,则按原样显示Bank Balance。如果它是INR,那么我想将它转换为Bank Balance * 55,然后显示新的余额。
以下是我当前的代码
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;
}这就是我要调用这个数组的java类
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添加逻辑。请用代码指导我。谢谢!
发布于 2013-01-02 16:44:08
使用此代码以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);https://stackoverflow.com/questions/14118862
复制相似问题