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.imageView.getContext()).load(myList.get(position).getUrl()).into(holder.imageView);
Thanks to Mateusz Pryczkowski
The answer for the images is:
Picasso.with(holder.imageView.getContext())
.load(myList.get(position).getSqliteImage()).into(holder.imageView);
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.
I have implemented the firebase recycler like this
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String REQUIRED = "Required";
// [START declare_database_ref]
private DatabaseReference mDatabase;
// [END declare_database_ref]
private FirebaseAuth mAuth;
private static String LOG_TAG = "CardViewActivity";
public static class PostHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View mView;
public PostHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.name);
field.setText(name);
}
public void setText(String text) {
TextView field = (TextView) mView.findViewById(R.id.body);
field.setText(text);
}
public void setImage(String image){
ImageView pp = (ImageView) mView.findViewById(R.id.image);
Picasso.with(Application.getAppContext()).load(image).placeholder(R.drawable.nodp).error(R.drawable.nodp).transform(new CircleTransform()).into(pp);
}
#Override
public void onClick(View mView) {
//what to do here
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
findViewById(R.id.progressBar3).setVisibility(View.VISIBLE);
findViewById(R.id.nopost).setVisibility(View.GONE);
//mListview = (ListView) findViewById(R.id.listview);
mAuth = FirebaseAuth.getInstance();
//the post list shit is happening here
final DatabaseReference root = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference postRef = root.child("users").child(user.getUid().toString()).child("posts");
RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerview);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) {
#Override
public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else
//find its exception and catch it
try {
String firstname = postList.getFirstname();
String lastname = postList.getLastname();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
postViewHolder.setName(name);
postViewHolder.setText(postList.getBody()); // set the vew holder
//note that picasso view holder was applied in the view holder instead
String image = postList.getImgUrl().toString();
postViewHolder.setImage(image);
findViewById(R.id.progressBar3).setVisibility(View.GONE);
}
catch(NullPointerException e) {
findViewById(R.id.nopost).setVisibility(View.VISIBLE);
}
}
};
recycler.setAdapter(mAdapter);
}
}
and it works perfectly for my use case, I have gone through questions about how to set up onClick listeners for recycler adapter, and I think I've got it as seen in my code above.
My question is:
minor issue: when an item is clicked, I want to pass data through an intent to another activity to display details about the post. I can achieve this through intent.putExtra() on my ViewHolder as referenced by another question. But my problem is that my view holder class is outside the activity's onCreate() (that's the only way it seemed to work) so I can't call start activity from within ViewHolder,
major issue: since PostList, from which my data is from, is not yet set in the ViewHolder, so I can't put postList.getName() and getBody() in an intent, but I think it would be okay if my onClick() can go inside the firebase block itself, here:
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) {
#Override
public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else
//find its exception and catch it
try {
String firstname = postList.getFirstname();
String lastname = postList.getLastname();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
postViewHolder.setName(name);
postViewHolder.setText(postList.getBody()); // set the vew holder
//note that picasso view holder was applied in the view holder instead
String image = postList.getImgUrl().toString();
postViewHolder.setImage(image);
findViewById(R.id.progressBar3).setVisibility(View.GONE);
}
catch(NullPointerException e) {
findViewById(R.id.nopost).setVisibility(View.VISIBLE);
}
}
//onclick goes here with all the put extra and startactivity stuff
};
recycler.setAdapter(mAdapter);
You mean that you cannot get the origin activity to create the intent? have you tried getContext(), getActivity() or getBaseContext()?
Context context = getContext();
final Intent intent = new Intent(context, FirstActivity.class);
// all your stuff
context.startActivity(intent);
If you want to perform good item click functionality with a proper way then create a separate class for FirebaseRecyclerAdapter and use Interface as item click listener so you can perform intent functionality from HomeActivity.
OR
Create reference of HomeActivity class and use that reference at
#Override
public void onClick(View mView) {
//what to do here
//mcontect will be your class reference
Intent i = new (mcontext,SecondActivity.class);
startActivity(i);
}
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");
I am using a Recyclerview and want to add images in it which works fine. I want to use int array of drawables in my resource folder and load images from a set folder on my sdcard. I have a string array which lists paths of all images and sets a textview in my cardview but i can't convert that into an int array to load the images.
Here is my code, i have commented out the string array and int arrays that load the drawable.resources and textviews which are hardcoded.
If you need any more info/code then i can post it. Thanks in advance.
The feed.setMyFiles(previewsPath [i]); works fine.
The feed.setThumbnail(intarray[i]); is where im stuck.
MainActivity
public class MainCardActivity extends ActionBarActivity {
private RecyclerView recyclerView;
private CardView cardView;
private ArrayList<FeedProperties> os_versions;
private RecyclerView.Adapter mAdapter;
// private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincardlayout);
initContrls();
}
private void initContrls() {
//cardView = (CardView) findViewById(R.id.cardList);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// final String[] versions = {"Synergy", "Synergy", "Synergy", "Synergy", "Synergy", "Synergy", "Synergy"};
// final int[] previewsImages = {R.drawable.psss, R.drawable.psss, R.drawable.psss, R.drawable.psss, R.drawable.sss, R.drawable.dog, R.drawable.sss, R.drawable.psss, R.drawable.dog, R.drawable.dog, R.drawable.sss, R.drawable.psss};
// Check for count
os_versions = new ArrayList<FeedProperties>();
File pathToFiles = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Overlays/Previews");
String[] previewsPath = pathToFiles.list();
//ArrayList<String> filePaths;
//populate filePaths with your list of file paths.
//feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
//int intarray[] = new int[previewsPath.length];
// for (int i = 0; i < previewsPath.length; i++) {
// FeedProperties feed = new FeedProperties();
ArrayList<String> filePaths = new ArrayList<>();
for (String s : pathToFiles.list()) {
filePaths.add(s); }
for (int i = 0; i < previewsPath.length; i++) {
FeedProperties feed = new FeedProperties();
feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
feed.setMyFiles(previewsPath [i]);
//feed.setMyFiles(previewsPath [i]);
// intarray[i] = Integer.parseInt(previewsPath[i]);
// feed.setThumbnail(intarray[i]);
os_versions.add(feed);
}
recyclerView.setHasFixedSize(true);
// ListView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Grid View
// recyclerView.setLayoutManager(new GridLayoutManager(this,2,1,false));
//StaggeredGridView
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));
mAdapter = new CardViewDataAdapter(os_versions);
recyclerView.setAdapter(mAdapter);
}
}
Model class
public class FeedProperties {
private String title;
private String myfiles;
private int thumbnail;
public String getTitle() {
return title;
}
public String getMyFiles() {
return myfiles;
}
public void setMyFiles(String myfiles) {
this.myfiles = myfiles;
}
public void setTitle(String title) {
this.title = title;
}
public int getThumbnail() {
return thumbnail;
}
public void setThumbnail(int thumbnail) {
this.thumbnail = thumbnail;
}
}
Adapter Class
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
private static ArrayList<FeedProperties> dataSet;
public CardViewDataAdapter(ArrayList<FeedProperties> os_versions) {
dataSet = os_versions;
}
#Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, null);
//create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {
FeedProperties fp = dataSet.get(i);
viewHolder.tvVersionName.setText(fp.getTitle());
viewHolder.tvmyfilesName.setText(fp.getMyFiles());
viewHolder.iconView.setImageResource(fp.getThumbnail());
viewHolder.feed = fp;
}
#Override
public int getItemCount() {
return dataSet.size();
}
#Override
public long getItemId(int position) {
return position;
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvVersionName;
public TextView tvmyfilesName;
public ImageView iconView;
public FeedProperties feed;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvVersionName = (TextView) itemLayoutView.findViewById(R.id.tvVersionName);
tvmyfilesName = (TextView) itemLayoutView.findViewById(R.id.tvmyfilesName);
iconView = (ImageView) itemLayoutView.findViewById(R.id.iconId);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), SecondPage.class);
v.getContext().startActivity(intent);
Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
ArrayList<String> filePaths = new ArrayList<>();
for (String s : pathToFiles.list()) {
filePaths.add(s);
}
for (int i = 0; i < previewsPath.length; i++) {
FeedProperties feed = new FeedProperties();
feed.setThumbnail(Drawable.createFromPath(filePaths.get(i)));
feed.setMyFiles(previewsPath [i]);
//feed.setMyFiles(previewsPath [i]);
//intarray[i] = Integer.parseInt(previewsPath[i]);
//feed.setThumbnail(intarray[i]);
os_versions.add(feed);
}
have you tried using the picasso library? here's a tutorial http://www.101apps.co.za/index.php/articles/using-bitmaps-efficiently.html. There's also a link in this tutorial showing how to use picasso and recyclerview. And here's a link to Picasso's web page http://square.github.io/picasso/. Good luck
I have the following RecyclerView adapter in place. It's working fine to display the items however I want to pass arguments to a Dialog or Fragment when I click one of the items:
public class NovaAdapter extends RecyclerView.Adapter<NovaListRowHolder> {
ArrayList<HashMap<String, String>> novaList = new ArrayList<HashMap<String, String>>();
public static final String STATUS = "status";
public static final String NAME = "name";
public static final String ID = "id";
private Context mContext;
public NovaAdapter(Context context, ArrayList<HashMap<String, String>> novaList) {
this.novaList = novaList;
this.mContext = context;
}
#Override
public NovaListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.instances_list, null);
NovaListRowHolder mh = new NovaListRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(NovaListRowHolder novaListRowHolder, int i) {
HashMap<String, String> e = novaList.get(i);
novaListRowHolder.name.setText(e.get(NAME));
novaListRowHolder.status.setText(e.get(STATUS));
novaListRowHolder.setId(e.get(ID));
}
#Override
public int getItemCount() {
return (null != novaList ? novaList.size() : 0);
}class NovaListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView name;
protected TextView status;
protected String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public NovaListRowHolder(View view) {
super(view);
view.setOnClickListener(this);
this.name = (TextView) view.findViewById(R.id.nameInstance);
this.status = (TextView) view.findViewById(R.id.statusInstance);
}
public void onClick(View view){
Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(R.layout.instances_listdetail);
dialog.setTitle("Details " + name.getText() + " " + getPosition());
dialog.show();
}
At the moment the click is only opening a placeholder dialog but it isn't passing any data. I tried to search everywhere for examples or tutorials on how to do it but didn't find it anywhere.
Sorry in advance it it's too obvious but it's my first app and I don't have much experience.
You should set the onClickListener in onBindViewHolder to each of the views. You can access the itemView directly from your viewHolder.
Now you can set a tag (or a few) to the view with the click listener, and retrieve them inside your onClick method.
If you need to pass values from recycler view to dialog means then use this
code of onBindViewHolder
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Offers offers = offersList.get(position);
holder.topic.setText(offers.getTopic());
holder.expiry.setText(offers.getExpiry());
}
Simply pass the values of by setText
//TextView inside dialog
offers_dialog_topic = (TextView) dialog.findViewById(R.id.offers_dialog_offer_type);
date_year = (TextView) dialog.findViewById(R.id.date_year);
offers_dialog_topic.setText(topic.getText().toString());
date_year.setText(expiry.getText().toString());
Hope this helps for other.
Thank you.