正在学习Android MVP,不确定我在做什么错

问题描述 投票:0回答:1

我正在学习Android MVP。我建立了一个使用RecyclerView,Retrofit的简单应用程序,并且正在使用免费的Pokemon API来加载Pokemon名称。下面是我的代码。请让我知道我在做什么错。应用运行时出现黑屏。以下是我的类和XML]

主要活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

RecyclerView项目xml。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
    android:id="@+id/pokemon_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="50dp"
    android:textSize="25sp">
    </TextView>

</LinearLayout>

MainActivity:

public class MainActivity extends AppCompatActivity implements MainContract.MainView {

    RecyclerView recyclerView;
    RVAdapter rvAdapter;
    MainContract.MainPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainPresenter = new MainPresenter(this);

    recyclerView = findViewById(R.id.main_rv);
    RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(manager);
    mainPresenter.getData();
    }

    @Override
    public void loadDataInList(List<Pokemon> pokemonList) {
    rvAdapter = new RVAdapter(pokemonList);
    recyclerView.setAdapter(rvAdapter);

    }
}

Presenter MainContract的接口:

public interface MainContract {
    interface MainView{
       void loadDataInList(List<Pokemon> pokemonList);

    }
    interface MainPresenter {
       void getData();
    }
}

Presenter类MyPresenter:

public class MyPresenter implements MainContract.MainPresenter {

    MainContract.MainView mainView;

    String  BASE_URL = "https://pokeapi.co/api/v2/";

    public MyPresenter(MainContract.MainView mainView) {
    this.mainView = mainView;
    }

    @Override
    public void getData() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    PokeService pokeService = retrofit.create(PokeService.class);
    Call<PokemonResponse> call = pokeService.getPokemons();
    call.enqueue(new Callback<PokemonResponse>() {
        @Override
        public void onResponse(Call<PokemonResponse> call, Response<PokemonResponse> response) {
        if (response.isSuccessful()){
            PokemonResponse pokemonResponse = response.body();

            ArrayList<Pokemon> pokemonArrayList = (ArrayList<Pokemon>) pokemonResponse.getResults();

            mainView.loadDataInList(pokemonArrayList);
         }else
            Log.e(TAG, "onResponse: " + response.errorBody());
        }

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

        }
    });

    }
}

型号

public class PokemonResponse {

    int count;
    String next;
    boolean previous;
    private ArrayList<Pokemon> results;

    public int getCount() {
    return count;
    }

    public void setCount(int count) {
    this.count = count;
    }

    public String getNext() {
    return next;
    }

    public void setNext(String next) {
    this.next = next;
    }

    public boolean isPrevious() {
    return previous;
    }

    public void setPrevious(boolean previous) {
    this.previous = previous;
    }

    public List<Pokemon> getResults() {
    return results;
    }

    public void setResults(ArrayList<Pokemon> results) {
    this.results = results;
    }
}

型号

public class Pokemon {
    private String name;

    public Pokemon(String name) {
    this.name = name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }
}

API服务:

public interface PokeService {

    @GET("/pokemon")
    Call<PokemonResponse> getPokemons();
}

RecyclerView适配器

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PokemonNamesViewHolder> {

    List<Pokemon> pokemonList;



    public RVAdapter(List<Pokemon> pokemonList) {
    this.pokemonList = pokemonList;
    }

    @Override
    public PokemonNamesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.rv_item, parent, false);
    return new  PokemonNamesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PokemonNamesViewHolder holder, int position) {
    Pokemon pokemon = pokemonList.get(position);
    holder.textView.setText(pokemon.getName());

    }

    @Override
    public int getItemCount() {
    return pokemonList.size();
    }

    public class PokemonNamesViewHolder extends   RecyclerView.ViewHolder{
    public TextView textView;


    public PokemonNamesViewHolder( View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.pokemon_name);
    }
    }
}

应用程序不会崩溃,我已在清单中设置了Internet权限,并在gradle中添加了依赖项。

json对象:

{
    "count": 964,
    "next": "https://pokeapi.co/api/v2/pokemon?offset=20&limit=20",
    "previous": null,
    "results": [
        {
            "name": "bulbasaur",
            "url": "https://pokeapi.co/api/v2/pokemon/1/"
        },
        {
            "name": "ivysaur",
            "url": "https://pokeapi.co/api/v2/pokemon/2/"
        },
        {
            "name": "venusaur",
            "url": "https://pokeapi.co/api/v2/pokemon/3/"
        }

    ]
}
java android android-recyclerview retrofit mvp
1个回答
-1
投票

据我所知,当将loadData设置为recyclerview时,您执行了类似的操作>]

    @Override
    public void loadDataInList(List<Pokemon> pokemonList) {
        rvAdapter = new RVAdapter(pokemonList);
        recyclerView.setAdapter(rvAdapter);
    }

此步骤仅意味着您将适配器设置为您的recyclerview,但是忘记了通知您recyclerview数据已更改。

您可以在设置适配器行进行测试之后添加rvAdapter.notifyDataChanged()

之类的东西。
© www.soinside.com 2019 - 2024. All rights reserved.