设想:
我正在尝试创建一个 RecyclerView,它将在一个列表中显示几种不同类型的项目(食物、书籍、食谱)。当用户在搜索栏中输入一些关键字时,将在改造的帮助下从服务器获取数据。所以,我有一个带有搜索栏的活动,另一个是在回收站视图中显示数据。
到目前为止所做的工作: 1. 在搜索的回收站视图的帮助下从服务器获取数据。2. 将搜索活动中的数据(可序列化)形式发送到显示活动。3. 创建一个带有多个视图适配器的回收器视图(借助下面列出的一些教程)。
链接:
一些相关项目:
理解
到目前为止我所理解的是,为了在回收器视图中显示多种类型的项目,我们需要为每个对象创建一个抽象视图持有者和单独的视图持有者。它们还必须具有相应的布局,并且它们的布局会动态地膨胀到回收站视图持有者中。
问题:
- 尝试将适配器设置为我的回收器视图适配器会出错。
- 我面临的问题是我似乎无法将从服务器接收到的数据与相应的视图持有者绑定。
代码:
我将省略我正在获取数据的活动,因为它正在工作。
SearhResultActivity(此活动从 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");
}
}
}
问题:
如何设置适配器以及如何将我的数据绑定到相应的视图?
PS 如果您想查看其他类或 XML 布局,请告诉我。
日志猫
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)