Intent error with private class - java

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

Related

SQLite database not updating after deleting rows

I am new, and I am making an app that displays data in a cardView within a recyclerView in android studio. I am trying to be able to delete the data that is displayed on the cardView. The cardView and data gets deleted as it should, but once I go back to the activity holding the cardView and recyclerView, it's all still there. Does anyone know why this is happening? I'll post my code below
ReminderDBHelper:
package com.example.lasttry;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList reminder_id, reminder_title, reminder_location, reminder_medication;
reminderDBHelper mydb;
Button reminderDelete;
CustomAdapter(Context context,
ArrayList reminder_id,
ArrayList reminder_title,
ArrayList reminder_location,
ArrayList reminder_medication) {
this.context = context;
this.reminder_id = reminder_id;
this.reminder_title = reminder_title;
this.reminder_location = reminder_location;
this.reminder_medication = reminder_medication;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.reminder_id.setText(String.valueOf(reminder_id.get(position)));
holder.reminder_title.setText(String.valueOf(reminder_title.get(position)));
holder.reminder_location.setText(String.valueOf(reminder_location.get(position)));
holder.reminder_medication.setText(String.valueOf(reminder_medication.get(position)));
holder.reminderDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mydb = new reminderDBHelper(context);
String id = String.valueOf(reminder_id);
mydb.deleteData(id);
reminder_id.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getLayoutPosition());
}
});
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(reminder_id.get(holder.getAdapterPosition())));
intent.putExtra("title", String.valueOf(reminder_title.get(holder.getAdapterPosition())));
intent.putExtra("location", String.valueOf(reminder_location.get(holder.getAdapterPosition())));
intent.putExtra("medication", String.valueOf(reminder_medication.get(holder.getAdapterPosition())));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return reminder_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView reminder_id, reminder_title, reminder_location, reminder_medication;
Button reminderDelete;
LinearLayout mainLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
reminderDelete = itemView.findViewById(R.id.reminder_delete);
reminder_id = itemView.findViewById(R.id.reminder_id);
reminder_title = itemView.findViewById(R.id.reminder_title);
reminder_location = itemView.findViewById(R.id.reminder_location);
reminder_medication = itemView.findViewById(R.id.reminder_medication);
mainLayout = itemView.findViewById(R.id.mainLayout);
}
}
}
CustomAdapter:
package com.example.lasttry;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList reminder_id, reminder_title, reminder_location, reminder_medication;
reminderDBHelper mydb;
Button reminderDelete;
CustomAdapter(Context context,
ArrayList reminder_id,
ArrayList reminder_title,
ArrayList reminder_location,
ArrayList reminder_medication) {
this.context = context;
this.reminder_id = reminder_id;
this.reminder_title = reminder_title;
this.reminder_location = reminder_location;
this.reminder_medication = reminder_medication;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.reminder_id.setText(String.valueOf(reminder_id.get(position)));
holder.reminder_title.setText(String.valueOf(reminder_title.get(position)));
holder.reminder_location.setText(String.valueOf(reminder_location.get(position)));
holder.reminder_medication.setText(String.valueOf(reminder_medication.get(position)));
holder.reminderDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mydb = new reminderDBHelper(context);
String id = String.valueOf(reminder_id);
mydb.deleteData(id);
reminder_id.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getLayoutPosition());
}
});
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(reminder_id.get(holder.getAdapterPosition())));
intent.putExtra("title", String.valueOf(reminder_title.get(holder.getAdapterPosition())));
intent.putExtra("location", String.valueOf(reminder_location.get(holder.getAdapterPosition())));
intent.putExtra("medication", String.valueOf(reminder_medication.get(holder.getAdapterPosition())));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return reminder_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView reminder_id, reminder_title, reminder_location, reminder_medication;
Button reminderDelete;
LinearLayout mainLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
reminderDelete = itemView.findViewById(R.id.reminder_delete);
reminder_id = itemView.findViewById(R.id.reminder_id);
reminder_title = itemView.findViewById(R.id.reminder_title);
reminder_location = itemView.findViewById(R.id.reminder_location);
reminder_medication = itemView.findViewById(R.id.reminder_medication);
mainLayout = itemView.findViewById(R.id.mainLayout);
}
}
}
I have searched stackOverflow and reddit, but nothing seems to work. I have tried making the array array list instead.

Data not displaying on recyclerview after button press

My data doesn't add to Recylerview after pressing the add button until I either restart the app or press the back button that's built in the action bar. My activity doesn't close after pressing the button so I usually use the back button on my device. Nothing gets added to my Recyclerview until the app restarts or when I add a new set of data and press the back button from the action bar, then 2 sets of data will be added.
I have also tried to finish the activity right after the button press but then that also doesn't add data into Recyclerview.
I want to know how I can make my activity finish after button press and display data in the Recyclerview or make it so that pressing the back button from my device still makes it display.
MainActivity Code:
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
FloatingActionButton newTaskBtn;
myDatabase myDB;
ArrayList<String> task_id, task_subject, task_description, task_due_date;
CustomAdapter customAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerTasks);
newTaskBtn = findViewById(R.id.floatingBtn);
newTaskBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
}
});
myDB = new myDatabase(MainActivity.this);
task_id = new ArrayList<>();
task_subject = new ArrayList<>();
task_description = new ArrayList<>();
task_due_date = new ArrayList<>();
storeDataArray();
customAdapter = new CustomAdapter(MainActivity.this, task_id, task_subject, task_description, task_due_date);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
void storeDataArray() {
Cursor cursor = myDB.readAllData();
if (cursor.getCount() == 0) {
Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
task_id.add(cursor.getString(0));
task_subject.add(cursor.getString(1));
task_description.add(cursor.getString(2));
task_due_date.add(cursor.getString(3));
}
}
}
}
Add Data Activity Code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends AppCompatActivity {
EditText subjectEntry, descEntry, dateEntry;
Button addBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
subjectEntry = findViewById(R.id.subjectEntry);
descEntry = findViewById(R.id.descEntry);
dateEntry = findViewById(R.id.dateEntry);
addBtn = findViewById(R.id.addbtn);
addBtn.setOnClickListener(view -> {
myDatabase myDB = new myDatabase(AddActivity.this);
myDB.addTask(subjectEntry.getText().toString().trim(),
descEntry.getText().toString().trim(),
Integer.valueOf(dateEntry.getText().toString().trim()));
});
}
}
Adapter Code
import android.content.Context;
import android.content.Intent;
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.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.myViewHolder> {
private Context context;
private ArrayList task_id, task_subject, task_description, task_due_date;
private int position;
CustomAdapter(Context context, ArrayList task_id, ArrayList task_subject, ArrayList task_description, ArrayList task_due_date){
this.context = context;
this.task_id = task_id;
this.task_description = task_description;
this.task_subject = task_subject;
this.task_due_date = task_due_date;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from (context);
View view = inflater.inflate(R.layout.to_do_list, parent, false);
return new myViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final myViewHolder holder, final int position) {
this.position = position;
holder.taskid_txt.setText(String.valueOf(task_id.get(position)));
holder.taskSubject_txt.setText(String.valueOf(task_subject.get(position)));
holder.taskDescription_txt.setText(String.valueOf(task_description.get(position)));
holder.taskDate_txt.setText(String.valueOf(task_due_date.get(position)));
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(task_id.get(position)));
intent.putExtra("Description", String.valueOf(task_description.get(position)));
intent.putExtra("Subject", String.valueOf(task_subject.get(position)));
intent.putExtra("Due Date", String.valueOf(task_due_date.get(position)));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return task_id.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView taskid_txt, taskSubject_txt, taskDescription_txt, taskDate_txt;
LinearLayout mainLayout;
public myViewHolder(#NonNull View itemView) {
super(itemView);
taskid_txt = itemView.findViewById(R.id.taskid_txt);
taskSubject_txt = itemView.findViewById(R.id.taskSubject_txt);
taskDescription_txt = itemView.findViewById(R.id.taskDescription_txt);
taskDate_txt = itemView.findViewById(R.id.taskDate_txt);
mainLayout = itemView.findViewById(R.id.mainLayout);
}
}
;
}
Refresh recycle view in onResume of MainActivity
#Override
public void onResume() {
super.onResume();
storeDataArray();
}

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

Recycler view images aren't clickable, to open up a new page

I'm trying to make each image within the RecyclerView clickable to open up the photo, along with its Title and a description, but all of this info is coming through Flickr, I've added in code that I thought should be suitable but it doesn't seem to be working, as none of the images are clicking, any guidance as to how to fix this is greatly appreciated.
Would a simple findByViewId() method in the details class suffice or is there more than I seem to be missing? Like I said, any help is appreciated.
Thank you!
ImageListAdapter Class
import android.content.Context;
import android.content.Intent;
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.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
/**
* Created by Adam on 22/12/2017.
*/
public class ImageListAdapter
extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
private NetworkImageView imageThumbnail;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView imageTitle;
public NetworkImageView mainImage;
public ViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
}
private View.OnClickListener itemClicked = new View.OnClickListener() {
int position = ViewHolder.this.getLayoutPosition();
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
imageThumbnail.setOnClickListener(itemClicked);
itemView.getContext().startActivity(intent);
}
};
}
//Data Source
public ArrayList<ImageInfo> imageList = new ArrayList<ImageInfo>();
// context
private Context context;
public ImageListAdapter(Context context) {
this.context = context;
}
#Override
public int getItemCount() {
return imageList.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create the view for the cell in the list
View v = LayoutInflater.from(context).inflate(R.layout.cell_image_card, parent, false);
ImageListAdapter.ViewHolder vh = new ImageListAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageInfo imageInfo = imageList.get(position);
holder.imageTitle.setText(imageInfo.title);
holder.mainImage.setImageUrl(imageInfo.url_m,
NetworkMgr.getInstance(context).imageLoader);
// holder.mainImage.setImageResource(imageInfo.imageResource);
}
}
DetailsActivity Class
public class DetailsActivity extends AppCompatActivity {
public TextView imageTitle;
public NetworkImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
photoPosition = intent.getIntExtra("PHOTO_POSITION", 0);
titleView.setText(photo.title);
imageView = NetworkMgr.getInstance(this).imageList.get(photoPosition);
}
}
you are just forgetting to set the onClick listener on the mainImage view
Step 1: set the click listener on mainImage
public ViewHolder(View itemView) {
super(itemView);
final Context mContext = itemView.getContext();
final int position = getAdapterPosition();
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
mainImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
mContext.startActivity(intent);
}
});
}
Step 2: get the position like this in details activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
int position = intent.getIntExtra("PHOTO_POSITION", 0);
ImageInfo imageInfo = NetworkMgr.getInstance(this).imageList.get(position);
//use image info to get the image and title details.
}

Using Download manager with recycler view

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

Categories

Resources