Olá, recentemente estudei a parte de passar dados pra uma outra Activity e estudei por fora em como trabalhar com Retrofit. Consegui passar os dados, mas a paginação dele está dando problema. Quando clico em um Pokémon do começo ele passa os dados, mas quando carrega mais dados e eu clico em outro o App fecha. Alguém saberia me ajudar?
Video que segui: https://www.youtube.com/watch?v=xQn8u4Htib4&t=67s
public class MainActivity extends AppCompatActivity {
private Retrofit retrofit;
private static final String TAG = "POKEDEX";
private RecyclerView recyclerView;
private ListaPokemonAdapter listaPokemonAdapter;
private int offset;
private boolean aptoCarregar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
listaPokemonAdapter = new ListaPokemonAdapter(this);
recyclerView.setAdapter(listaPokemonAdapter);
recyclerView.setHasFixedSize(true);
final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
int visibleItemCount = layoutManager.getChildCount();
int totalIntemCount = layoutManager.getItemCount();
int pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if (aptoCarregar) {
if ((visibleItemCount + pastVisibleItems) >= totalIntemCount) {
Log.i(TAG, "Carregado");
aptoCarregar = false;
offset += 20;
obterDados(offset);
}
}
}
}
});
retrofit = new Retrofit.Builder()
.baseUrl("https://pokeapi.co/api/v2/")
.addConverterFactory(GsonConverterFactory.create())
.build();
aptoCarregar = true;
offset = 0;
obterDados(offset);
}
private void obterDados(int offset) {
ApiService service = retrofit.create(ApiService.class);
Call<PokemonResultado> pokemonResultadoCall = service.obterListaPokemon(20, offset);
pokemonResultadoCall.enqueue(new Callback<PokemonResultado>() {
@Override
public void onResponse(Call<PokemonResultado> call, Response<PokemonResultado> response) {
aptoCarregar = true;
if (response.isSuccessful()) {
PokemonResultado pokemonResultado = response.body();
final ArrayList<Pokemon> listaPokemon = pokemonResultado.getResults();
listaPokemonAdapter.adicionarPokemon(listaPokemon);
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Pokemon p = listaPokemon.get(position);
String aString = Integer.toString(p.getNumber());
int aInt = Integer.parseInt(aString);
Intent i = new Intent(MainActivity.this, DetailActivity.class);
i.putExtra("ID", aInt);
i.putExtra("capa", aString);
startActivity(i);
}
@Override
public void onLongItemClick(View view, int position) {
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}));
} else {
Log.e(TAG, "onResponse" + response.errorBody());
}
}
@Override
public void onFailure(Call<PokemonResultado> call, Throwable t) {
aptoCarregar = true;
Log.e(TAG, "onErro" + t.getMessage());
}
});
}
}