1

Intent: Retrieving single data from SQLite table and display in TextView

Case: Fail to retrieve data, and unable to show in mainactivity.

Expected Result: TextView displaying the current total bank balance in main activity (e.g. Total bal: $7600)

Note Please show me the code of mainactiviy on retrieving display data as well. Thanks in advance (and sorry for my bad english).

package com.fa.money;
import java.util.ArrayList;
import java.util.HashMap;
import android.util.Log;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBController  extends SQLiteOpenHelper {
private static final String LOGCAT = null;

public DBController(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 1);
    Log.d(LOGCAT,"Created");
}

@Override
public void onCreate(SQLiteDatabase database) {
    String query, query2;
//  query = "CREATE TABLE tbBank ( bankID INTEGER PRIMARY KEY, bankname TEXT)";
    query = "CREATE TABLE tbBank ( bankID INTEGER PRIMARY KEY AUTOINCREMENT, bankname TEXT, baccID TEXT, baccBal TEXT, remarks TEXT)";
    query2 = "CREATE TABLE tbMain (mainID INTEGER PRIMARY KEY AUTOINCREMENT, bankbal TEXT, loanbal TEXT)";
    database.execSQL(query);
   database.execSQL(query2);

    Log.d(LOGCAT,"Bank Account Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    String query,query2;
    query = "DROP TABLE IF EXISTS tbBank";
    query2 = "DROP TABLE IF EXISTS tbMain";
    database.execSQL(query);
    database.execSQL(query2);
    onCreate(database);
}

public void addBank(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("bankname", queryValues.get("bankname"));
    values.put("baccID", queryValues.get("bankaccid"));
    values.put("baccBal", queryValues.get("totalbal"));
    values.put("remarks", queryValues.get("remarks"));
    database.insert("tbBank", null, values);
    database.close();
}

public int updateBank(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();    
    ContentValues values = new ContentValues();
    values.put("bankname", queryValues.get("bankname"));
    return database.update("tbBank", values, "bankID" + " = ?", new String[] { queryValues.get("bankID") });
    //String updateQuery = "Update  words set txtWord='"+word+"' where txtWord='"+ oldWord +"'";
    //Log.d(LOGCAT,updateQuery);
    //database.rawQuery(updateQuery, null);
    //return database.update("words", values, "txtWord  = ?", new String[] { word });
}

public void deleteBank(String id) {
    Log.d(LOGCAT,"delete");
    SQLiteDatabase database = this.getWritableDatabase();    
    String deleteQuery = "DELETE FROM  tbBank where bankID='"+ id +"'";
    Log.d("query",deleteQuery);     
    database.execSQL(deleteQuery);
}

public ArrayList<HashMap<String, String>> getAllbank() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM tbBank";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("bankID", cursor.getString(0));
            map.put("bankname", cursor.getString(1));
            wordList.add(map);
        } while (cursor.moveToNext());
    }


    return wordList;
}

public HashMap<String, String> getBankinfo(String id) {
    HashMap<String, String> wordList = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM tbBank where bankID='"+id+"'";
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
                //HashMap<String, String> map = new HashMap<String, String>();
            wordList.put("bankname", cursor.getString(1));
               //wordList.add(map);
        } while (cursor.moveToNext());
    }                   
return wordList;
}
//getbankbal - ERROR here !
public String getBankbal() {
      String bankbalresult;
      SQLiteDatabase database = this.getReadableDatabase();
      String selectQuery = "SELECT bankbal FROM tbMain";
      Cursor cursor = database.rawQuery(selectQuery, null);
      if (cursor.moveToFirst()) {
        do {
            bankbalresult.put("bankbal", cursor.getString(1));
        } while (cursor.moveToNext());
      }           
      return bankbalresult;
    } }

1 Answer 1

11

I think you should try declaring bankbal as double rather than text. Anyways if you want it to be text Try doing this

Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor!=null && cursor.getCount()>0)
{
      cursor.moveToFirst()
        do {
            bankbalresult = cursor.getString(0);
        } while (cursor.moveToNext());
      }           
}
      return bankbalresult;
    } }

// In the activity where you want it displayed you can do

 TextView tv = new TextView();
    DBController info = new DBController(this);
    info.open();
    String data = info.getBankBal();
    info.close();
    tv.setText(data);
Sign up to request clarification or add additional context in comments.

Comments

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.