I have prepared a Database in my asset folder . I have one recyclerView for showing the title in the database . Now i want show DetailActivity with specificid . my means is show description of post . I think it can be done with intent , but i don't know how .
public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {
private Context ctx;
private List<Post> posts;
//constructor
public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
this.ctx = ctx;
this.posts= posts;
}
//ViewHodler Pattern
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
intent.putExtra("postId" , "id");
itemView.getContext().startActivity(intent);
}
}
//OncreateViewHolder
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
//OnBindViewHolder
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Post current = Post.get(position);
holder.txtTitle.setText(current.getTitle());
}
//getItemCount method
#Override
public int getItemCount() {
return posts.size();
}
}
i have a 3 columns in my database
1.id 2.title 3.desc
and 3 methods in my Post model Class :
getId() 2.getTitle() 3.getDesc()
as you can see i can send intent to DetailActivity . But i don't know how can i achieve to sending specific id to DetailActivity. thanks
Make one Variable id in ViewHolder and assign that in onBindViewHolder() , I updated the code according to that.
public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {
private Context ctx;
private List<Post> posts;
//constructor
public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
this.ctx = ctx;
this.posts= posts;
}
//ViewHodler Pattern
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView txtTitle;
private int id;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
// here pass id through intent
intent.putExtra("postId" , id);
itemView.getContext().startActivity(intent);
}
}
//OncreateViewHolder
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
//OnBindViewHolder
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Post current = Post.get(position);
holder.txtTitle.setText(current.getTitle());
// add this line
holder.id = Post.get(postion).getId();
}
//getItemCount method
#Override
public int getItemCount() {
return posts.size();
}
}
How can i achieve to sending specific id to DetailActivity
Use setTag method of holder.txtTitle TextView for storing id with TextView and call getTag inside onClick of View Click like:
1. In onBindViewHolder save current using setTag:
holder.txtTitle.setTag(current);
holder.txtTitle.setText(current.getTitle());
2. In onClick of View use getTag as:
#Override
public void onClick(View v) {
txtTitle= (TextView)v.findViewById(R.id.txtTitle);
Post current=(Post)txtTitle.getTag();
intent.putExtra("postId" , current.getId());
intent.putExtra("postTitle" , current.getTitle());
....
}
Related
Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)
attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);
This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.
Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).
My recyclerView:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
}
}
}
This is example:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
your Activity. in Oncreate()
public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(int position) {
// code here
}
}
Create an interface something like
public interface OnSingleItemClickListener{
void onSingleItemClick(int position);
}
Then implement it on your ViewHolder like this
public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener {
public ViewHolder(View itemView) {
super(itemView);
}
#Override
void onSingleItemClick(int position){
if(listItems.get(position) == listItems.get(getAdapterPosition)){
// TODO do something here
}
}
now on your OnBindViewHolder inside your adapter you must do this.
holder.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
holder.onSingleItemClick(position);
}
}):
I am using a RecyclerView in my project and I am having an issue where my RecyclerView is being populated by the same values as if the values are being overwritten inside the forEach in onBindViewHolder below:
class ProfListAdapter extends RecyclerView.Adapter<ViewHolder>{
private LayoutInflater mLayoutInflater;
public ProfListAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
return new ViewHolder(mLayoutInflater
.inflate(R.layout.recycler_view, viewGroup, false));
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position){
System.out.println(position);
final String profName;
Integer k = 0;
for (Integer key : mData.keySet()) {
viewHolder.setData(mData.get(key));
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent intent = ProfPager.newIntent(getActivity(), position);
startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mData.size();
}
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
private ViewHolder(View itemView){
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id._prof); // galleryimage
}
private void setData(String profName){
mTextView.setText(profName);
System.out.println("setData" + profName);
}
}
When I log out to the console I can see that the value in setData() is getting the correct item to display. However, what is being displayed inside the fragment is just the last profName that is comes across in the forEach. What could be causing this behaviour? Here is a link to the entire fragment: https://pastebin.com/xS7CSbbK
If I use the position, it does not always coincide with the key in the Hashmap that stores the SQLite _id.
Setting data on ViewHolder
You may not need the for each loop inside the onBindViewHolder, instead try to set data using the position value available at onBindViewHolder. cheers :)
For RecyclerView , use ArrayList for simplicity
And change your onBindViewHolder like this
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position){
System.out.println(position);
viewHolder.setData(mData.get(position));
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent intent = ProfPager.newIntent(getActivity(), position);
startActivity(intent);
}
});
}
I assume mData as ArrayList
I make app with a couple of different categories of pictures. I want to display those pictures with RecyclerView.
I want to display button as a image when I clicked button before.
Can I make it in one adapter? Now I have two adapters, that code is as follow:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private int[] images = {
R.drawable.imageA1, .... R.drawable.imageA10
};
private int[] images2 = {
R.drawable.imageB1, .... R.drawable.imageB10
};
class ViewHolder extends RecyclerView.ViewHolder{
public int currentItem;
public ImageView itemImage, itemImage2;
public TextView itemTitle;
public ViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView)itemView.findViewById(R.id.imageView);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.article_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.itemImage.setImageResource(images[i]);
}
#Override
public int getItemCount() {
return images.length;
}
}
You can set method in adapter that you will call when users clicks button:
// for button A
public void setImages() {
this.showImages = true;
}
// for button B
public void setImages2() {
this.showImages = false;
}
Then modify your on bind method to bind appropriate images:
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
if (this.showImages)
viewHolder.itemImage.setImageResource(images[i]);
else
viewHolder.itemImage.setImageResource(images2[i]);
}
You need to pass images array dynamically to your adapter as below
private int[] images = {
R.drawable.imageA1, .... R.drawable.imageA10
};
private int[] images2 = {
R.drawable.imageB1, .... R.drawable.imageB10
};
on click of button A
RecyclerAdapter myAdapter = new RecyclerAdapter(images)
yourRecyclerview.setAdapter(myAdapter )
on click of button B
RecyclerAdapter myAdapter = new RecyclerAdapter(images2)
yourRecyclerview.setAdapter(myAdapter )
change in your adapter as following
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private int[] images = new int[];
public RecyclerAdapter(int[] imgs){
images = imgs;
}
class ViewHolder extends RecyclerView.ViewHolder{
public int currentItem;
public ImageView itemImage, itemImage2;
public TextView itemTitle;
public ViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView)itemView.findViewById(R.id.imageView);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.article_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.itemImage.setImageResource(images[i]);
}
#Override
public int getItemCount() {
return images.length;
}
}
Yes possible. make constructor in adpater like that:
public RecyclerAdapter(Context context,int[] images)
{
this.diff_images=images;
this.context=context;
}
the diff_images is also int array in adapter class.
and make images array in your activity class. when you press button one then pass image1 array in adpater and if when press button2 then pass image2 array (if your want to show same view for both cases and if you want to show different view for both images)
if your adapter already set like:
recycleViewAdapter = new RecyclerAdapter(this, imageArray);
recyclerView.setAdapter(recycleViewAdapter);
then you can also make one more method in adapter class and only notify adapter for update image
public void setNewImageList(Context context, int[] imageArray) {
this.context = context;
this.diff_images = imageArray;
notifyDataSetChanged();
}
try this.
I have a recycler view and I am implementing OnClicklistener inside it. Basically , I have an adapter class called actressadapter and a viewholder class MyViewHolder.I am implementing OnClickListener inside viewholder class to initiate another activity via intent method. My basic data is inside a class called actress which has three variables name,country(both String) and an Id(UUID). I am providing this data that is of actresses to my adapter.Next activity is just displaying the name of actress like abc ,def etc that it retrieves . The fault is that it shows name of only one actress after clicking. For instance if it shows abc ,then for each click it will show abc.Don't know why is this happening because as per code I am passing actressname as an extra .
public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private Actress actress,mActress;
public static final String str="shivam.panwar.actressdetails.actressadapter.str";
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView naam, desh;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,actress.getName());
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}
public actressadapter(List<Actress> al) {
this.al = al;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.actress_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
}
#Override
public int getItemCount() {
return al.size();
}
}
If required any further assist about code please comment.
try this way hope it works...
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
holder.naam.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,holder.naam.getText().toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,Actressview.class);
intent.putExtra(str,holder.naam.getText().toString());
context.startActivity(intent);
}
});
}
The best way to use the method getLayoutPosition() of adapter class to get the selected item(position).
You have to update your code for better result. Replace your existing code with that.
public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private Actress actress,mActress;
private int itemPosition; // change
public static final String str="shivam.panwar.actressdetails.actressadapter.str";
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView naam, desh;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
itemPosition = getLayoutPosition(); // change
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,al.get(itemPosition).getName()); // change
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}
public actressadapter(List<Actress> al) {
this.al = al;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.actress_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
}
#Override
public int getItemCount() {
return al.size();
}
}
Hope it will work..
your issue is that you made actress a variable of the Adapte, and it should be of the Holder.
To fix it:
Delete the line:
private Actress actress,mActress;
Change the onBindViewHolder method to:
holder.bindactress(al.get(position))
And make replace your holder class with that:
// holder HAVE TO be static. It's WRONG not doing it static
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView naam, desh;
Actress actress;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,actress.getName());
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
this.actress = mActress;
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}
Im trying show a toast msg when clicked on a item from my RecycleView, Ive tried many examples,
but its not giving me anything. Can somebody give me a different example that i can follow, at the end i wanna set the onClick to show a new fragment. If I can get an example on that, it will be great.
Im using this code:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private List<Movie> movies;
private int card_layout;
private Context mContext;
public MovieAdapter(List<Movie> movies, int card_layout, Context context) {
this.movies = movies;
this.card_layout = card_layout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(card_layout, viewGroup, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final Movie movie = movies.get(i);
viewHolder.movieImage.setImageDrawable(mContext.getDrawable(movie.getImageResourceId(mContext)));
viewHolder.movieName.setText(movie.mName);
viewHolder.currentMovie = movie;
}
#Override
public int getItemCount(){
return movies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView movieName;
public ImageView movieImage;
public Movie currentMovie;
public ViewHolder( View itemView) {
super(itemView);
movieName = (TextView) itemView.findViewById(R.id.movieName);
movieImage = (ImageView)itemView.findViewById(R.id.movieImage);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View itemView){
Toast.makeText(itemView.getContext(),currentMovie.mName,Toast.LENGTH_SHORT ).show();
}
});
}
}
}
Do I have to implement something in my MainActivity as well?
and please dont get mad with me, Im just a starting with all this. All your help will be appriciated. thanks
Layout is not clickable by default. to make clickable add setClickable to true :
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
card_layout, viewGroup, false);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
After wasting an hour of time, I found the most appropriate and simplest solution to this problem:
Try this, it will definitely work.No matters whether cards are used in grids or not.
RecyclerView Adapter:
ProductCardRecyclerViewAdapter.java
public class ProductCardRecyclerViewAdapter extends RecyclerView.Adapter<ProductCardViewHolder> {
public final String TAG=getClass().getSimpleName();
Context context;
private List<ProductEntry> productList;
private Integer[] cardImages;
String[] cardTitle;
String[] cardSubtitle;
public ProductCardRecyclerViewAdapter(Context context, Integer[] imageList, String[] cardTitle, String[] cardSubtitle) {
this.cardImages = imageList;
this.cardTitle = cardTitle;
this.cardSubtitle = cardSubtitle;
this.context = context;
}
#NonNull
#Override
public ProductCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card, parent, false);
return new ProductCardViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull ProductCardViewHolder holder, int position) {
// TODO: Put Recycler ViewHolder Cards binding code here in MDC-102
holder.imgCard.setImageResource(cardImages[position]);
holder.productTitle.setText(cardTitle[position]);
holder.productPrice.setText(cardSubtitle[position]);
holder.productCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Material Card clicked "+cardTitle[position]+" : "+context.getClass());
//TODO: Perform card clicked working
Context c = v.getContext();
Toast.makeText(c, cardTitle[position], Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return cardImages.length;
}
}
RecyclerViewHolder
ProductCardViewHolder.java
public class ProductCardViewHolder extends RecyclerView.ViewHolder {
CardView productCard;
ImageView imgCard;
public TextView productTitle;
public TextView productPrice;
public ProductCardViewHolder(#NonNull View itemView) {
super(itemView);
imgCard = itemView.findViewById(R.id.product_image);
productTitle = itemView.findViewById(R.id.product_title);
productPrice = itemView.findViewById(R.id.product_price);
productCard=itemView.findViewById(R.id.cardofproducts);
// TODO: Find and store views from itemView
}
}
Hope, it will help a lot.