Using Download manager with recycler view - java

Here is my DataAdapter class:
package com.example.ashish.fileserver;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import retrofit2.Callback;
/**
* Created by ashish on 23/8/16.
*/
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersions> android;
private static String url;
private Context context;
public DataAdapter(Context context,ArrayList<AndroidVersions> android) {
this.android = android;
this.context = context;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(android.get(i).getName());
viewHolder.tv_version.setText(android.get(i).getVer());
//viewHolder.tv_api_level.setText(android.get(i).getApi());
}
#Override
public int getItemCount() {
return android.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView tv_name,tv_version,tv_api_level;
public CardView card;
ArrayList<AndroidVersions> android = new ArrayList<AndroidVersions>();
public ViewHolder(View view) {
super(view);
this.android = android;
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
view.setOnClickListener(this);
//tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
}
#Override
public void onClick(View view) {
System.out.println(getAdapterPosition());
int position = getAdapterPosition();
if (position == 1){
System.out.println("hey Ashish");
url = "http://vfu.bg/en/e-Learning/Computer-Basics--CTE_I__-_Computer_Basics.ppt";
String servicestring = Context.DOWNLOAD_SERVICE;
DownloadManager downloadmanager;
downloadmanager = (DownloadManager)getSystemService(servicestring);
Uri uri = Uri
.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
long reference = downloadmanager.enqueue(request);
Toast.makeText(context.getApplicationContext(),
"Your file is now downloading...", Toast.LENGTH_LONG).show();
}
}
}
}
I want to Download some files at the time when I click on a Cardview but it's showing some error that java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference. When I click on the card view it says App Stopped Working. How could I solve thsi problem?

If you check DownloadManager, it says:
Note that the application must have the INTERNET permission to use
this class.
Are you good with that? Also, this kind operation should be done in a different AsyncTask, so that you can keep your main thread free.
Finally, you will not get any log by using System.out.println. Use Android Log API instead

String servicestring = Context.DOWNLOAD_SERVICE;
downloadmanager = (DownloadManager)getSystemService(servicestring);
error was in these two lines, I've changed it to:
String servicestring = Context.DOWNLOAD_SERVICE;
downloadmanager = (DownloadManager)context.getSystemService(servicestring);
And in main activity this line was showing error:
adapter = new DataAdapter(data);
to:
adapter = new DataAdapter(data, ScrollingActivity_Test.this);

Related

A simple implementation of a RecyclerView Listener

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);
.....
}

Use epicDialog in Adapter class

I have built an overview of an order with a ReyclerView. Below you can see the code for the adapter class. My problem only occurs in the following place, if the user clicks on an element of ReyclerView, a popup with an expanded view should appear.
This popup also occurs. Unfortunately, as soon as I want to populate the TextView with the order code, the app supports and says null Attempt to invoke virtual method'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference.
How can I avoid this null error so that it all works?
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class UserBestellAdapter extends RecyclerView.Adapter<UserBestellAdapter.ViewHolder> {
ArrayList<Bestellung> bestellung;
Context mContext;
Dialog epicDialog;
public UserBestellAdapter(Context context, ArrayList<Bestellung> list) {
mContext = context;
bestellung = list;
epicDialog = new Dialog(mContext);
}
#NonNull
#Override
public UserBestellAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_bestell, parent, false);
UserBestellAdapter.ViewHolder viewHolder = new UserBestellAdapter.ViewHolder(view);
return viewHolder;
}
#NonNull
#Override
public void onBindViewHolder(#NonNull UserBestellAdapter.ViewHolder holder, int position) {
//Gesamtpreis: holder.item_betrag.setText(String.valueOf(bestellung.get(position).getBetrag()));
// Datum: holder.item_datum.setText(bestellung.get(position).getDatum());
holder.item_items.setText(bestellung.get(position).getProdukte());
//holder.item_code.setText(bestellung.get(position).getBestellnummer());
String bestellid =bestellung.get(position).getBestellnummer() + "";
holder.item_code.setText(bestellid);
holder.item_betrag.setText(Double.toString(bestellung.get(position).getSumme()));
holder.item_datum.setText(bestellung.get(position).getDatum());
holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView order_overview_number = epicDialog.findViewById(R.id.order_overview_number);
order_overview_number.setText();
epicDialog.setContentView(R.layout.order_popup_waiting);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
/*
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
epicDialog.dismiss();
}
});*/
epicDialog.show();
}
});
}
#Override
public int getItemCount() {
return bestellung.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView item_items, item_betrag, item_datum, item_code;
private ConstraintLayout layout_user_bestellung;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item_items = itemView.findViewById(R.id.items);
item_betrag = itemView.findViewById(R.id.betrag);
item_datum = itemView.findViewById(R.id.datum);
item_code = itemView.findViewById(R.id.code);
layout_user_bestellung = itemView.findViewById(R.id.layout_user_bestellung);
}
}
}
Your Dialog is initialized with new Dialog(context) but in your onClick Method you expect that your epicDialog has a TextVew with the id order_overview_number.
This will always return null and thus order_overview_number.setText() thows a NPE.
To reference a view of a Dialog the Dialog needs a layout which contains that view, similar to fragments and activities.
This may have further information for you:
How to create a Custom Dialog box in android?
https://developer.android.com/guide/topics/ui/dialogs

Android Studio & Firebase: CardView Loading Problem

I have a screen that shows my movie names and their pictures which are in my Firebase with CardView. Functionally, it works perfectly. But the problem is; firstly it shows the loadingPanel, secondly it shows the movie names, lastly it shows them with the pictures. Normally, it should show the loadingPanel until all names and pictures are shown.
My database's structure is like this. name is the string which is the name of my movie. profilePic is the string that contains the picture link from Firebase storage.
Where is my fault? Can you fix it?
Movies.java
package com.example.XXXX;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Movies extends AppCompatActivity {
DatabaseReference reference;
RecyclerView recyclerView;
ArrayList<Profile> list;
MyAdapter adapter;
private String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview_films);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
reference = FirebaseDatabase.getInstance().getReference().child("Films");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<Profile>();
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
Profile p = dataSnapshot1.getValue(Profile.class);
list.add(p);
}
adapter = new MyAdapter(Movies.this,list);
recyclerView.setAdapter(adapter);
findViewById(R.id.loadingPanel).setVisibility(View.GONE);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Movies.this,"Ooops... Something is wrong.",Toast.LENGTH_SHORT).show();
}
});
Intent intent = getIntent();
message = intent.getStringExtra("EXTRA_MESSAGE");
ImageButton backButton = (ImageButton) findViewById(R.id.imageButton13);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Movies.this, Menu.class);
i.putExtra("EXTRA_MESSAGE",message);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
});
}
#Override
public void onBackPressed() {
Intent i = new Intent(Movies.this, Menu.class);
i.putExtra("EXTRA_MESSAGE",message);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
}
MyAdapter.java
package com.example.XXXX;
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.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
ArrayList<Profile> profiles;
public MyAdapter(Context c, ArrayList<Profile> p){
context = c;
profiles = p;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview,viewGroup, false));
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.name.setText(profiles.get(i).getName());
Picasso.get().load(profiles.get(i).getProfilePic()).into(myViewHolder.profilePic);
}
#Override
public int getItemCount() {
return profiles.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView name;
ImageView profilePic;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.film_name);
profilePic = (ImageView) itemView.findViewById(R.id.filmProfile);
}
}
}
Profile.java
package com.example.XXXX;
public class Profile {
private String name;
private String profilePic;
public Profile() {
}
public Profile(String name, String profilePic) {
this.name = name;
this.profilePic = profilePic;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfilePic() {
return profilePic;
}
public void setProfilePic(String profilePic) {
this.profilePic = profilePic;
}
}
According to your comments, I understand that you want to display a ProgessBar "until all names and pictures are settled and ready".
When you are using the following line of code in your onDataChange() method:
findViewById(R.id.loadingPanel).setVisibility(View.GONE);
It means that you hide the ProgessBar when you finished getting the data from the database. What you have done till now, you only got the name and a reference (link) to the actual picture, not the picture itself. Since you already have the name, this is instantly displayed in your TextView, that's why you see it first. To display the actual image, it takes some time to download it and idsplay it in your ImageView using Picasso. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before the image is downloaded and displayed, that's why you see it delayed. This time depdends on the size of the image. To solve this, you have two options.
The first one would be to store a thumbnail of your actual image that has a small size and can be displayed almost instantly or you can attach a listener to each picture and display the entire view, only when the image is downloaded. I'm not familiar with Picasso but when using Glide for Android, you use RequestListener's onResourceReady() method.

I am trying to create a recyclerAdapter inside another recyclerAdapter but my code has a "null object reference" error [duplicate]

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().

Intent error with private class

I am trying to call another class using intent. But it is not working. I am using android studio to implement this project.
The app should show a screen with logos of popular brands like amazon, google, twitter, facebook, etc. When a logo is clicked, it shows another screen(activity), by calling AnotherActivity.java
Below is the class which seems to contain the problem:
package com.aquino.gridlayoutmanagerrecyclerview;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ActivityMain extends AppCompatActivity {
RecyclerView rvMain;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvMain = (RecyclerView) findViewById(R.id.rvMain);
Bitmap[] logos = new Bitmap[12];
logos[0] = BitmapFactory.decodeResource(getResources(),
R.drawable.medida2);
logos[1] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_fb);
logos[2] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_google);
logos[3] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_insta);
logos[4] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linkedin);
logos[5] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_microsoft);
logos[6] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_myspace);
logos[7] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_skype);
logos[8] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_snapchat);
logos[9] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_twitter);
logos[10] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_viber);
logos[11] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_whatsapp);
MyAdapter adapter = new MyAdapter(getResources().getStringArray(R.array.company_list), logos);
rvMain.setLayoutManager(new GridLayoutManager(ActivityMain.this, 2));
rvMain.setAdapter(adapter);
}//end of onCreate()
private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
String[] companyList;
Bitmap[] logoList;
public MyAdapter(String[] companyList, Bitmap[] logoList) {
this.companyList = companyList;
this.logoList = logoList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
MyViewHolder viewHolder = new MyViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.logo.setImageBitmap(logoList[position]);
holder.logo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
}
});
holder.name.setText(companyList[position]);
}
#Override
public int getItemCount() {
return companyList.length;
}
}//end of MyAdapter
private class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView logo;
public TextView name;
public MyViewHolder(View itemView) {
super(itemView);
logo = (ImageView)itemView.findViewById(R.id.ivLogo);
name = (TextView)itemView.findViewById(R.id.tvCompany);
}
}//end of MyViewHolder
}
I cannot figure out why it is not working to add intent inside private class MyAdapter.
Any help is welcome
As per your code, all are fine. but in the private adapter, you will not get the context of the class directly by "this". so just replace code.
Intent intent = new Intent(ActivityMain.this, AnotherActivity.class);
startActivity(intent);
Im not sure what error you are getting.
good way to implement onclick in recyclerview is to create an interface.
implement the interface in the activity, create intent in the implemented method of interface in activity and call it in recyclerview
or use this :
Defining a RecyclerView's onCLickListener in an Activity

Categories

Resources