sakibguy / worked-issues

arget medi a[rpescriptionmedicie] &ssets[dr] (reeeverse._ax:p)
2 stars 0 forks source link

[ORG] RU-MAKEAPP: java.lang.IndexOutOfBoundsException: Index: 4, Size: 1 #61

Closed sakibguy closed 1 year ago

sakibguy commented 1 year ago

Render issue on UI when complex api query

All data received 1 time api call

attention 1 (called muti times api/endpoints)

for (int i = 0; i < liveWords.size(); i++) {
                        int index = i;
                        getOddsData(liveWords.get(index).getGameId(), index);
                    }

attention 2 (err)

modelLives.add(new ModelLive(
                        "" + liveWords.get(index).getLeagueName(),
                        "" + liveWords.get(index).getTeam1Name(),
                        "" + liveWords.get(index).getTeam2Name(),
                        "" + liveWords.get(index).getScore(),
                        "" + liveWords.get(index).getGameId(),
                        "" + oddsWords.get(index).getHome_od(),
                        "" + oddsWords.get(index).getDraw_od(),
                        "" + oddsWords.get(index).getAway_od(),
                        "" + oddsWords.get(index).getHandicap(),
                        "" + oddsWords.get(index).getOver_od(),
                        "" + oddsWords.get(index).getUnder_od()
                ));

Code snippets nested complex api call

getData()

ArrayList<LiveWord> liveWords;
    LiveGames games;
    public LiveGames getData() {
        retrofit2.Call<LiveGames> liveGames = LiveAPI.getService().getLiveGames();
        liveGames.enqueue(new Callback<LiveGames>() {
            @Override
            public void onResponse(Call<LiveGames> call, Response<LiveGames> response) {
                liveWords = null;
                liveWords = new ArrayList<LiveWord>();
                games = response.body();

                Toast.makeText(getActivity().getApplicationContext(), "Live data showing", Toast.LENGTH_LONG).show();

                for (int i = 0; i < games.getGamesLive().size(); i++) {
                    Log.d(TAG, "onCreateView: ----ok"+ i + "---- " + games.getGamesLive().get(i).getLeague().getName() + ", home_away: " + games.getGamesLive().get(i).getHome().getName() + ", " + games.getGamesLive().get(i).getAway().getName() + ", game_id: " + games.getGamesLive().get(i).getGameId());

                    liveWords.add(new LiveWord(
                            "" + games.getGamesLive().get(i).getLeague().getName(),
                            "" + games.getGamesLive().get(i).getHome().getName(),
                            "" + games.getGamesLive().get(i).getAway().getName(),
                            "" + games.getGamesLive().get(i).getScore(),
                            "" + games.getGamesLive().get(i).getGameId(),
                            R.drawable.number_one,
                            R.raw.number_one));
                }

                // Clear the adapter of previous earthquake data
                adapter.clear();

                // If there is a valid list of {@link Earthquake}s, then add them to the adapter's
                // data set. This will trigger the ListView to update.
                if (liveWords != null && !liveWords.isEmpty()) {
                    // mAdapter.addAll(earthquakes);    // Blink "No earthquakes found."
                    // Request odds data

                    for (int i = 0; i < liveWords.size(); i++) {
                        int index = i;
                        getOddsData(liveWords.get(index).getGameId(), index);
                    }
                    //adapter.addAll(liveWords);
                }

            }

            @Override
            public void onFailure(Call<LiveGames> call, Throwable t) {

            }
        });
        return games;
    }

GetOddsData()

    ArrayList<ModelLive> modelLives = new ArrayList<ModelLive>();
    ArrayList<OddsWord> oddsWords = new ArrayList<OddsWord>();
    OddsList oddsList;
    public void getOddsData(String gameId, int index) {
        String url = "https://spoyer.com/api/en/get.php?login=ayna&token=12784-OhJLY5mb3BSOx0O&task=odds&game_id=" + gameId;

        retrofit2.Call<OddsList> oddsListCall = OddsAPI.getService().getOddsList(url);
        oddsListCall.enqueue(new Callback<OddsList>() {
            @Override
            public void onResponse(Call<OddsList> call, Response<OddsList> response) {
                //oddsWords = null;
                //oddsWords = new ArrayList<OddsWord>();
                oddsList = response.body();

                Toast.makeText(getActivity().getApplicationContext(), "Live data showing", Toast.LENGTH_LONG).show();

//                Log.d(TAG, "getOddsData: ----" +
//                        "gameId==="+ gameId +
//                        ", 1 home_od=" + oddsList.getOdds().getBet365().getLive().get(0).getHomeOd() +
//                        ", x draw_od=" + oddsList.getOdds().getBet365().getLive().get(0).getDrawOd() +
//                        ", 2 away_od=" + oddsList.getOdds().getBet365().getLive().get(0).getAwayOd() +
//                        ", 1x over_od=" + oddsList.getOdds().getBet365().getLive().get(2).getOverOd() +
//                        ", 12 handicap=" + oddsList.getOdds().getBet365().getLive().get(2).getHandicap() +
//                        ", 2x under_od=" + oddsList.getOdds().getBet365().getLive().get(2).getUnderOd());

                oddsWords.add(new OddsWord(
                        "" + gameId,
                        "" + oddsList.getOdds().getBet365().getLive().get(0).getHomeOd(), "" + oddsList.getOdds().getBet365().getLive().get(0).getDrawOd(),
                        "" + oddsList.getOdds().getBet365().getLive().get(0).getAwayOd(),
                        "" + oddsList.getOdds().getBet365().getLive().get(2).getHandicap(),
                        "" + oddsList.getOdds().getBet365().getLive().get(2).getOverOd(),
                        "" + oddsList.getOdds().getBet365().getLive().get(2).getUnderOd()));

                for (int i = 0; i < oddsWords.size(); i++) {
                    Log.d(TAG, "OddsWord: ----" +
                            "gameId==="+ oddsWords.get(i).getGame_id() +
                            ", 1 home_od=" + oddsWords.get(i).getHome_od() +
                            ", x draw_od=" + oddsWords.get(i).getDraw_od() +
                            ", 2 away_od=" + oddsWords.get(i).getAway_od() +
                            ", 12 handicap=" + oddsWords.get(i).getHandicap() +
                            ", 1x over_od=" + oddsWords.get(i).getOver_od() +
                            ", 2x under_od=" + oddsWords.get(i).getUnder_od());
                }

                if (oddsWords.size() == liveWords.size()){
                    Log.d(TAG, "size: =");
                }

                // String leagueName, String team1Name, String team2Name, String score, String game_id, String home_od, String draw_od, String away_od, String handicap, String over_od, String under_od
                modelLives.add(new ModelLive(
                        "" + liveWords.get(index).getLeagueName(),
                        "" + liveWords.get(index).getTeam1Name(),
                        "" + liveWords.get(index).getTeam2Name(),
                        "" + liveWords.get(index).getScore(),
                        "" + liveWords.get(index).getGameId(),
                        "" + oddsWords.get(index).getHome_od(),
                        "" + oddsWords.get(index).getDraw_od(),
                        "" + oddsWords.get(index).getAway_od(),
                        "" + oddsWords.get(index).getHandicap(),
                        "" + oddsWords.get(index).getOver_od(),
                        "" + oddsWords.get(index).getUnder_od()
                ));

                // keyboard Clear the adapter of previous earthquake data
                adapter.clear();

                // If there is a valid list of {@link Earthquake}s, then add them to the adapter's
                // data set. This will trigger the ListView to update.
                if (modelLives != null && !modelLives.isEmpty()) {
                    // mAdapter.addAll(earthquakes);    // Blink "No earthquakes found."
                    adapter.addAll(modelLives);
                }
            }

            @Override
            public void onFailure(Call<OddsList> call, Throwable t) {

            }
        });
    }
sakibguy commented 1 year ago

Partially solved (not perfectly)

Checked oddsWords = liveWords. If same then initialized ModelLive class to hold all values it needed to render on UI.

Logic

if (oddsWords.size() == liveWords.size()){
Log.d(TAG, "size: =");
}

Init part


if (oddsWords.size() == liveWords.size()){
Log.d(TAG, "size: =");
                // String leagueName, String team1Name, String team2Name, String score, String game_id, String home_od, String draw_od, String away_od, String handicap, String over_od, String under_od
                modelLives.add(new ModelLive(
                        "" + liveWords.get(index).getLeagueName(),
                        "" + liveWords.get(index).getTeam1Name(),
                        "" + liveWords.get(index).getTeam2Name(),
                        "" + liveWords.get(index).getScore(),
                        "" + liveWords.get(index).getGameId(),
                        "" + oddsWords.get(index).getHome_od(),
                        "" + oddsWords.get(index).getDraw_od(),
                        "" + oddsWords.get(index).getAway_od(),
                        "" + oddsWords.get(index).getHandicap(),
                        "" + oddsWords.get(index).getOver_od(),
                        "" + oddsWords.get(index).getUnder_od()
                ));

                // keyboard Clear the adapter of previous earthquake data
                adapter.clear();

                // If there is a valid list of {@link Earthquake}s, then add them to the adapter's
                // data set. This will trigger the ListView to update.
                if (modelLives != null && !modelLives.isEmpty()) {
                    // mAdapter.addAll(earthquakes);    // Blink "No earthquakes found."
                    adapter.addAll(modelLives);
                }
            }


## 

## Snap
![1](https://user-images.githubusercontent.com/24685590/184166481-1834f4cb-3f49-4f14-8c16-a9e0342df774.PNG)
sakibguy commented 1 year ago

Detected bug

From above logical mistake detected on initialization. Init called only once!

Solved

1