使用 Retrofit 从服务器获取多个视图项来创建 RecyclerView

Creating RecyclerView with multiple view item fetching from server with Retrofit

场景:

我正在尝试创建一个 RecyclerView,它将在一个列表中显示几种不同的项目(食物、书籍、食谱)。 当用户在搜索栏中输入一些关键字时,数据将在改造的帮助下从服务器获取。 因此,我有一个带搜索栏的 activity,另一个用于在回收站视图中显示数据。

到目前为止做了什么: 1. 借助回收站搜索视图从服务器获取数据。 2. 将搜索 activity 中的数据(以可序列化的形式)发送到显示 activity。 3. 使用多视图适配器创建回收器视图(借助下面列出的一些教程)。

链接:

  1. Example 1

  2. Example 2

一些相关项目:

  1. Some Example Project

了解

到目前为止我所了解的是,为了在回收站视图中显示多种类型的项目,我们需要创建一个抽象视图持有者 以及每个对象的单独视图持有者。它们还必须具有相应的布局,并且它们的布局会动态地膨胀到回收器视图持有者中。

问题:

  1. 尝试将适配器设置为我的回收器视图适配器时出现错误。
  2. 我面临的问题是我似乎无法将从服务器接收到的数据与相应的视图持有者绑定。

代码:

我将省略 activity 我正在获取数据的地方,因为它正在工作。

SearhResultActivity(这个 activity 从 SearchActivity 接收数据,应该显示带有数据的回收站视图)

package com.example.naisse.Activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;
import com.example.naisse.AdapterAndClass.ResultAdaptar;
import com.example.naisse.AdapterAndClass.SearchRecyclerAdapter;
import com.example.naisse.Api.Model.Search.Article;
import com.example.naisse.Api.Model.Search.Food;
import com.example.naisse.Api.Model.Search.Hadith;
import com.example.naisse.Api.Model.Search.Manuscript;
import com.example.naisse.Api.Model.Search.Quran;
import com.example.naisse.Fragment.SearchTabFragment;
import com.example.naisse.R;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class SearchResultActivity extends HomeActivity {

private static final String TAG = "SearchResultActivity";

@BindView(R.id.search_result_recycler)
RecyclerView recyclerView;

private SearchRecyclerAdapter searchRecyclerAdapter;

private List<String> dataList;

protected FrameLayout frames;
private FragmentManager mFragmentManager;
private ResultAdaptar resultAdaptar;


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

    frames = findViewById(R.id.search_bar_holder);
    mFragmentManager = (this).getSupportFragmentManager();
    mFragmentManager.beginTransaction().add(R.id.search_bar_holder, new SearchTabFragment()).commit();


    Food foodData = new Food();
    ArrayList<Book> getBookArray = new ArrayList<>();
    ArrayList<Recipe> getRecipe = new ArrayList<>();

    foodData = (Food) getIntent().getSerializableExtra("foodDetails");
    getBookArray = (ArrayList<Book>) getIntent().getSerializableExtra("bookList");
    getRecipe = (ArrayList<Recipe>) getIntent().getSerializableExtra("recipeList");

    /*There is something wrong here, it says "Attempt to invoke virtual method on a null object reference" for searchRecyclerAdapter*/
    searchRecyclerAdapter = new SearchRecyclerAdapter(this);
    recyclerView.setAdapter(searchRecyclerAdapter);

}

}

SearchRecyclerAdapter(此适配器包含用于声明和将所有 viewHolder 连接到抽象 viewHolder 并连接到回收器视图的代码)

package com.example.naisse.AdapterAndClass;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.naisse.Api.Model.Search.Food;
import com.example.naisse.Api.Model.Search.Quran;
import com.example.naisse.R;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchRecyclerAdapter extends RecyclerView.Adapter<SearchRecyclerAdapter.AbstractViewHolder>{

private static final String TAG = "SearchRecyclerAdapter";

public static final int TYPE_FOOD = 0;
public static final int TYPE_BOOK = 1;


private Context context;
private List<Object> data;

/*I belive im missing something here*/
public SearchRecyclerAdapter(Context context){
    this.context = context;
}

public void add(List searchData){
    if(data == null){
        data = new ArrayList<>();
    }
    data.clear();
    data.addAll(searchData);
    notifyDataSetChanged();
}


@Override
public AbstractViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    switch (i){
        case TYPE_FOOD:{
            /*search_result_fragment is the xml layout for food adapter*/
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_result_fragment, viewGroup, false);
            return new FoodViewAdapter(view);
        }
        case TYPE_BOOK:{
            /*search_result_fragment is the xml layout for book adapter*/
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.viewholder_book, viewGroup, false);
            return new BooksViewAdapter(view);
        }

        default:
            throw new IllegalArgumentException("Invalid view type");

    }

}

@Override
public void onBindViewHolder(AbstractViewHolder abstractViewHolder, int i) {
    Object object = data.get(i);
    abstractViewHolder.bind(object);
}

@Override
public int getItemViewType(int i){
    Object object = data.get(i);

    if (object instanceof Food){
        return TYPE_FOOD;
    }else if (object instanceof Book){
        return TYPE_BOOK;
    }

    throw new IllegalArgumentException("Invalid position");
}

@Override
public int getItemCount() {
    return data == null ? 0 : data.size();
}

public abstract class AbstractViewHolder<T> extends RecyclerView.ViewHolder{

    public AbstractViewHolder(@NonNull View itemView) {
        super(itemView);

        ButterKnife.bind(this, itemView);
    }

    public abstract void bind(T type);
}


public class FoodViewAdapter extends AbstractViewHolder<Food> {
    @BindView(R.id.result_food_title)
    TextView foodTitle;
    @BindView(R.id.result_food_content)
    TextView content;

    private FoodViewAdapter(View itemView){
        super(itemView);
    }

    @Override
    public void bind(Food type) {
        foodTitle.setText("Food");
        content.setText("title");
    }

}

public class BooksViewAdapter extends AbstractViewHolder<Book> {
    @BindView(R.id.result_book_title)
    TextView bookTitle;
    @BindView(R.id.result_book_content)
    TextView content;

    private BooksViewAdapter(View itemView){
        super(itemView);
    }

    @Override
    public void bind(Book type) {
        bookTitle.setText("title");
        content.setText("Text");
    }

}

}

问题:

How do i set the adapter and how do I bind my data to the corresponding views?

P.S 如果您想查看其他 类 或 XML 布局,请告诉我。

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.naisse, PID: 12570
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.naisse/com.example.naisse.Activity.SearchResultActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
    at com.example.naisse.Activity.SearchResultActivity.onCreate(SearchResultActivity.java:67)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

您使用 butterknife 而忘记在 activity

中添加绑定视图

只需添加这一行

ButterKnife.bind(this);

到您的 onCreate() 的 SearchResultActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    ButterKnife.bind(this);
    //other codes
}

第二部分

对于第二部分,您需要将数据添加到您的 recycelerView(适配器)

为此,我们更改了适配器中的添加功能。使用此代码代替您的代码

public void add(ArrayList newData){
    if(data == null){
        data = new ArrayList<>();
    }
    data.addAll(searchData);
    notifyDataSetChanged();
}

现在 SearchResultActivity 尝试像这样将数据发送到适配器

searchRecyclerAdapter = new SearchRecyclerAdapter(this);
recyclerView.setAdapter(searchRecyclerAdapter);
searchRecyclerAdapter.add(foodData);
searchRecyclerAdapter.add(getBookArray);
searchRecyclerAdapter.add(getRecipe);