簡體   English   中英

學習 Android 的 MVP,不知道我做錯了什么

[英]Learning MVP for Android and not sure what I am doing wrong

我正在學習 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>

回收商查看項目 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>

主要活動:

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();
    }
}

演示者 class 我的演示者:

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 object:

{
    "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/"
        }

    ]
}

據我所知,當您將 loadData 設置為 recyclerview 時,您執行了類似的操作

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

此步驟僅意味着您將適配器設置為您的 recyclerview,但是您忘記通知您 recyclerview 數據已更改。

您可以在 setadapter 行之后添加類似rvAdapter.notifyDataChanged()的內容來測試它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM