-1

I am attempting to update information in an SQLite Database. The information being read by my getter is correct, but whenever I go to update the information within the record, i.e. League Name, Basis Score, etc.. I am being to told that the size of the array is 0.

I can see in the debug that I am calling the correct record id and the information that is being pulled from the database is correct. This is confirming to me that the record is indeed the correct one.

I can see in the debug that the information is correct and the variables are storing the changes made to the record.

Before changes are made to the data

After changes are made to the data

Can't see to figure out what it is I am doing incorrectly. I have gone through several different posts on this site, including this one.What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

I cannot seem to find anything that will assist me with resolving this. Can someone assist me with this please, I have posted my code below.

My code to get the record upon entering the Activity

public void getLeagueProfile(int position) {


        SQLiteOpenHelper database = new DatabaseHelper(this);
        SQLiteDatabase db = database.getReadableDatabase();

        Cursor viewLeagueProfile = db.query(League.TABLE_NAME, new String[]{League.COLUMN_ID, League.COLUMN_NAME, League.COLUMN_BASE_SCORE, League.COLUMN_BASE_SCORE_PERCENTAGE, League.COLUMN_WINS, League.COLUMN_LOSSES, League.COLUMN_TIMESTAMP}, League.COLUMN_ID + "=?", new String[]{String.valueOf(position)}, null, null, null, null);

        if (viewLeagueProfile.moveToFirst()) {

            //Prepare League Object
            league = new League(viewLeagueProfile.getInt(viewLeagueProfile.getColumnIndex(League.COLUMN_ID)), ln = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_NAME)), bs = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE)), bsp = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE_PERCENTAGE)), lw = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_WINS)), ll = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_LOSSES)), viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_TIMESTAMP)));

            Log.e("values : ", ln + ", " + bs + ", " + bsp + ", " + lw + ", " + ll);

            final EditText leagueName = findViewById(R.id.etLeagueName);
            final EditText basisScore = findViewById(R.id.etBasisScoreValue);
            final EditText basisScorePercentage = findViewById(R.id.etBasisScorePercentageValue);
            final EditText leagueWins = findViewById(R.id.etLeaguePointsWon);
            final EditText leagueLosses = findViewById(R.id.etLeaguePointsLost);

            leagueName.setText(ln);
            basisScore.setText(bs);
            basisScorePercentage.setText(bsp);
            leagueWins.setText(lw);
            leagueLosses.setText(ll);

            Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(viewLeagueProfile));

            //Close Database Connection
            viewLeagueProfile.close();
        }
    }

My code to update the record in the database when Save is clicked

private void updateLeague(String ln, String bs, String bsp, String lw, String ll, final int position) {

        League n = leaguesList.get(position);

        //Updating League Text
        n.setName(ln);
        n.setBaseScore(bs);
        n.setBaseScorePercentage(bsp);
        n.setWins(lw);
        n.setLosses(ll);

        //Updating The League In The Database
        db.updateLeague(n);

        //Refreshing The List
        leaguesList.set(position, n);
        mAdapter.notifyItemChanged(position);
    }

My Update function in my DatabaseHelper.java

public int updateLeague(League league) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(League.COLUMN_NAME, league.getName());
        values.put(League.COLUMN_BASE_SCORE, league.getBaseScore());
        values.put(League.COLUMN_BASE_SCORE_PERCENTAGE, league.getBaseScorePercentage());
        values.put(League.COLUMN_WINS, league.getWins());
        values.put(League.COLUMN_LOSSES, league.getLosses());

        //Updating Row
        return db.update(League.TABLE_NAME, values, League.COLUMN_ID + " = ?",
                new String[]{String.valueOf(league.getId())});
    }

The debug console log

09/27 16:29:25: Launching app
No apk changes detected since last installation, skipping installation of D:\Documents\Android Studio Project\tpbcUI\app\build\outputs\apk\debug\app-debug.apk
$ adb shell am force-stop ca.rvogl.tpbcui
$ adb shell am start -n "ca.rvogl.tpbcui/ca.rvogl.tpbcui.views.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: ca.rvogl.tpbcui | ca.rvogl.tpbcui.test
Waiting for application to come online: ca.rvogl.tpbcui | ca.rvogl.tpbcui.test
Connecting to ca.rvogl.tpbcui
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Debugger is active
I/System.out: Debugger has connected
    waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8635', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1341)
W/System: ClassLoader referenced unknown path: /data/app/ca.rvogl.tpbcui-2/lib/x86
D/: HostConnection::get() New Host Connection established 0x9cfff640, tid 6650
D/: HostConnection::get() New Host Connection established 0x9bcdf080, tid 6666
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xa0485120: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/ViewConfigCompat: Could not find method getScaledScrollFactor() on ViewConfiguration
D/id: 4
D/leagueId: 4
D/id: 3
D/leagueId: 3
I/art: Do partial code cache collection, code=12KB, data=19KB
I/art: After code cache collection, code=12KB, data=19KB
    Increasing code cache capacity to 128KB
I/art: Do partial code cache collection, code=12KB, data=38KB
I/art: After code cache collection, code=12KB, data=38KB
    Increasing code cache capacity to 256KB
I/art: JIT allocated 60KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
    Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
D/id: 2
D/leagueId: 2
D/id: 1
D/leagueId: 1
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
I/art: Do full code cache collection, code=81KB, data=55KB
I/art: Starting a blocking GC JitCodeCache
I/art: After code cache collection, code=79KB, data=43KB
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/leagueId: 0
E/values :: Saturday Practice Bowling, 190, 100, 0, 0
V/Cursor Object: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@ae7b8b5
    0 {
       _id=4
       name=Saturday Practice Bowling
       basescore=190
       basescorepercentage=100
       wins=0
       losses=0
       timestamp=2018-09-20 17:58:16
    }
    <<<<<
E/values :: Saturday Practice Bowling, 190, 100, 0, 0
I/art: Do partial code cache collection, code=118KB, data=89KB
I/art: After code cache collection, code=115KB, data=87KB
    Increasing code cache capacity to 512KB
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
D/OpenGLRenderer: endAllActiveAnimators on 0x8b721c80 (MenuPopupWindow$MenuDropDownListView) with handle 0x8cdb7740
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)
I/art: Starting a blocking GC Instrumentation
E/values :: Saturday Practice Bowling, 190, 100, 0, 0
V/Cursor Object: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@66308ae
    0 {
       _id=4
       name=Saturday Practice Bowling
       basescore=190
       basescorepercentage=100
       wins=0
       losses=0
       timestamp=2018-09-20 17:58:16
    }
    <<<<<
I/art: JIT allocated 61KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/art: Do full code cache collection, code=245KB, data=147KB
    Starting a blocking GC JitCodeCache
    After code cache collection, code=175KB, data=97KB
D/EGL_emulation: eglMakeCurrent: 0xa0485120: ver 3 1 (tinfo 0xa0483130)

If I missed posting something could help with resolving this please let me know and I will gladly post it.

2018-09-27 16:08:09.615 5820-5820/ca.rvogl.tpbcui E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ca.rvogl.tpbcui, PID: 5820
    java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at ca.rvogl.tpbcui.views.LeagueProfileEditActivity.updateLeague(LeagueProfileEditActivity.java:189)
        at ca.rvogl.tpbcui.views.LeagueProfileEditActivity.access$000(LeagueProfileEditActivity.java:28)
        at ca.rvogl.tpbcui.views.LeagueProfileEditActivity$1.onClick(LeagueProfileEditActivity.java:124)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)

I am getting it on this line League n = leaguesList.get(position);

Activity Code:

public class LeagueProfileEditActivity extends AppCompatActivity {

    League league;
    public List<League> leaguesList = new ArrayList<>();
    private LeagueAdapter mAdapter;

    public DatabaseHelper db;

    private static final String PREFS_NAME = "prefs";
    private static final String PREF_BLUE_THEME = "blue_theme";
    private static final String PREF_GREEN_THEME = "green_theme";
    private static final String PREF_ORANGE_THEME = "purple_theme";
    private static final String PREF_RED_THEME = "red_theme";
    private static final String PREF_YELLOW_THEME = "yellow_theme";

    public String ln, bs, bsp, lw, ll;
    public int leagueId;

    @Override
    protected void onResume() {
        super.onResume();
        db = new DatabaseHelper(this);
        //mAdapter.notifyDatasetChanged(db.getAllLeagues());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Use Chosen Theme
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean useBlueTheme = preferences.getBoolean(PREF_BLUE_THEME, false);
        if (useBlueTheme) {
            setTheme(R.style.AppTheme_Blue_NoActionBar);
        }
        boolean useGreenTheme = preferences.getBoolean(PREF_GREEN_THEME, false);
        if (useGreenTheme) {
            setTheme(R.style.AppTheme_Green_NoActionBar);
        }
        boolean useOrangeTheme = preferences.getBoolean(PREF_ORANGE_THEME, false);
        if (useOrangeTheme) {
            setTheme(R.style.AppTheme_Orange_NoActionBar);
        }
        boolean useRedTheme = preferences.getBoolean(PREF_RED_THEME, false);
        if (useRedTheme) {
            setTheme(R.style.AppTheme_Red_NoActionBar);
        }
        boolean useYellowTheme = preferences.getBoolean(PREF_YELLOW_THEME, false);
        if (useYellowTheme) {
            setTheme(R.style.AppTheme_Yellow_NoActionBar);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_league_profile_edit);

        mAdapter = new LeagueAdapter(this, leaguesList);

        String savedLeagueId = String.valueOf(getIntent().getIntExtra("leagueId",2));
        leagueId = Integer.valueOf(savedLeagueId);

        getLeagueProfile(leagueId);

        final Button save_button = (Button) findViewById(R.id.bSave);
        save_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                final EditText leagueName = findViewById(R.id.etLeagueName);
                final EditText basisScore = findViewById(R.id.etBasisScoreValue);
                final EditText basisScorePercentage = findViewById(R.id.etBasisScorePercentageValue);
                final EditText leagueWins = findViewById(R.id.etLeaguePointsWon);
                final EditText leagueLosses = findViewById(R.id.etLeaguePointsLost);

                int leagueId = league.getId();

                updateLeague(league, leagueName.getText().toString(), basisScore.getText().toString(), basisScorePercentage.getText().toString(), leagueWins.getText().toString(), leagueLosses.getText().toString(), leagueId);

                Intent intent = new Intent(getApplicationContext(), LeagueProfileViewActivity.class);
                intent.putExtra("leagueId", leagueId);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);

            }

        });

        final Button cancel_button = (Button) findViewById(R.id.bCancel);
        cancel_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int leagueId = league.getId();
                Intent intent = new Intent(getApplicationContext(), LeagueProfileViewActivity.class);
                intent.putExtra("leagueId", leagueId);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);
            }

        });

    }

    public void getLeagueProfile(int position) {


        SQLiteOpenHelper database = new DatabaseHelper(this);
        SQLiteDatabase db = database.getReadableDatabase();

        Cursor viewLeagueProfile = db.query(League.TABLE_NAME, new String[]{League.COLUMN_ID, League.COLUMN_NAME, League.COLUMN_BASE_SCORE, League.COLUMN_BASE_SCORE_PERCENTAGE, League.COLUMN_WINS, League.COLUMN_LOSSES, League.COLUMN_TIMESTAMP}, League.COLUMN_ID + "=?", new String[]{String.valueOf(position)}, null, null, null, null);

        if (viewLeagueProfile.moveToFirst()) {

            //Prepare League Object
            league = new League(viewLeagueProfile.getInt(viewLeagueProfile.getColumnIndex(League.COLUMN_ID)), ln = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_NAME)), bs = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE)), bsp = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE_PERCENTAGE)), lw = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_WINS)), ll = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_LOSSES)), viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_TIMESTAMP)));

            Log.e("values : ", ln + ", " + bs + ", " + bsp + ", " + lw + ", " + ll);

            final EditText leagueName = findViewById(R.id.etLeagueName);
            final EditText basisScore = findViewById(R.id.etBasisScoreValue);
            final EditText basisScorePercentage = findViewById(R.id.etBasisScorePercentageValue);
            final EditText leagueWins = findViewById(R.id.etLeaguePointsWon);
            final EditText leagueLosses = findViewById(R.id.etLeaguePointsLost);

            leagueName.setText(ln);
            basisScore.setText(bs);
            basisScorePercentage.setText(bsp);
            leagueWins.setText(lw);
            leagueLosses.setText(ll);

            Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(viewLeagueProfile));

            //Close Database Connection
            viewLeagueProfile.close();
        }

    }

        private void updateLeague(final League league, String ln, String bs, String bsp, String lw, String ll, final int position) {

        League n = leaguesList.get(position);

        //Updating League Text
        n.setName(ln);
        n.setBaseScore(bs);
        n.setBaseScorePercentage(bsp);
        n.setWins(lw);
        n.setLosses(ll);

        //Updating The League In The Database
        db.updateLeague(n);

        //Refreshing The List
        leaguesList.set(position, n);
        mAdapter.notifyItemChanged(position);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.league_profile_edit_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_cancel) {
            /*Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            overridePendingTransition(0, 0);*/
            return true;
        }


        return super.onOptionsItemSelected(item);
    }
}
MikeT
  • 51,415
  • 16
  • 49
  • 68
Robert Vogl
  • 250
  • 5
  • 19
  • Please include where you are getting the Array Out of Bounds and the stack-trace when you get it. – MikeT Sep 27 '18 at 21:12
  • @MikeT I posted the logcat and the particular line it is failing on above. – Robert Vogl Sep 27 '18 at 21:26
  • It seems you are never populating `leaguesList`. `getLeagueProfile` creates a new `League` Object but its not set/saved to `leaguesList` there, right? – makadev Sep 27 '18 at 21:29
  • leaguelist is empty as per `League n = leaguesList.get(position); ` you need to populate it. – MikeT Sep 27 '18 at 21:30
  • So `leaguesList` is causing the exception.... But your code doesn't include where you declare / populate this variable.. – dustytrash Sep 27 '18 at 22:30
  • @MikeT Isn't the following code populating leagueList `League league; private List leaguesList = new ArrayList<>(); private LeagueAdapter mAdapter;` – Robert Vogl Sep 27 '18 at 23:30

1 Answers1

1

You issue is that leaguesList is an empty List, thus League n = leaguesList.get(position); is trying to get the 4th element out of 0 elements. Hence the exception.

@MikeT Isn't the following code populating leagueList League league; private List leaguesList = new ArrayList<>(); private LeagueAdapter mAdapter;

NO. League league; (with class scope) has nothing to do with private List leaguesList = new ArrayList<>(); other than they are of the same type. So using getLeagueProfile(leagueId); just sets(instantiates) the league variable/object.

I don't believe that you need leaguesList nor mAdapter. I believe you need the following (i.e. just use the single league variable):-

public class LeagueProfileEditActivity extends AppCompatActivity {

    League league;
    //public List<League> leaguesList = new ArrayList<>(); //<<<<<<<<<< DELETED
    //private LeagueAdapter mAdapter; //<<<<<<<<<< DELETED

    public DatabaseHelper db;

    private static final String PREFS_NAME = "prefs";
    private static final String PREF_BLUE_THEME = "blue_theme";
    private static final String PREF_GREEN_THEME = "green_theme";
    private static final String PREF_ORANGE_THEME = "purple_theme";
    private static final String PREF_RED_THEME = "red_theme";
    private static final String PREF_YELLOW_THEME = "yellow_theme";

    public String ln, bs, bsp, lw, ll;
    public int leagueId;

    @Override
    protected void onResume() {
        super.onResume();
        db = new DatabaseHelper(this);
        //mAdapter.notifyDatasetChanged(db.getAllLeagues()); //<<<<<<<<<< Not needed
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Use Chosen Theme
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean useBlueTheme = preferences.getBoolean(PREF_BLUE_THEME, false);
        if (useBlueTheme) {
            setTheme(R.style.AppTheme_Blue_NoActionBar);
        }
        boolean useGreenTheme = preferences.getBoolean(PREF_GREEN_THEME, false);
        if (useGreenTheme) {
            setTheme(R.style.AppTheme_Green_NoActionBar);
        }
        boolean useOrangeTheme = preferences.getBoolean(PREF_ORANGE_THEME, false);
        if (useOrangeTheme) {
            setTheme(R.style.AppTheme_Orange_NoActionBar);
        }
        boolean useRedTheme = preferences.getBoolean(PREF_RED_THEME, false);
        if (useRedTheme) {
            setTheme(R.style.AppTheme_Red_NoActionBar);
        }
        boolean useYellowTheme = preferences.getBoolean(PREF_YELLOW_THEME, false);
        if (useYellowTheme) {
            setTheme(R.style.AppTheme_Yellow_NoActionBar);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_league_profile_edit);

        //mAdapter = new LeagueAdapter(this, leaguesList); //<<<<<<<<<< not needed

        String savedLeagueId = String.valueOf(getIntent().getIntExtra("leagueId",2));
        leagueId = Integer.valueOf(savedLeagueId);

        getLeagueProfile(leagueId); //<<<<<<<<<< sets the values for league

        final Button save_button = (Button) findViewById(R.id.bSave);
        save_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                final EditText leagueName = findViewById(R.id.etLeagueName);
                final EditText basisScore = findViewById(R.id.etBasisScoreValue);
                final EditText basisScorePercentage = findViewById(R.id.etBasisScorePercentageValue);
                final EditText leagueWins = findViewById(R.id.etLeaguePointsWon);
                final EditText leagueLosses = findViewById(R.id.etLeaguePointsLost);

                int leagueId = league.getId();

                updateLeague(league, leagueName.getText().toString(), basisScore.getText().toString(), basisScorePercentage.getText().toString(), leagueWins.getText().toString(), leagueLosses.getText().toString(), leagueId);

                Intent intent = new Intent(getApplicationContext(), LeagueProfileViewActivity.class);
                intent.putExtra("leagueId", leagueId);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);

            }

        });

        final Button cancel_button = (Button) findViewById(R.id.bCancel);
        cancel_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int leagueId = league.getId();
                Intent intent = new Intent(getApplicationContext(), LeagueProfileViewActivity.class);
                intent.putExtra("leagueId", leagueId);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);
            }

        });

    }

    public void getLeagueProfile(int position) {


        SQLiteOpenHelper database = new DatabaseHelper(this);
        SQLiteDatabase db = database.getReadableDatabase();

        Cursor viewLeagueProfile = db.query(League.TABLE_NAME, new String[]{League.COLUMN_ID, League.COLUMN_NAME, League.COLUMN_BASE_SCORE, League.COLUMN_BASE_SCORE_PERCENTAGE, League.COLUMN_WINS, League.COLUMN_LOSSES, League.COLUMN_TIMESTAMP}, League.COLUMN_ID + "=?", new String[]{String.valueOf(position)}, null, null, null, null);

        if (viewLeagueProfile.moveToFirst()) {

            //Prepare League Object
            league = new League(viewLeagueProfile.getInt(viewLeagueProfile.getColumnIndex(League.COLUMN_ID)), ln = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_NAME)), bs = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE)), bsp = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_BASE_SCORE_PERCENTAGE)), lw = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_WINS)), ll = viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_LOSSES)), viewLeagueProfile.getString(viewLeagueProfile.getColumnIndex(League.COLUMN_TIMESTAMP)));

            Log.e("values : ", ln + ", " + bs + ", " + bsp + ", " + lw + ", " + ll);

            final EditText leagueName = findViewById(R.id.etLeagueName);
            final EditText basisScore = findViewById(R.id.etBasisScoreValue);
            final EditText basisScorePercentage = findViewById(R.id.etBasisScorePercentageValue);
            final EditText leagueWins = findViewById(R.id.etLeaguePointsWon);
            final EditText leagueLosses = findViewById(R.id.etLeaguePointsLost);

            leagueName.setText(ln);
            basisScore.setText(bs);
            basisScorePercentage.setText(bsp);
            leagueWins.setText(lw);
            leagueLosses.setText(ll);

            Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(viewLeagueProfile));

            //Close Database Connection
            viewLeagueProfile.close();
        }
    }

        private void updateLeague(final League league, String ln, String bs, String bsp, String lw, String ll, final int position) {

        //League n = leaguesList.get(position); //<<<<<<<<<<<<<<<<<<< ERROR

        //Updating League Object
        //n.setName(ln); //<<<<<<<<<< replaced by
        league.setName(ln)
        //n.setBaseScore(bs); //<<<<<<<<<< replaced by
        league.setBaseScore(bs);
        //n.setBaseScorePercentage(bsp); //<<<<<<<<<< replaced by
        league.setBaseScorePercentage(bsp);
        //n.setWins(lw); //<<<<<<<<<< replaced by
        league.setWinds(lw);
        //n.setLosses(ll); //<<<<<<<<<< replaced by
        league.setLosses(ll);

        //Updating The League In The Database
        db.updateLeague(league);

        //Refreshing The List
        //leaguesList.set(position, n); //<<<<<<<<<< DELETED NOT USED????
        //mAdapter.notifyItemChanged(position); //<<<<<<<<<< DELETED NOT USED????
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.league_profile_edit_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_cancel) {
            /*Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            overridePendingTransition(0, 0);*/
            return true;
        }


        return super.onOptionsItemSelected(item);
    }
}
  • Notes
  • The above is in-principle code, it has not been tested not syntax checked. So it may contain errors.
  • The above code has been written with no knowledge of the Options Menu content, so the codes makes no consideration for any impact that may have.
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • MikeT you are correct, the changes you made worked. How do you determine whether or not an Array is needed or not? I think this is where my problem is, I am unable to determine when one method is better suited versus another – Robert Vogl Sep 28 '18 at 14:02
  • If only dealing with one League, such as when editing or deleting a league then there is no need for a list. Generally you'd only need a list when presenting a list. So typically an App would display a list and allow a selection from that list so handling that selected item only then needs to deal with a single item. – MikeT Sep 28 '18 at 20:26
  • Thanks I will keep this in mind. What if I am creating a new league that never existed before would the same hold true editing or deleting a league? – Robert Vogl Sep 28 '18 at 20:33
  • Adding a new league would be similar to edit, except you don't need to pass anything to the add/insert activity. – MikeT Sep 28 '18 at 20:39
  • Thanks again MikeT – Robert Vogl Sep 28 '18 at 20:52