Passing image to next activity when wi-fi is off - android

In my app,I store the image in SQLite and read read it from there. I can display it,in a recyclerview using picasso. But when I launch a new activity,by clicking a row,the image doesn't appear.
Here is my code.
From this adapter I get the image and the other data from the database.
public class MySQLAdapter extends RecyclerView.Adapter<MySQLAdapter.RowHolderClass> {
private List<SQLiteModel> myList;
private Context mContext;
private int focused;
public MySQLAdapter(Activity activity,ArrayList<SQLiteModel> ss) {
this.myList = ss;
this.mContext=activity;
}
#Override
public MySQLAdapter.RowHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sqlite_adapter, null);
final RowHolderClass holder = new RowHolderClass(v);
return holder;
}
#Override
public void onBindViewHolder(RowHolderClass holder, int position) {
final SQLiteModel listItems = myList.get(position);
holder.itemView.setSelected(focused==position);
Picasso.with(holder.imageView.getContext())
.load(myList.get(position).getSqliteImage()).into(holder.imageView);
holder.fName.setText(myList.get(position).getSqliteFirstName());
holder.lName.setText(myList.get(position).getSqliteSecondName());
holder.jobRole.setText(myList.get(position).getSqliteRole());
//holder.imageView.setImageResource(Integer.parseInt(myList.get(position).getSqliteImage()));
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String first_name = listItems.getSqliteFirstName();
String last_name = listItems.getSqliteSecondName();
String job_role = listItems.getSqliteRole();
String image_url = listItems.getSqliteImage();
String id= String.valueOf(listItems.getSqliteId());
//Log.v("id",id);
Intent i = new Intent(mContext, NoInternetDetailedActivity.class);
i.putExtra("firstName",first_name);
i.putExtra("lastName",last_name);
i.putExtra("jobRole",job_role);
i.putExtra("imageUrl",image_url);
i.putExtra("memberId",id);
mContext.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return myList.size();
}
//ViewHolder pattern used to read the data of the row fast.
class RowHolderClass extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView fName, lName, jobRole;
protected RelativeLayout relativeLayout;
public RowHolderClass(View itemView) {
super(itemView);
this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.recLayout);
this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
this.fName = (TextView) itemView.findViewById(R.id.firstName);
this.lName = (TextView) itemView.findViewById(R.id.lastName);
this.jobRole = (TextView) itemView.findViewById(R.id.roleView);
}
}
}
Then I pass the data in a new activity.
public class NoInternetDetailedActivity extends AppCompatActivity {
ImageView imageView;
TextView textView0,textView1,textView2,textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_no_internet_detailed);
Intent i = getIntent();
String memberId = i.getExtras().getString("memberId");
String firstName = i.getExtras().getString("firstName");
String lastName = i.getExtras().getString("lastName");
String jobRole = i.getExtras().getString("jobRole");
String image = i.getExtras().getString("imageUrl");
//imageView = (ImageView)findViewById(R.id.newImageView);
textView0 = (TextView)findViewById(R.id.textView0);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
textView0.setText("Member id: "+ memberId);
textView1.setText(firstName);
textView2.setText(lastName);
textView3.setText(jobRole);
//This line gives me the exception!
Picasso.with(this).load(image).into(imageView);
}
}
The exception I get is:
Caused by: java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
Any ideas?
Thanks,
Theo.

/*Use this aproach*/
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
/* wifi is not enabled */
if (!wifi.isWifiEnabled()){
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
}
/* Receive image on second activity */
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Related

How to display full screen images when PagerAdapter has been clicked?

How to display full screen images when touched from ViewPager?
** I used Glide to fetch images from urls.
I was tried to intent to FullScreenActivity and sent imageUrls but i can't startActivity() via instantiateItem method from ImagePagerAdapter.java.
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
private Context mContext;
private String[] imageUrls;
public ImagePagerAdapter(Context mContext, String[] imageUrls) {
this.mContext = mContext;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull final ViewGroup container, final int position) {
final ImageView imageView = new ImageView(mContext);
Glide.with(mContext)
.load(imageUrls[position])
.into(imageView);
container.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Display full screen images when touched.
}
});
return imageView;
}
}
LocalExperienceDetail.java
public class LocalExperienceDetail extends AppCompatActivity {
private String[] imageUrls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_experience_detail);
String company_name_eng = getIntent().getExtras().getString("name_eng");
String img1 = getIntent().getExtras().getString("img1");
String img2 = getIntent().getExtras().getString("img2");
String img3 = getIntent().getExtras().getString("img3");
String img4 = getIntent().getExtras().getString("img4");
String img5 = getIntent().getExtras().getString("img5");
String description_activity = getIntent().getExtras().getString("activity");
String time = getIntent().getExtras().getString("time");
String price = getIntent().getExtras().getString("price");
String tel = getIntent().getExtras().getString("tel");
String car_park = getIntent().getExtras().getString("car_park");
String address = getIntent().getExtras().getString("address");
String website = getIntent().getExtras().getString("website");
TextView tvCompanyNameEng = findViewById(R.id.txtCompanyName);
TextView tvDescActivity = findViewById(R.id.txtDescActivity);
TextView tvPrice = findViewById(R.id.txtPrice);
TextView tvTel = findViewById(R.id.txtTel);
TextView tvCarPark = findViewById(R.id.txtCarPark);
TextView tvAddress = findViewById(R.id.txtAddress);
TextView tvWebsite = findViewById(R.id.txtWebsite);
TextView tvTime = findViewById(R.id.txtTime);
ViewPager viewPager = findViewById(R.id.imgView);
tvCompanyNameEng.setText(company_name_eng);
tvDescActivity.setText(description_activity);
tvPrice.setText(price);
tvTel.setText(tel);
tvCarPark.setText(car_park);
tvAddress.setText(address);
tvWebsite.setText(website);
tvTime.setText(time);
imageUrls = new String[] {
img1,
img2,
img3,
img4,
img5
};
ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
I expect the full screen images can slide able.
Design
It is possible. Method startActivity is the method member of context class (doc)
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Display full screen images when touched.
Intent intent = new Intent(); // Your intent
mContext.startActivity(intent);
}
});
If you need to work with images, somethings like gallery, facebook has developed the Fresco library. Check it out.

recycle view won't recieve a bundle data

i'm trying to pass a bundle from a product detail activity to a cart with a recycle view but when i click on add to cart the cart is opened but remains empty i don't know what's wrong with the code
here is the first activity
public class product_details extends AppCompatActivity {
ImageView product_img, share;
TextView name, price;
public int img;
public int price1;
public String name1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
product_img = (ImageView) findViewById(R.id.Product_Img);
name = (TextView) findViewById(R.id.product_name);
price = (TextView) findViewById(R.id.product_price);
share = (ImageView) findViewById(R.id.share);
img = getIntent().getIntExtra("img", 00);
Glide.with(this).load(img).into(product_img);
name1 = getIntent().getStringExtra("name");
name.setText(name1);
price1 = getIntent().getIntExtra("price", 0);
price.setText(String.valueOf(price1));
ImageView cart = (ImageView) findViewById(R.id.add_cart);
cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), CartActivity.class);
Bundle extras = new Bundle();
extras.putString("price1", String.valueOf((price1)));
extras.putString("name1", name1);
extras.putString("img1",String.valueOf(img));
intent.putExtras(extras);
startActivity(intent);
}
my second activity class
public class CartRecycler {
int images_id;
String productname,productprice,productquantity;
public String getProductquantity() {
return productquantity;
}
public CartRecycler(int images_id , String productname , String productprice , String productquantity){
this.setImages_id(images_id);
this.setProductname(productname);
this.setProductprice(productprice);
this.setProductquantity(productquantity);
}
public CartRecycler(String productname, int images_id, String productprice) {
this.productname = productname;
this.images_id = images_id;
this.productprice = productprice;
}
public int getImages_id() {
return images_id;
}
public void setImages_id(int images_id) {
this.images_id = images_id;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public String getProductprice() {
return productprice;
}
public void setProductprice(String productprice) {
this.productprice = productprice;
}
public void setProductquantity(String productquantity) {
this.productquantity = productquantity;
}
}
cart adapter
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
ArrayList<CartRecycler> adapterlist = new ArrayList<>();
CartActivity cartactivity;
Context cnx;
public CartAdapter(ArrayList<CartRecycler> adapterlist,Context cnx){
this.adapterlist =adapterlist;
this.cnx =cnx;
cartactivity = (CartActivity) cnx;
}
#Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_cards,parent,false);
CartViewHolder cartviewholder = new CartViewHolder(v,cartactivity);
return cartviewholder;
}
#Override
public void onBindViewHolder(CartViewHolder holder, int position) {
holder.image.setImageResource(adapterlist.get(position).getImages_id());
holder.name.setText(adapterlist.get(position).getProductname());
holder.price.setText(adapterlist.get(position).getProductprice());
holder.quantity.setText(adapterlist.get(position).getProductquantity());
if(!cartactivity.edit_mode){
holder.checkornot.setVisibility(View.GONE);
}
else {
holder.checkornot.setVisibility(View.VISIBLE);
holder.checkornot.setChecked(false);
}
}
#Override
public int getItemCount() {
return adapterlist.size();
}
public static class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView image;
TextView price,quantity,name;
CheckBox checkornot;
CartActivity cartactivity;
CardView cardv;
public CartViewHolder(View itemView, CartActivity cartactivity) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.cartimage);
name = (TextView) itemView.findViewById(R.id.pn);
price = (TextView) itemView.findViewById(R.id.p);
quantity = (TextView) itemView.findViewById(R.id.q);
checkornot = (CheckBox) itemView.findViewById(R.id.ckbox);
this.cartactivity= cartactivity;
cardv = (CardView) itemView.findViewById(R.id.cardd);
cardv.setOnLongClickListener((View.OnLongClickListener) cartactivity);
checkornot.setOnClickListener(this);
}
#Override
public void onClick(View view) {
cartactivity.prepareselection(view,getAdapterPosition());
}
}
public void updateadapter(ArrayList<CartRecycler> list){
for(CartRecycler cartRecycler:list){
adapterlist.remove(cartRecycler);
}
notifyDataSetChanged();
}
}
second activity
public class CartActivity extends AppCompatActivity implements View.OnLongClickListener{
Button l;
ImageView imv;
Toolbar t;
RecyclerView rv;
RecyclerView.LayoutManager layoutmanager;
RecyclerView.Adapter adapter;
ArrayList<CartRecycler> cartitems = new ArrayList<>();
ArrayList<CartRecycler> selected_items_list = new ArrayList<>();
int countt =0;
boolean edit_mode =false;
TextView counterr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
t = (Toolbar) findViewById(R.id.toolb);
setSupportActionBar(t);
getSupportActionBar().setDisplayShowTitleEnabled(false);
l = (Button) findViewById(R.id.checkoutsummary);
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent n = new Intent(CartActivity.this,SummaryActivity.class);
startActivity(n);
}
});
rv = (RecyclerView) findViewById(R.id.mycartrecycler);
layoutmanager = new LinearLayoutManager(this);
rv.setLayoutManager(layoutmanager);
rv.setHasFixedSize(true);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String price = extras.getString("price1");
String img1 = extras.getString("img1");
int imag1 = Integer.parseInt(img1);
String name = extras.getString("name1");
for (int i = 0; i < cartitems.size(); i++) {
CartRecycler cart = new CartRecycler(name, imag1, price);
cartitems.add(cart);
adapter.notifyDataSetChanged();
i++;
}
} else {
Intent intent = new Intent(this, test.class);
startActivity(intent);
}
adapter = new CartAdapter(cartitems,CartActivity.this);
rv.setAdapter(adapter);
counterr = (TextView) findViewById(R.id.selecteditemcounter);
counterr.setVisibility(View.GONE);
}
#Override
public boolean onLongClick(View view) {
t.getMenu().clear();
t.inflateMenu(R.menu.cart_edit_mode);
counterr.setVisibility(View.VISIBLE);
edit_mode = true;
adapter.notifyDataSetChanged();
return true;
}
public void prepareselection(View view,int position){
if(((CheckBox)view).isChecked()){
selected_items_list.add(cartitems.get(position));
countt = countt+1;
updatecounter(countt);
}
else {
selected_items_list.remove(cartitems.get(position));
countt = countt -1;
updatecounter(countt);
}
}
public void updatecounter(int counter){
if(counter==0){
counterr.setText("0 items selected");
}
else {
counterr.setText(counter+" item selected");
}
}
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.item_delete){
CartAdapter cartAdapter = (CartAdapter) adapter;
cartAdapter.updateadapter(selected_items_list);
}
else if(item.getItemId()== android.R.id.home)
{
adapter.notifyDataSetChanged();
}
return true;
}
}
for (int i = 0; i < cartitems.size(); i++) This loop will not run until and unless size of cartitems is greater than 0 and hence all the code under it's scope will never get executed

display image from android database

I have a local database that i want get res id from it(string saving) and show in application, also i have a recycler view and I want after click on items, display image.
public class AdapterApp extends RecyclerView.Adapter<AdapterApp.AdapterViewHolder>{
public static class AdapterViewHolder extends RecyclerView.ViewHolder{
public TextView txtDescription;
public ViewGroup layoutRoot;
public TextView txtTitle;
public ImageView imgDesc;
public AdapterViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
txtDescription = (TextView) itemView.findViewById(R.id.txtDesc);
layoutRoot = (ViewGroup) itemView.findViewById(R.id.layoutRoot);
imgDesc = (ImageView) itemView.findViewById(R.id.imgDesc);
}
}
private ArrayList<StructApp> names = new ArrayList<StructApp>();
public AdapterApp(ArrayList<StructApp> names){
this.names = names;
}
#Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = G.inflater.from(viewGroup.getContext()).inflate(R.layout.adapter_app,viewGroup,false);
return new AdapterViewHolder(view);
}
#Override
public void onBindViewHolder(final AdapterViewHolder adapterViewHolder, final int i) {
final StructApp name = names.get(i);
adapterViewHolder.txtTitle.setText(name.title);
adapterViewHolder.layoutRoot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity,ActivityDesc.class);
intent.putExtra("description",name.description);
G.currentActivity.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return names.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
database activity:
public class ActivityTitleEngineer extends ActivityEnhanced {
private RecyclerView recyclerView;
private AdapterApp adapter;
private ArrayList<StructApp> applications = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reshte_math);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(G.context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
adapter = new AdapterApp(G.reshte);
populateFromDatabase();
recyclerView.setAdapter(adapter);
}
public void populateFromDatabase() {
G.reshte.clear();
DataBaseConnection db = new DataBaseConnection(this, "database", 4);
final SQLiteDatabase database = db.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM Reshte ", null);
while (cursor.moveToNext()) {
StructApp application = new StructApp();
application.description = cursor.getString(cursor.getColumnIndex("description"));
application.title = cursor.getString(cursor.getColumnIndex("title"));
application.imgDesc = cursor.getString(cursor.getColumnIndex("img"));
G.reshte.add(application);
}
cursor.close();
}
}
activity of display image:
public class ActivityDesc extends AppCompatActivity {
private TextView txtDesc;
private ImageView imgDesc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reshte_desc);
txtDesc = (TextView) findViewById(R.id.txtDesc);
imgDesc = (ImageView) findViewById(R.id.imgDesc);
Bundle extras = getIntent().getExtras();
if(extras!= null){
String desc = extras.getString("description");
txtDesc.setText(desc);
}
}
}
Try this method :)
StorageReference islandRef = MainGameFragment.storageReference.child("Capture.PNG");
final long ONE_MEGABYTE = 1024 * 1024;
islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(bytes);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
imageSettings.setImageBitmap(bitmap);
settingsText4.setText("Click to visit site for more information");
settingsText4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "site";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
mProgress.setVisibility(View.GONE);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});

Display URL image in sqlite

In my app,I sucessfully retrieve from a webserver a list of images. They are read as urls ie
http://mw2.google.com/mw-panoramio/photos/medium/131093147.jpg
and so on.
I store the images in the SQLite as string urls. But now I want to display them in a recyclerview.
This is my adapter.
public class MySQLAdapter extends RecyclerView.Adapter<MySQLAdapter.RowHolderClass> {
private List<SQLiteModel> myList;
private Context mContext;
private int focused;
public MySQLAdapter(Activity activity,ArrayList<SQLiteModel> ss) {
this.myList = ss;
this.mContext=activity;
}
#Override
public MySQLAdapter.RowHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_row, null);
final RowHolderClass holder = new RowHolderClass(v);
return holder;
}
#Override
public void onBindViewHolder(RowHolderClass holder, int position) {
final SQLiteModel listItems = myList.get(position);
holder.itemView.setSelected(focused==position);
holder.getLayoutPosition();
holder.fName.setText(myList.get(position).getSqliteFirstName());
holder.lName.setText(myList.get(position).getSqliteSecondName());
holder.jobRole.setText(myList.get(position).getSqliteRole());
//holder.imageView.setImageResource(Integer.parseInt(myList.get(position).getSqliteImage()));
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String first_name = listItems.getSqliteFirstName();
String last_name = listItems.getSqliteSecondName();
String job_role = listItems.getSqliteRole();
//String image_url = listItems.get();
//String id= String.valueOf(listItems.getSqliteId());
//Log.v("id",id);
Intent i = new Intent(mContext, NoInternetDetailedActivity.class);
i.putExtra("firstName",first_name);
i.putExtra("lastName",last_name);
i.putExtra("jobRole",job_role);
//i.putExtra("imageUrl",image_url);
//i.putExtra("memberId",id);
mContext.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return myList.size();
}
//ViewHolder pattern used to read the data of the row fast.
class RowHolderClass extends RecyclerView.ViewHolder {
protected NetworkImageView imageView;
protected TextView fName, lName, jobRole;
protected RelativeLayout relativeLayout;
public RowHolderClass(View itemView) {
super(itemView);
this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.recLayout);
this.imageView = (NetworkImageView) itemView.findViewById(R.id.imageView);
this.fName = (TextView) itemView.findViewById(R.id.firstName);
this.lName = (TextView) itemView.findViewById(R.id.lastName);
this.jobRole = (TextView) itemView.findViewById(R.id.roleView);
}
}
}
Ι display the rest of the data inside the onBindViewHolder as
holder.fName.setText(myList.get(position).getSqliteFirstName());
holder.lName.setText(myList.get(position).getSqliteSecondName());
holder.jobRole.setText(myList.get(position).getSqliteRole());
I think I need to convert the URL String into a byte array inside that method. I am not sure. Any ideas?
And something else. When I click on recyclerview's row I get the following exception.
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Context.getPackageName()' on a null object
reference
at android.content.ComponentName.<init>(ComponentName.java:77)
This is how my recyclerview looks like without the image.
public void fetchDataFromDB() {
dba = new DatabaseHandler(getApplicationContext());
ArrayList<SQLiteModel> membersDB = dba.getMembersDetails();
for(int i=0; i<membersDB.size();i++){
int id = membersDB.get(i).getSqliteId();
String fname = membersDB.get(i).getSqliteFirstName();
String lname = membersDB.get(i).getSqliteSecondName();
String role = membersDB.get(i).getSqliteRole();
String image = membersDB.get(i).getSqliteImage();
//String desc = moviesFromDB.get(i).getContent();
SQLiteModel f = new SQLiteModel();
f.setSqliteId(id);
f.setSqliteFirstName(fname);
f.setSqliteSecondName(lname);
f.setSqliteRole(role);
f.setSqliteImage(image);
myarr.add(f);
}
dba.close();
}
}
Use
Picasso
This is very simple library which allows you to download and cache you images.
So in you case you should write something like:
Picasso.with(mContext)
.load(myList.get(position).getSqliteImage())
.into(holder.imageView);
You have there NetworkImageView, that is currently less and less used. I would suggest you to change this to standard ImageView + using Picasso library, then you can load image by:
Picasso.with(holder.image‌​View.getContext()).load(myList.get(position).getUrl()‌​).into(holder.image‌​View);
Thanks to Mateusz Pryczkowski
The answer for the images is:
Picasso.with(holder.imageView.getContext())
.load(myList.get(position).getSqliteImage()).into(holder.imageView);

Changing an Image based on what was clicked

I have successfully passed my title and description using the code below, but when i try to pass my header image, nothing happens. Where is my error?
MyPageHolder.java
public abstract class MyPageHolder extends RecyclerView.ViewHolder{
protected final Activity activity;
protected MyPageAdapter adapter;
public TextView txtTitle, txtDescription;
public ImageView imgImage;
public View view;
public MyPageHolder(View v, Activity activity, MyPageAdapter adapter) {
super(v);
this.activity = activity;
this.adapter = adapter;
imgImage = (ImageView) v.findViewById(R.id.cardview_image);
txtTitle = (TextView) v.findViewById(R.id.cardview_title);
txtDescription = (TextView) v.findViewById(R.id.cardview_description);
view = (CardView) v.findViewById(R.id.card_view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*/ this is where the magic happens when clicked /*/
}
});
}
public void bind(MenuPageItems dataModel, Activity activity, final int position) {
final MenuPageItems m = (MenuPageItems)dataModel;
imgImage.setImageResource(m.image);
txtTitle.setText(m.title);
txtDescription.setText(m.description);
view.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v){
Intent cvIntent = new Intent(view.getContext(), EndpageActivity.class);
cvIntent.putExtra("endpageHeader", m.image);
// text to pass to endpage activity
cvIntent.putExtra("endpageTitle", m.title);
cvIntent.putExtra("endpageTheContent", m.description);
view.getContext().startActivity(cvIntent);
}
});
}
}
EndpageActivity.java
public class EndpageActivity extends AppCompatActivity {
TextView t1;
ImageView iv1;
private void loadBackdrop() {
String uri = "#drawable/p400x200";
// where backdropX.jpg (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
final ImageView imageView = (ImageView) findViewById(R.id.endpage_header_image);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.endpage_layout);
Intent intent = getIntent();
String sTitle = intent.getStringExtra("endpageTitle");
String sTheContent = intent.getStringExtra("endpageTheContent");
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.endpage_header);
collapsingToolbar.setTitle(sTitle);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// this adds the back button arrow to the header
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
iv1 = (ImageView) findViewById(R.id.endpage_header_image);
iv1.setImageDrawable(getResources().getDrawable(getIntent().getIntExtra("endpageHeader", 0)));
t1 = (TextView)findViewById(R.id.endpage_the_content);
t1.setText(sTheContent);
loadBackdrop();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
}
the app runs but only my text changes, the imageview is still showing my placeholder image.
Please try this
iv1.setImageResource(getIntent().getExtras().getInt("endpageHeader"));
If it is also not working,print your
getIntent().getExtras().getInt("endpageHeader")
value in log and check it is of the form `R.drawable.your_image`

Categories

Resources