これは基本的な質問ですが、私はAndroidとJavaを初めて使用するので、助けてください。DB(SQLite)からすべての行を取得して、画面に表示しようとしています。これまでのところ、単純な検索と表示には問題なく機能しています。
しかし今、私はロジックを追加したいと思います。配列内の特定のフィールドが値と等しい場合、何らかの操作を実行してから、そのフィールドを画面に表示します。
より詳細な銀行テーブル-銀行名、銀行残高、銀行通貨があります。銀行通貨を変数で読み取りたいのですが、米ドルの場合は、銀行残高をそのまま表示します。INRの場合は、銀行残高* 55に変換して、新しい残高を表示します。
これが私の現在のコードです
DBヘルパー
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);
}
}
これは、銀行名と金額をそのまま正確に表すための単純なコードです。IFELSEのロジックを追加したいと思います。コードを教えてください。ありがとう!