这是第一个回收站适配器的代码,每当单击第一个按钮时,它必须显示包含另一个回收站适配器的隐藏布局。两个回收站适配器都从arraylist中获取数据
package com.example.cholomanglicmot.nativechickenandduck.BroodersDirectory;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import com.example.cholomanglicmot.nativechickenandduck.DatabaseHelper;
import com.example.cholomanglicmot.nativechickenandduck.R;
import java.io.Console;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class RecyclerAdapter_Brooder_Pen extends RecyclerView.Adapter<RecyclerAdapter_Brooder_Pen.RecyclerViewHolder> {
ArrayList<Brooders_Pen> arrayList = new ArrayList<>();
//Map<String, ArrayList<String>> brooder_inventory_dictionary = new HashMap<String, ArrayList<String>>();
RecyclerView recyclerView;
RecyclerView.Adapter recycler_adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Brooder_Inventory> arrayList2 = new ArrayList<>();
RecyclerAdapter_Brooder_Pen(ArrayList<Brooders_Pen> arrayList, ArrayList<Brooder_Inventory> arrayList2){
this.arrayList = arrayList;
this.arrayList2 = arrayList2;
}
Context context;
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brooder_row_layout,parent, false);
context = parent.getContext();
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int position) {
final Brooders_Pen broodersPen = arrayList.get(position);
final RecyclerViewHolder holder2 = holder;
holder.brooder_pen_number.setText(broodersPen.getBrooder_pen_number());
holder.brooder_pen_content.setText(broodersPen.getBrooder_pen_content().toString());
holder.brooder_pen_free.setText(broodersPen.getBrooder_pen_free().toString());
holder.brooder_inventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(holder2.brooder_inventory_layout.getVisibility() == View.VISIBLE){
holder2.brooder_inventory_layout.setVisibility(View.GONE);
}else{
holder2.brooder_inventory_layout.setVisibility(View.VISIBLE);
//THIS IS WHERE I TRY TO CREATE THE RECYCLER ADAPTER FOR THE CURRENT ITEM BUT THE ERROR SAYS "Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference"
recycler_adapter = new RecyclerAdapter_Brooder_Inventory(arrayList2); //create another recycleradapter
recyclerView.setAdapter(recycler_adapter);
recycler_adapter.notifyDataSetChanged();
}
}
});
holder.brooder_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentActivity activity = (FragmentActivity)(context);
FragmentManager fm = activity.getSupportFragmentManager();
CreateBrooderDialog alertDialog = new CreateBrooderDialog();
alertDialog.show(fm, "CreateBrooderDialog");
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView brooder_pen_number;
TextView brooder_pen_content;
TextView brooder_pen_free;
HorizontalScrollView brooder_inventory_layout;
ImageButton brooder_add;
ImageButton brooder_inventory;
RecyclerViewHolder(View view){
super(view);
brooder_pen_number = view.findViewById(R.id.brooder_pen_number);
brooder_pen_content = view.findViewById(R.id.brooder_pen_content);
brooder_pen_free = view.findViewById(R.id.brooder_pen_free);
brooder_inventory_layout = view.findViewById(R.id.brooder_inventory_layout);
brooder_add = view.findViewById(R.id.brooder_add);
brooder_inventory = view.findViewById(R.id.brooder_inventory);
}
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
这是在按下第一个回收站适配器中的按钮时必须创建并显示的第二个回收站适配器的代码
package com.example.cholomanglicmot.nativechickenandduck.BroodersDirectory;
import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.cholomanglicmot.nativechickenandduck.DatabaseHelper;
import com.example.cholomanglicmot.nativechickenandduck.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class RecyclerAdapter_Brooder_Inventory extends RecyclerView.Adapter<RecyclerAdapter_Brooder_Inventory.RecyclerViewHolder> {
ArrayList<Brooder_Inventory> arrayList2 = new ArrayList<>();
DatabaseHelper myDb;
Map<String, ArrayList<String>> brooder_inventory_dictionary = new HashMap<String, ArrayList<String>>();
RecyclerAdapter_Brooder_Inventory(ArrayList<Brooder_Inventory> arrayList2){
this.arrayList2 = arrayList2;
// this.brooder_inventory_dictionary = brooder_inventory_dictionary;
}
Context context;
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brooder_inventory_row_layout,parent, false);
context = parent.getContext();
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int position) {
final Brooder_Inventory brooder_inventory = arrayList2.get(position); //nakuha mo na kung anong brooder_inventory ka
final RecyclerViewHolder holder2 = holder;
holder.brooder_inventory_code.setText(brooder_inventory.getBrooder_inv_brooder_id());
}
@Override
public int getItemCount() {
return arrayList2.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView brooder_inventory_code;
TextView brooder_inventory_family;
TextView brooder_inventory_line;
TextView brooder_inventory_gen;
TextView brooder_inventory_batch_date;
TextView brooder_inventory_date_added;
TextView brooder_inventory_last_update;
TextView brooder_inventory_mort;
TextView brooder_inventory_cull;
ImageView brooder_inventory_number_male;
ImageView brooder_inventory_number_female;
ImageView brooder_inventory_total;
RecyclerViewHolder(View view){
super(view);
brooder_inventory_code = view.findViewById(R.id.brooder_inventory_code);
brooder_inventory_family= view.findViewById(R.id.brooder_inventory_family);
brooder_inventory_line = view.findViewById(R.id.brooder_inventory_line);
brooder_inventory_gen = view.findViewById(R.id.brooder_inventory_gen);
brooder_inventory_batch_date = view.findViewById(R.id.brooder_inventory_batch_date);;
brooder_inventory_date_added = view.findViewById(R.id.brooder_inventory_date_added);;
brooder_inventory_last_update = view.findViewById(R.id.brooder_inventory_last_update);;
brooder_inventory_mort = view.findViewById(R.id.brooder_inventory_mort);;
brooder_inventory_cull = view.findViewById(R.id.brooder_inventory_cull);;
brooder_inventory_number_male = view.findViewById(R.id.brooder_inventory_number_male);;
brooder_inventory_number_female = view.findViewById(R.id.brooder_inventory_number_female);;
brooder_inventory_total = view.findViewById(R.id.brooder_inventory_total);;
}
}
}
答案 0 :(得分:0)
您尚未初始化RecyclerView recyclerView
。此处提供了适配器的那个
recyclerView.setAdapter(recycler_adapter);
为空。这就是为什么您出错的原因。
但是还有其他一些问题。您是否要在外部recyclerview的行内创建recyclerviews?如果是这样,则需要在每个持有人中放入RecyclerView
。在使用它时,您可能还必须为每个recyclerviews使用一个单独的适配器,因此您也应该将其添加到支架中,或将新的适配器附加到onCreateViewHolder()
中。