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`
Related
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.
Please someone help me, I want to display image, text, and detail product from RecyclerView.Adapter<CustomAdapter.ViewHolder> (First Intent) to My Detail Product Second Intent with intent. But I cannot access variable abc on First Intent when I declare in Second Intent.
Please someone help me.
this is RecyclerView.Adapter<CustomAdapter.ViewHolder> or First Intent
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView image;
private TextView text;
private TextView nama;
private TextView harga;
public ViewHolder(final View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
text = (TextView) itemView.findViewById(R.id.text);
nama= (TextView) itemView.findViewById(R.id.nama);
harga = (TextView) itemView.findViewById(R.id.harga);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent abc = new Intent(v.getContext(), DetailProduct.class);
abc.putExtra("abb", my_data.get(getAdapterPosition()).getDetail());
v.getContext().startActivity(abc);
}
});
}
}
And this is my Detail Product or Second Intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_product);
TextView resulttext = (TextView) findViewById(R.id.hasiltext);
String text = abc.getStringExtra("abb"); // i cannot acces variable abc in detailproduct
resulttext.setText(text);
}
The problem is in DetailActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_product);
TextView resulttext = (TextView) findViewById(R.id.hasiltext);
if(getTntent()!=null && getIntent().hasExtra("abb")){
String text = getIntent().getStringExtra("abb");
resulttext.setText(text);
}
}
There are various categories of events given and each category is selected based on which button you click. When a respective button is clicked, for that category, data is called from the Firebase Database in the form of a Recycler View, however if that category is not present in my database, I want show a NoResults Page.
I tried using an if-else condition however it is not working
Categories_Frag.java
public class Categories_Frag extends Fragment implements View.OnClickListener {
ImageView arch, art, business, literature, science, tech;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.categories2, container, false);
arch = (ImageView) view.findViewById(R.id.arch);
art = (ImageView) view.findViewById(R.id.artImg);
business = (ImageView) view.findViewById(R.id.businessImg);
literature = (ImageView) view.findViewById(R.id.literatureImg);
science = (ImageView) view.findViewById(R.id.scienceImg);
tech = (ImageView) view.findViewById(R.id.techImg);
arch.setOnClickListener(this);
art.setOnClickListener(this);
business.setOnClickListener(this);
literature.setOnClickListener(this);
science.setOnClickListener(this);
tech.setOnClickListener(this);
return view;
}
public void onClick(View view){
Intent i = new Intent(getActivity(), EventView.class);
switch(view.getId()) {
case R.id.arch:
String architecture = "Architecture";
i.putExtra("Category", architecture);
break;
case R.id.artImg:
String art = "Art";
i.putExtra("Category", art);
break;
case R.id.businessImg:
String business = "Business";
i.putExtra("Category", business);
break;
case R.id.literatureImg:
String literature = "Literature";
i.putExtra("Category", literature);
break;
case R.id.scienceImg:
String science = "Science";
i.putExtra("Category", science);
break;
case R.id.techImg:
String technology = "Technology";
i.putExtra("Category", technology);
break;
}
startActivity(i);
}
}
EventView.java
public class EventView extends AppCompatActivity {
DatabaseReference databaseReference;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_view);
databaseReference = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");
recyclerView = (RecyclerView) findViewById(R.id.request_EventList);
//Avoid unnecessary layout passes by setting setHasFixedSize to true
recyclerView.setHasFixedSize(true);
//Select the type of layout manager you would use for your recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
final String event_cat = getIntent().getStringExtra("Category");
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setTitle(event_cat);
getSupportActionBar().setTitle(event_cat);
databaseReference.orderByChild("category").equalTo(event_cat).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot !=null){
startShowing();
}
else{
Intent i = new Intent(getApplicationContext(), NoResults.class);
startActivity(i);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void startShowing() {
final String event_cat = getIntent().getStringExtra("Category");
FirebaseRecyclerAdapter<Event, EventView.RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventView.RequestViewHolder>(
Event.class,
R.layout.events_list_row,
EventView.RequestViewHolder.class,
databaseReference.orderByChild("category").equalTo(event_cat)
) {
#Override
protected void populateViewHolder(EventView.RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setCategory(model.getCategory());
viewHolder.setLocation(model.getLocation());
viewHolder.setPrice(model.getPrice());
viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());
viewHolder.setEvent_user_image(getApplicationContext(), model.getEvent_user_image());
viewHolder.setEvent_username(model.getEvent_username());
viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(EventView.this, EventDetails.class);
startActivity(intent);
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView imageButton;
public RequestViewHolder(View itemView) {
super(itemView);
mView = itemView;
imageButton = (ImageView) mView.findViewById(R.id.request_image);
}
public void setTitle(String title) {
TextView a_title = (TextView) mView.findViewById(R.id.request_title);
a_title.setText(title);
}
public void setLocation(String location) {
TextView a_desc = (TextView) mView.findViewById(R.id.request_location);
a_desc.setText(location);
}
public void setCategory(String category) {
TextView a_category = (TextView) mView.findViewById(R.id.request_category);
a_category.setText(category);
}
public void setPrice(String price) {
TextView a_price = (TextView) mView.findViewById(R.id.request_price);
a_price.setText(price);
}
public void setImageUrl(Context ctx, String imageUrl) {
ImageView a_image = (ImageView) mView.findViewById(R.id.request_image);
Picasso.with(ctx).load(imageUrl).into(a_image);
}
public void setEvent_username(String event_username) {
TextView a_event_username = (TextView) mView.findViewById(R.id.userName);
a_event_username.setText(event_username);
}
public void setEvent_user_image(Context ctx, String event_user_image) {
CircleImageView a_event_user_image = (CircleImageView) mView.findViewById(R.id.event_user_image);
Picasso.with(ctx).load(event_user_image).into(a_event_user_image);
}
}
}
NoResults.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NoResults extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_no_results);
}
}
activity_no_results.xml
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/noresults">
</ImageView>
I have two activities, one shows the movie list, and the other shows movie details. I have a shared element transition on a movie's poster. It works fine when I use with linearlayoutmanager, but when I use a gridLayoutManager for the recyclerView, after I press back button get back from the detail activity, the other image becomes blank as shown here.image
In this picture, I clicked on x-men's movie poster, but the image next to it becomes blank.
I have set a unique transitionName for every item. It's probably not the reason.
this is my activity
public class MainActivity extends AppCompatActivity implements MovieGridAdapter.OnItemClickListener {
private MovieGridAdapter mAdapter;
private int mPosition;
private Toolbar mToolbar;
private RecyclerView mMovieGridLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mMovieGridLayout = (RecyclerView) findViewById(R.id.movie_grid);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
mMovieGridLayout.setLayoutManager(gridLayoutManager);
mAdapter = new MovieGridAdapter();
mAdapter.setOnItemClickListener(this);
mMovieGridLayout.setAdapter(mAdapter);
}
#Override
protected void onStart() {
super.onStart();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String sortBy = sharedPreferences.getString(getString(R.string.sort_by_key), getString(R.string.popularity));
MoviePuller.getMoviePuller().discoverMovies(sortBy);
}
#Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingActivity.class);
startActivity(intent);
return true;
} else if (id == R.id.action_refresh) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String sortBy = sharedPreferences.getString(getString(R.string.sort_by_key), getString(R.string.popularity));
MoviePuller.getMoviePuller().discoverMovies(sortBy);
}
return super.onOptionsItemSelected(item);
}
#Subscribe
public void onPullSuccessEvent(Integer eventId) {
if (eventId == EventId.PULL_SUCCESS) {
mAdapter.notifyDataSetChanged();
}
}
#Override
public void onItemClick(View v, int position) {
mPosition = position;
Intent intent = new Intent(this, MovieDetailActivity.class);
ImageView imageView = (ImageView) v.findViewById(R.id.poster);
TextView title = (TextView) v.findViewById(R.id.title);
ActivityOptionsCompat scaleUpAnimationOptions = createScaleUpAnimationOptions(imageView,title);
intent.putExtra(Constants.POSITION, position);
ActivityCompat.startActivity(this, intent, scaleUpAnimationOptions.toBundle());
}
private ActivityOptionsCompat createScaleUpAnimationOptions(View view, View view2) {
Pair<View,String> pair = new Pair<>(view,view.getTransitionName());
Pair<View,String> pair2 = new Pair<>(view2,view2.getTransitionName());
Pair<View,String> pair3 = new Pair<View, String>(mToolbar,mToolbar.getTransitionName());
// View statusBar = getWindow().getDecorView().findViewById(android.R.id.navigationBarBackground);
// Pair<View,String> pair4 = new Pair<>(statusBar, ViewCompat.getTransitionName(statusBar));
return ActivityOptionsCompat.makeSceneTransitionAnimation(this,pair,pair2,pair3);
}
}
this is my adapter
public class MovieGridAdapter extends RecyclerView.Adapter<MovieGridAdapter.MyHolder> {
private JSONArray mMovies;
private OnItemClickListener mOnItemClickListener;
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie, parent, false);
return new MyHolder(cardView,mOnItemClickListener);
}
#Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.bind(position);
}
#Override
public int getItemCount() {
mMovies = MoviePuller.getMovies();
if (mMovies != null) {
return mMovies.length();
}
return 0;
}
public interface OnItemClickListener {
void onItemClick(View v, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}
class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView mPoster;
public TextView mTitle;
private OnItemClickListener mOnItemClickListener;
public MyHolder(View itemView, OnItemClickListener listener) {
super(itemView);
mOnItemClickListener = listener;
mTitle = (TextView) itemView.findViewById(R.id.title);
mPoster = (ImageView) itemView.findViewById(R.id.poster);
itemView.setOnClickListener(this);
}
public void bind(int position) {
if (mMovies != null) {
try {
JSONObject movie = mMovies.getJSONObject(position);
String title = movie.getString(Constants.KEY_TITLE);
String posterPath = movie.getString(Constants.KEY_POSTER_PATH);
Picasso.with(itemView.getContext()).
load(MoviePuller.POSTER_BASE_URL + posterPath).into(mPoster);
mPoster.setTransitionName(itemView.getResources().getString(R.string.transition_poster) + position);
mTitle.setText(title);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
#Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
}
this is my detail activity
public class MovieDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moviedetail);
Slide slide = new Slide(Gravity.BOTTOM);
slide.addTarget(R.id.divider);
slide.addTarget(R.id.vote_average);
slide.addTarget(R.id.release_date);
slide.addTarget(R.id.plot_synopsis);
slide.addTarget(R.id.rating);
slide.addTarget(R.id.divider1);
getWindow().setEnterTransition(slide);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ImageView poster = (ImageView) findViewById(R.id.poster);
TextView tvTitle = (TextView) findViewById(R.id.title);
RatingBar voteAverage = (RatingBar) findViewById(R.id.vote_average);
TextView tvReleaseDate = (TextView) findViewById(R.id.release_date);
TextView tvPlotSynopsis = (TextView) findViewById(R.id.plot_synopsis);
TextView tvRating = (TextView) findViewById(R.id.rating);
int position = getIntent().getIntExtra(Constants.POSITION, -1);
poster.setTransitionName(getString(R.string.transition_poster) + position);
JSONArray movies = MoviePuller.getMovies();
if(movies != null && movies.length() > position) {
try {
JSONObject movie = movies.getJSONObject(position);
String title = movie.getString(Constants.KEY_TITLE);
String poster_path = movie.getString(Constants.KEY_POSTER_PATH);
String release_date = movie.getString(Constants.KEY_RELEASE_DATE);
String vote_average = movie.getString(Constants.KEY_VOTE_AVERAGE);
String overview = movie.getString(Constants.KEY_OVERVIEW);
String id = movie.getString(Constants.KEY_ID);
MoviePuller.getMoviePuller().getTrailers(id);
MoviePuller.getMoviePuller().getReviews(id);
toolbar.setTitle(title);
setSupportActionBar(toolbar);
Picasso.with(this).load(MoviePuller.POSTER_BASE_URL+poster_path).into(poster);
tvTitle.setText(title);
tvReleaseDate.setText(release_date);
tvPlotSynopsis.setText(overview);
voteAverage.setRating(Float.parseFloat(vote_average)/2);
tvRating.setText(vote_average);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
#Subscribe
public void onEvent(EventId eventId){
if(eventId.mId == EventId.GET_TRAILER_SUCCESS){
JSONArray trailerArray = (JSONArray) eventId.mEventObject;
}else if(eventId.mId == EventId.GET_REVIEW_SUCCESS){
JSONArray reviewArray = (JSONArray) eventId.mEventObject;
}
}
}
the recyclerView xml is like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/movie_grid"/>
</RelativeLayout>
What should i do to avoid this?
i record a gif
I guess the problem is with the way you are passing positions of the views between DetailActivity and the MainActivity.
In MainActivity onItemClick() method replace this
intent.putExtra(Constants.POSITION, position);
with this
Bundle bundle = new Bundle();
bundle.putInt(Constants.POSITION, position);
intent.putExtras(bundle);
And in DetailActivity replace this
int position = getIntent().getIntExtra(Constants.POSITION, -1);
with this
int position = getIntent().getExtras().getInt(Constants.POSITION);
Also, what is itemView in MyHolder class, bind() method. You need to pass context to the adapter and use that.
Create this constructor in MovieGridAdapter :
public MovieGridAdapter(Context context) {
this.context = context;
}
and In bind() method replace occurences of itemView:
Picasso.with(context).load(MoviePuller.POSTER_BASE_URL + posterPath).into(mPoster);
mPoster.setTransitionName(context.getString(R.string.transition_poster) + position);
Initialize MovieGridAdapter in MainActivity in this way:
mAdapter = new MovieGridAdapter(this);
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");