Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在学习Android的MVP,但不确定我做错了什么_Java_Android_Android Recyclerview_Retrofit_Mvp - Fatal编程技术网

Java 正在学习Android的MVP,但不确定我做错了什么

Java 正在学习Android的MVP,但不确定我做错了什么,java,android,android-recyclerview,retrofit,mvp,Java,Android,Android Recyclerview,Retrofit,Mvp,我正在学习Android的MVP。我建立了一个简单的应用程序,使用RecyclerView,改装,我使用免费的口袋妖怪API加载口袋妖怪的名字。下面是我的代码。请让我知道我做错了什么。当应用程序运行时,我会看到一个空白屏幕。下面是我的类和XML 主要活动的xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro

我正在学习Android的MVP。我建立了一个简单的应用程序,使用RecyclerView,改装,我使用免费的口袋妖怪API加载口袋妖怪的名字。下面是我的代码。请让我知道我做错了什么。当应用程序运行时,我会看到一个空白屏幕。下面是我的类和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>
API服务:

public interface PokeService {

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

据我所见,当您将loadData设置为recyclerview时,您做了如下操作

@覆盖
公共void loadDataInList(列表pokemonList){
rvAdapter=新的rvAdapter(口袋妖怪列表);
recyclerView.setAdapter(rvAdapter);
}
此步骤仅意味着您将适配器设置为recyclerview,但您忘记通知您recyclerview数据已更改


您可以在设置适配器行进行测试之后添加类似于rvAdapter.notifyDataChanged()的内容。

是否确定API调用和响应解析successfully@CôngHải Hả我在上面添加了API。在onFailure中,请添加t.printStackTrace(),这样您将在日志中看到错误(如果有)。让我知道发生了什么错误我尝试添加了rvAdapter.notifyDataChanged(),但仍然得到相同的结果。这是完全正确的。起初他没有设置数据,适配器也没有。所以没有什么需要更新的。
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);

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

    }
    interface MainPresenter {
       void getData();
    }
}
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;
    }
}
public interface PokeService {

    @GET("/pokemon")
    Call<PokemonResponse> getPokemons();
}
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);
    }
    }
}
{
    "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/"
        }

    ]
}