• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Linking databse entry to an Imagebutton view

Lebronest

Lurker
I am developing an app to display player stats when user clicks on an Imagebutton corresponding to the respective player. I want statistics to modify when I update them in SQLite database. My query is, how should I link them? I am searching for something relevant for a week now.

Please help.

This is my update activity
Code:
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bSQLUpdate:
            Boolean didItWork = true;
            try {
                String name = sqlName.getText().toString();
                String goal = sqlGoals.getText().toString();
                Goals entry = new Goals(SQLiteExampleChelsea.this);
                entry.open();
                entry.createEntry(name, goal);
                entry.close();
            } catch (Exception e) {
                didItWork = false;
                String error = e.toString();

                Dialog d = new Dialog(this);
                d.setTitle("Dang it!");
                TextView tv = new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
            } finally {
                if (didItWork) {
                    Dialog d = new Dialog(this);
                    d.setTitle("Yea!");
                    TextView tv = new TextView(this);
                    tv.setText("success");
                    d.setContentView(tv);
                    d.show();
                }
            }
            break;

        case R.id.bSQLOpenView:
            Intent  i = new Intent("com.akshay.SoccerPlus.SQLVIEWCHELSEA");
            startActivity(i);
            break;

        case R.id.bgetInfo:
            String s = sqlJersey.getText().toString();
            long l = Long.parseLong(s);
            Goals gol = new Goals(this);
            gol.open();
            String returnedName = gol.getName(l);
            String returnedGoals = gol.getGoals(l);
            gol.close();

            sqlName.setText(returnedName);
            sqlGoals.setText(returnedGoals);
            break;
        case R.id.bSQLmodify:
            break;
        case R.id.bSQLdelete:
            break;
        }

    }
This is my database Activity
Code:
 private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_JERSEYID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " TEXT NOT NULL, " + KEY_GOALS + " TEXT NOT NULL);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
            // TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE );
onCreate(db);
        }

    }

    public Goals(Context c) {
        ourContext = c;

    }
    public Goals open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        
        return this;
    }
public void close(){
    ourHelper.close();
}
public long createEntry(String name, String goal) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name); 
    cv.put(KEY_GOALS, goal);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
     
}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_JERSEYID,KEY_NAME,KEY_GOALS
    };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = ""; 
    int iJersey = c.getColumnIndex(KEY_JERSEYID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iGoals = c.getColumnIndex(KEY_GOALS);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
         result = result + c.getString(iJersey) + " " + c.getString(iName) + " " + c.getString(iGoals) + "\n";
    }

    
    return result;
}
public String getName(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_JERSEYID,KEY_NAME,KEY_GOALS
    };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_JERSEYID + "=" + l, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String name = c.getString(1);
        return name;
        
        
        
    }
    return null;
}
public String getGoals(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_JERSEYID,KEY_NAME,KEY_GOALS
    };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_JERSEYID + "=" + l, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String goals = c.getString(2);
        return goals;
        
    }
    return null;
}
}
Suppose I chose Player A by clicking on its imagebutton and it displayed its name jersey no. and goals, but how can i link these attributes to the database activity? and enable changes if made, when the database is modified.
Also what should I use to display those attributes when user clicks on imagebutton, TextView?
 
Back
Top Bottom