I have Tried to set an adapter for my recycler view and it throws me the following error
Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
this is the code for current activty
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.TextView;
public class resourcetab extends AppCompatActivity {
private TextView txt;
private DataModel2 data =new DataModel2();
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resourcetab);
data =getIntent().getParcelableExtra("object");
resourceadapter adapter = new resourceadapter(data);
recyclerView.setAdapter(adapter);
}
}
the code for recycler view adapter
package com.example.myapplication;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class resourceadapter extends RecyclerView.Adapter<resourceadapter.ViewHolder> {
private List<String> URL= new ArrayList<>();
private List<String> type=new ArrayList<>();
private List<String> Giver=new ArrayList<>();
public resourceadapter(DataModel2 data){
this.URL=data.URL;
this.type=data.type;
this.Giver=data.Giver;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_resource , parent , false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
URL.get(position);
holder.type1.setText(type.get(position));
holder.name1.setText(Giver.get(position));
holder.liner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return Giver.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private LinearLayout liner ;
private TextView type1;
private TextView name1;
public ViewHolder(#NonNull View itemView) {
super(itemView);
liner = itemView.findViewById(R.id.liner);
name1 = itemView.findViewById(R.id.textView6);
type1 = itemView.findViewById(R.id.textView7);
}
}
}
You have to initialize your recycler view by findViewById method befor setting adapter in onCreate.
Like,
recyclerView = findViewById(R.id.recView) //id of Recycler View which you given in your xml
Related
I have the following code and result:
public class MyClass {
public static void main(String args[]) {
String[][] parent_table = {{"I-101", "First(parent)"}, {"I-102", "Second(parent)"}, {"I-103", "Third(parent)"}};
String[][] child_table = {{"I-101", "First-1(child)"}, {"I-101", "First-2(child)"}, {"I-102", "Second-1(child)"}, {"I-102", "Second-2(child)"}, {"I-103", "Third-1(child)"}};
for(String[] parent: parent_table) {
System.out.println(parent[1]);
for (String[] child: child_table) {
if (parent[0].equals(child[0])) {
System.out.println(child[1]);
}
}
}
}
}
First(parent)
First-1(child)
First-2(child)
Second-2(child)
Second-1(child)
Second-2(child)
Third(parent)
Third-1(child)
I apply this in nested recyclerview but I'm not getting the actual result, I need the above-mentioned result.
I Need Help to Solve This.
Here is my MainActivity:
package com.example.nestedrecyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
MyAdapter myAdapter;
ArrayList<ParentItem> parentItemArrayList;
ArrayList<ChildItem> childItemArrayList;
RecyclerView RVparent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RVparent = findViewById(R.id.RVparent);
String[][] parent_table = {{"I-101", "First(parent)"}, {"I-102", "Second(parent)"}, {"I-103", "Third(parent)"}};
String[][] child_table = {{"I-101", "First-1(child)"}, {"I-101", "First-2(child)"}, {"I-102", "Second-1(child)"}, {"I-102", "Second-2(child)"}, {"I-103", "Third-1(child)"}};
parentItemArrayList = new ArrayList<>();
childItemArrayList = new ArrayList<>();
for(String[] parent: parent_table) {
ParentItem parentItem = new ParentItem(parent[1]);
parentItemArrayList.add(parentItem);
for (String[] child: child_table) {
if (parent[0].equals(child[0])) {
ChildItem childItem = new ChildItem(child[1]);
childItemArrayList.add(childItem);
}
}
}
myAdapter = new MyAdapter(this,parentItemArrayList,childItemArrayList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RVparent.setLayoutManager(layoutManager);
RVparent.setAdapter(myAdapter);
}
}
Here is my MyAdapter:
package com.example.nestedrecyclerview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Activity activity;
ArrayList<ParentItem> parentItemArrayList;
ArrayList<ChildItem> childItemArrayList;
public MyAdapter(Activity activity, ArrayList<ParentItem> parentItemArrayList, ArrayList<ChildItem> childItemArrayList) {
this.activity = activity;
this.parentItemArrayList = parentItemArrayList;
this.childItemArrayList = childItemArrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//holder.tvName.setText(arrayList.get(position));
ParentItem parentItem = parentItemArrayList.get(position);
holder.tvOrderId.setText(parentItem.orderId);
MemberAdp adapterMember = new MemberAdp(childItemArrayList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
holder.nested_rv.setLayoutManager(linearLayoutManager);
holder.nested_rv.setAdapter(adapterMember);
}
#Override
public int getItemCount() {
return parentItemArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvOrderId, tvqty, tvprice;
CircleImageView ivParent;
RecyclerView nested_rv;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvOrderId = itemView.findViewById(R.id.tvOrderId);
nested_rv = itemView.findViewById(R.id.nested_rv);
}
}
}
Here is my MenberAdp:
package com.example.nestedrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class MemberAdp extends RecyclerView.Adapter<MemberAdp.ViewHolder> {
ArrayList<ChildItem> childarrayList;
public MemberAdp(ArrayList<ChildItem> childarrayList) {
this.childarrayList = childarrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ChildItem childItem = childarrayList.get(position);
holder.tvItemName.setText(childItem.itemName);
}
#Override
public int getItemCount() {
return childarrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tvItemName, tvqtyChild, tvpriceChild;
CircleImageView ivChild;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvItemName = itemView.findViewById(R.id.tvItemName);
}
}
}
It's a chatting application where i only wanted to display the users from database those whose numbers are available on the users device contact list.
I have got the list of contact list but i don't know how to query the database or i even tried a work around where it displays the user information who are present but those who are not present it shows a default itemview. You can check image link for reference
package com.example.myapplication;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* A simple {#link Fragment} subclass.
* Use the {#link Fragment1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment1 extends Fragment {
private FirebaseFirestore firebaseFirestore;
LinearLayoutManager linearLayoutManager;
private FirebaseAuth firebaseAuth;
ImageView imageViewOfUser;
FirestoreRecyclerAdapter<firebasemodel, NoteViewHolder> chatAdapter;
RecyclerView recyclerView;
ArrayList<String> contactList;
ArrayList<String> getcontactList;
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
recyclerView = view.findViewById(R.id.recyclerview);
contactList = new ArrayList<String>();
getcontactList = getContactList();
Query query = firebaseFirestore.collection("Users").whereNotEqualTo("uid", firebaseAuth.getUid());
FirestoreRecyclerOptions<firebasemodel> allusername = new FirestoreRecyclerOptions.Builder<firebasemodel>().setQuery(query, firebasemodel.class).build();
chatAdapter = new FirestoreRecyclerAdapter<firebasemodel, NoteViewHolder>(allusername) {
#Override
protected void onBindViewHolder(#NonNull NoteViewHolder noteViewHolder, int i, #NonNull firebasemodel firebasemodel) {
for (String m : getcontactList) {
if (firebasemodel.getContact().equals(m)) {
noteViewHolder.particularusername.setText(firebasemodel.getName());
String uri = firebasemodel.getImage();
Picasso.get().load(uri).into(imageViewOfUser);
if (firebasemodel.getStatus().equals("Online")) {
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
noteViewHolder.statusofuser.setTextColor(Color.rgb(66, 133, 244));
} else {
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
}
noteViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked on particular user", Toast.LENGTH_SHORT).show();
}
});
} else {
//else code?
}
}
}
#NonNull
#Override
public NoteViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chatviewlayout, parent, false);
return new NoteViewHolder(view);
}
};
recyclerView.setHasFixedSize((true));
linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(chatAdapter);
return view;
}
public class NoteViewHolder extends RecyclerView.ViewHolder{
private TextView particularusername;
private TextView statusofuser;
public NoteViewHolder(#NonNull View itemView) {
super(itemView);
particularusername = itemView.findViewById(R.id.nameofuser);
statusofuser = itemView.findViewById(R.id.statusofuser);
imageViewOfUser = itemView.findViewById(R.id.imageviewofuser);
}
}
//-----------------------------------------------------------------------------------------------//
#Override
public void onStart() {
super.onStart();
chatAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
if(chatAdapter!=null){
chatAdapter.stopListening();
}
}
//-----------------------------------------------------------------------------------------//
private ArrayList<String> getContactList(){
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while (phones.moveToNext()){
String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
contactList.add(phone);
}
return contactList;
}
}
I was trying to practice using the Recycler in Android Studio using Java.
I have made so much progress so far.
When I run the application it crashes immediately, I have checked the logs and I had an error on the below:
Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
and
Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
Below is my MainActivity class:
package com.example.recyleview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recView.findViewById(R.id.recView);
recView.setLayoutManager(new LinearLayoutManager(this));
setContentView(R.layout.activity_main);
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Jacob", "jacob#gmail.com", "later"));
contacts.add((new Contact("Sara ", "sara#gmail.com", "later")));
contacts.add(new Contact("Nino", "nino#gmail.com", "later"));
ContactsRecViewAdapter adapter = new ContactsRecViewAdapter(this);
adapter.setContacts(contacts);
recView.setAdapter(adapter);
}
}
and here is another class called ContactsRecViewAdapter
package com.example.recyleview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ContactsRecViewAdapter extends RecyclerView.Adapter<ContactsRecViewAdapter.ViewHolder>{
private ArrayList<Contact> contacts = new ArrayList<>();
private final Context context;
public ContactsRecViewAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ContactsRecViewAdapter.ViewHolder holder, int position) {
holder.txtName.setText(contacts.get(position).getName());
holder.parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, contacts.get(position).getName() + " Clicked", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return contacts.size();
}
public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private final TextView txtName;
private final RelativeLayout parent;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
parent = itemView.findViewById(R.id.parent);
}
}
}
Change
recView.findViewById(R.id.recView);
to
recView = findViewById(R.id.recView);
Also you have duplicated:
setContentView(R.layout.activity_main);
You have to set the contentView before you try to find the views.
I want the app to open a page when the user taps on an item and pass some extra information to that intent. That's it. I tried about 7 - 10 onclickListener types but I didn't solve anything. I am posting my adapter class with the latest implementation ( that doesn't work ). Json works and the Recyclerview loads data and images from url with no problems.
package byDragosT.myapplication;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class RecipeApiAdapter extends RecyclerView.Adapter<RecipeApiAdapter.RecipeViewHolder> {
private ArrayList<RecipeItem> recipeItems;
private Context context;
public RecipeApiAdapter(ArrayList<RecipeItem> recipeItems, Context context){
this.recipeItems = recipeItems;
this.context = context;
}
#NonNull
#Override //// Mare grija ce layout pui aici - sfat pentru viitor - mi-a luat 4 ore sa gasesc eroarea
public RecipeViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_in_recyclerview_api, parent, false);
return new RecipeViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecipeViewHolder holder, int position) {
final RecipeItem recipeItem = recipeItems.get(position);
holder.title.setText(recipeItem.getMnameRecipe());
holder.description.setText(recipeItem.getMdescribeRecipe());
Picasso.get().load(recipeItem.getItemPictureUrl())
.into(holder.jpegRecipe);
System.out.println("image with title: " + holder.title.getText() + "was added in theory");
}
#Override
public int getItemCount() {
return recipeItems.size();
}
public static class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView jpegRecipe;
public TextView title;
public TextView description;
public RelativeLayout containerView;
public RecyclerView mRecyclerView;
RecipeViewHolder(View view){
super(view);
jpegRecipe = view.findViewById(R.id.picture);
title = view.findViewById(R.id.recipe_name);
description = view.findViewById(R.id.ingredients_list);
containerView = view.findViewById(R.id.recipe_item_container); // vezi aici
mRecyclerView = view.findViewById(R.id.recyclerView);
}
#Override
public void onClick(View v) {
int itemPosition = mRecyclerView.getChildLayoutPosition(v);
RecipeItem current = recipeItems.get(itemPosition);
Intent intent = new Intent(v.getContext(), RecipeDetailsActivity.class);
intent.putExtra("name", current.getMnameRecipe());
v.getContext().startActivity(intent);
}
}
}
Try adding view.setOnClickListener(this);
RecipeViewHolder(View view){
super(view);
view.setOnClickListener(this);
.....
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
This is the code for the first Recycler Adapter, whenever the 1st button is clicked, it must show the hidden layout containing the other recycler adapter. Both recycler adapters get their data from an 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();
}
}
This is the code for the 2nd Recycler adapter that must be created and displayed when the button in the first recycler adapter is pressed
this image is what it must look like when the button for creating another recycler adapter is clicked
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);;
}
}
}
You have not initialized your RecyclerView recyclerView. The one that is given an adaptor here,
recyclerView.setAdapter(recycler_adapter);
is null.This is why you have your error.
But there are a few more issues at hand. Are you trying to create recyclerviews inside the rows of the outer recyclerview? If so, you need to put a RecyclerView inside each holder. While you are at it, you probably will also have to have a seperate adaptor for each of those recyclerviews so you should add that to the holder as well or attach new ones in onCreateViewHolder().