I can't get the layout.xml to show all of the images, it only shows the image that I added into the android:src="blabla.jpg" and all of the list use "blabla.jpg".
I can show the text from the arraylist perfectly fine, but the problem is only the image so i tried this code
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mItemHolder = new ArrayList<>();
private ArrayList<String> mDescHolder = new ArrayList<>();
private ArrayList<Integer> mImageHolder = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
holder.itemHolder.setText(mItemHolder.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on: " + mItemHolder.get(position));
//ini ke intent
Intent intent = new Intent(mContext, ListDetail.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_ITEM", mItemHolder.get(position));
extras.putString("EXTRA_DESC", mDescHolder.get(position));
extras.putInt("EXTRA_IMAGE", mImageHolder.get(position));
intent.putExtras(extras);
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mItemHolder.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView itemHolder;
ImageView imageHolder;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageHolder = itemView.findViewById(R.id.ImageItem);
itemHolder = itemView.findViewById(R.id.NameItem);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
Any help would be appreciated,
In your onBindViewHolder add:
holder.imageHolder.setImageResource(image);
use the setImageresource property of imageView and get the image from the list of that position inside onBindViewHolder.
holder.imageHolder.setImageResource(mImageHolder.get(position));
Related
I building an app to practice in arraylist and recyclerView.
In the main activity I have "Add" button than I can add things to the arraylit to display in the recyclerView,However,the first time I add new line its working fine,but when I try to add another one,its replacing the first line I added with the new line.
I will be glad if you can point what am I doing wrong,Thank you.
Add Line Activity:
public void AddButton(View view){
if (imageUri != null && !mEditText.getText().toString().isEmpty()){
Intent intent = new Intent(AddCompany.this,MainActivity.class);
intent.putExtra("isAddNewCompany", true);
intent.putExtra("CompanyImage", imageUri.toString());
intent.putExtra("CompanyName", mEditText.getText().toString());
startActivity(intent);
}
}
MainActivity OnCreate with the views:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
ArrayList<String> listTitle = new ArrayList<>();
ArrayList<String> listPicture = new ArrayList<>();
if (intent.getBooleanExtra("isAddNewCompany",false)){
String CompanyImage = intent.getStringExtra("CompanyImage");
String companyName = intent.getStringExtra("CompanyName");
listTitle.add(companyName);
listPicture.add(CompanyImage);
}
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerAdapter adapter = new RecyclerAdapter(this,listPicture,listTitle);
adapter.setClickListener(new RecyclerAdapter.ItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.e("Position Number - ", String.valueOf(position));
Toast.makeText(MainActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
}
RecyclerView Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private final ArrayList<String> mData;
private final ArrayList<String> mImageView;
private final LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
RecyclerAdapter(Context context, ArrayList<String> imageView, ArrayList<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mImageView = imageView;
}
// inflates the row layout from xml when needed
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
String picture = mImageView.get(position);
Picasso.get().load(picture).into(holder.myImageView);
holder.myTextView.setText(animal);
}
// total number of rows
#Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ImageView myImageView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
myImageView = itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
When you start activity ,every time new list instance will be created that's why you got only one value.if you want already added value, use same list instance.
List<String> listTitle;
List<String> listPicture;
public List<String> getLisPictureInstance(){
if(listPicture ==null)
listPicture = new ArrayList<>();
return listPicture ;
}
public List<String> getListTitleInstance(){
if(listTitle ==null)
listTitle = new ArrayList<>()
return listTitle ;
}
if (intent.getBooleanExtra("isAddNewCompany",false)){
String CompanyImage = intent.getStringExtra("CompanyImage");
String companyName = intent.getStringExtra("CompanyName");
getListTitleInstance().add(companyName);
getLisPictureInstance().add(CompanyImage);
}
I am working on this diary app on Android studio and I want people to be able to delete their entries, but I have no idea how.
This is the TextListAdapter (recyclerview):
public class TextListAdapter extends RecyclerView.Adapter<TextListAdapter.TextViewHolder>{
private File[] allTextFiles;
private TimeAgo timeAgo;
private onTextItemListClick onTextItemListClick;
public TextListAdapter(File[] allTextFiles, onTextItemListClick onTextItemListClick) {
this.allTextFiles = allTextFiles;
this.onTextItemListClick = onTextItemListClick;
}
#NonNull
#Override
public TextViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text_item, parent, false);
timeAgo = new TimeAgo();
return new TextViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull TextListAdapter.TextViewHolder holder, int position) {
holder.cardText.setText(allTextFiles[position].getName());
holder.textCardTitle.setText(timeAgo.getTimeAgo(allTextFiles[position].lastModified()));
}
#Override
public int getItemCount() {
return allTextFiles.length;
}
//Inner Class
public class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final Context context;
//Adding components
private TextView textCardTitle;
private Button cardText;
private MaterialButton textCardBtn;
private String recordFile;
private ImageButton deleteText;
public TextViewHolder(#NonNull View itemView) {
super(itemView);
textCardTitle = itemView.findViewById(R.id.text_card_title);
cardText = itemView.findViewById(R.id.card_text);
textCardBtn = itemView.findViewById(R.id.text_card_btn);
context = itemView.getContext();
deleteText = itemView.findViewById(R.id.delete_menu_btn);
textCardBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onTextItemListClick.onClickListener(allTextFiles[getAdapterPosition()], getAdapterPosition());
final Intent intent;
intent = new Intent(context, DiaryEntry.class);
intent.putExtra(Intent.EXTRA_TEXT, cardText.getText());
context.startActivity(intent);
}
}
//Interface
public interface onTextItemListClick{
void onClickListener(File file, int position);
}
}
And this is the fragment it comes from:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textList = view.findViewById(R.id.text_list_view);
//Reversing audioList
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
textList.setLayoutManager(linearLayoutManager);
String path = getActivity().getFilesDir().getAbsolutePath();
File directory = new File(path);
allTextFiles = directory.listFiles();
textListAdapter = new TextListAdapter(allTextFiles, this);
textList.setHasFixedSize(true);
textList.setAdapter(textListAdapter);
}
What can I try next?
First of all you need set the clickListener inside TextViewHolder for deleteText
like this.
deleteText.setOnClickListener(this);
Then inside onClick of your function of the Adaptor
int position = getAdaptorPosition;
allTextFiles.remove(position);
notifyItemRemoved(position);
Hopefully, it will work inform me if found any error i would love to help you.
I am creating a RecyclerView which shows imageview and textview. But Imageview is giving me error when i am setting data in adapter class then it is giving error below:
Imageview cannot be applied to TypedArray
I tried Integer data type also but it is also giving error. Please help to get out of this.
Recycler Adapter Code is here:
public class RecyclerAdpater extends RecyclerView.Adapter<RecyclerAdpater.ViewHolder> {
ArrayList<NewModel>newModels;
Context context;
private int limit = 4;
RecyclerInterface recyclerInterface;
public RecyclerAdpater(ArrayList<NewModel>newModels, Context context){
this.newModels = newModels;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_layout, parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(newModels.get(position).getSociallogo()); //here i am getting error.
holder.textView.setText(newModels.get(position).getSocailtext());
}
#Override
public int getItemCount() {
if(newModels.size() > limit){
return limit;
}else {
return newModels.size();
}
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView textView, viewtext;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.sociallogo);
textView = itemView.findViewById(R.id.socialtext);
imageView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(context, WebviewActivity.class);
intent.putExtra("URL NAME", newModels.get(getAdapterPosition()).getHref());
context.startActivity(intent);
}
}
Fragment code when i am setting the adapter:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_for_you, container, false);
recyclerView = view.findViewById(R.id.recyclerview1);
TypedArray logimages = getResources().obtainTypedArray(R.array.social_image);
String[] logotext = getResources().getStringArray(R.array.social_name);
String [] href = getResources().getStringArray(R.array.social_url);
newModels = new ArrayList<>();
for(int i =0; i<logimages.length();i++) {
NewModel newModel = new NewModel(logimages, logotext[i], href[i]);
newModels.add(newModel);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);
readapter= new RecyclerAdpater(newModels,getContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(readapter);
return view;
}
NewMode.class file:
public class NewModel {
TypedArray sociallogo;
String socailtext;
String href;
public NewModel(TypedArray sociallogo, String socailtext, String href){
this.sociallogo = sociallogo;
this.socailtext= socailtext;
this.href = href;
}
public TypedArray getSociallogo(){
return sociallogo;
}
public String getSocailtext(){
return socailtext;
}
public String getHref(){
return href;
}
}
When I just display string data it seems to show up fine, but when I try to display an int or double the RecyclerView won't populate. I am pulling from the Google places API JSON. I think maybe it is the way I'm using my onBindViewHolder()? Thanks
// MainActivity
private void getJson() {
String URL = https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7085,-74.003124&opennow&radius=5000&type=restaurant&key=
JsonObjectRequest root = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray results = response.getJSONArray("results");
Log.d("RESULTS", String.valueOf(results));
for(int i = 0; i<results.length(); i++) {
JSONObject resultsObj = results.getJSONObject(i);
mImageUrls.add(resultsObj.getString("icon"));
mTitle.add(resultsObj.getString("name"));
//mRating.add(resultsObj.getDouble("rating"));
mPriceLevel.add(resultsObj.getInt("price_level"));
}
getRecyclerView();
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(root);
}
private void getRecyclerView() {
// LinearLayoutManager
recyclerView = findViewById(R.id.recyclerView);
adapter = new RecyclerViewAdapter(mImageUrls, mTitle, mPriceLevel, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
}
And the RecyclerViewAdapter Class is.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> rNames = new ArrayList<>();
private ArrayList<String> rImageUrls = new ArrayList<>();
private ArrayList<Double> rRatings = new ArrayList<>();
private ArrayList<Integer> rPriceLevel = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<String> image, ArrayList<String> name, ArrayList<Integer> priceLevel, Context context) {//, ArrayList<Double> rating
this.rImageUrls = image;
this.rNames = name;
//this.rRatings = rating;
this.rPriceLevel = priceLevel;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
//return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
holder.name.setText(rNames.get(position));
//holder.rating.setText(rRatings.get(position));
holder.priceLevel.setText(rPriceLevel.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));
Toast.makeText(mContext, rNames.get(position), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return rNames.size(); //we can use rImageUrls/rRatings as well because they are all the same size
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
TextView rating;
TextView priceLevel;
TextView address;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
name = (TextView) itemView.findViewById(R.id.title);
//rating = itemView.findViewById(R.id.rating);
priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
The reason for this is you are not converting Double to String
change this line holder.rating.setText(rRatings.get(position)); to holder.rating.setText(String.valueOf(rRatings.get(position)));
I would like to suggest you to create an object having all necessary parameters in it and then pass a single list of that object to the RecyclerViewAdapter.
public class MapElement {
public String name;
public String imageUrl;
public Double rating;
public Integer price;
}
Then create an ArrayList of this object when you get the response from your getJson function call. Now in your adapter just use a single list of MapElement object to show the data from there.
The modified adapter should look like this.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<MapElement> elements = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<MapElement> elements, Context context) {
this.elements = elements;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
holder.name.setText(elements.get(position).name);
holder.rating.setText(elements.get(position).rating);
holder.priceLevel.setText(elements.get(position).price);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));
Toast.makeText(mContext, elements.get(position).name, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return elements.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
TextView rating;
TextView priceLevel;
TextView address;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
name = (TextView) itemView.findViewById(R.id.title);
rating = itemView.findViewById(R.id.rating);
priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
First you should check that the rating you are getting is a double value.
It might be the case that an exception is thrown after this line
mRating.add(resultsObj.getDouble("rating"))
Also as #hooray_todd siggested textview only takes string value so you have to change your data into string.
I made an Adapter for my recyclerView. This adapter works when I use on any xxxActivity.java but when I try to use is on a Fragment Its hows error. Doesn't let me Pass the onClickHandler() that I created in Adapter.
I am Setting Adapter Like this -
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, mClickHandler)); //ClickListener doesn't work :'(
Another try was like --
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, getmClickHandler()));
Here, I implemented getClickHandler() in the Fragment--
public FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler getmClickHandler() {
return mClickHandler;
} //Still doesn't work :'(
and the Adapter Part--
Constructor like this-
public FixedPlaceListAdapter(Context mContext, List<PlaceBean>
placeBeanList, FixedPlaceListAdapterOnclickHandler mClickHandler) {
this.mContext = mContext;
this.placeBeanList = placeBeanList;
this.mClickHandler = mClickHandler;
}
I tried to do this ... but still doesn't work--
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler.mClickhandler));
here is my full adapter code-
public class FixedPlaceListAdapter extends RecyclerView.Adapter<FixedPlaceListAdapter.FixedPlaceListAdapterViewHolder> {
private final FixedPlaceListAdapterOnclickHandler mClickHandler;
Context mContext;
List<PlaceBean> placeBeanList;
public FixedPlaceListAdapter(Context mContext, List<PlaceBean> placeBeanList, FixedPlaceListAdapterOnclickHandler mClickHandler) {
this.mContext = mContext;
this.placeBeanList = placeBeanList;
this.mClickHandler = mClickHandler;
}
public void setData(List<PlaceBean> placeBeanList) {
this.placeBeanList = placeBeanList;
notifyDataSetChanged();
}
#Override
public FixedPlaceListAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.top_list_single, parent, false);
return new FixedPlaceListAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(FixedPlaceListAdapterViewHolder holder, int position) {
PlaceBean pb = placeBeanList.get(position);
holder.nameTextView.setText(pb.getName());
holder.addressTextView.setText(pb.getVicinity());
holder.rating.setRating(pb.getRating());
if (pb.getPhotoref() != null) {
String imageUrl = UrlsUtil.getSinglePhotoUrlString(mContext, pb.getPhotoref(), "350", "300");
Picasso.with(mContext)
.load(imageUrl)
.into(holder.thumbnailImage);
}
}
#Override
public int getItemCount() {
return placeBeanList.size();
}
public interface FixedPlaceListAdapterOnclickHandler {
void onClick(String id);
}
public class FixedPlaceListAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView nameTextView;
TextView addressTextView;
RatingBar rating;
ImageView thumbnailImage;
public FixedPlaceListAdapterViewHolder(View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.place_name_now_in_list);
addressTextView = (TextView) itemView.findViewById(R.id.address_in_list);
rating = (RatingBar) itemView.findViewById(R.id.rating_single_place_in_list);
thumbnailImage = (ImageView) itemView.findViewById(R.id.place_image_thumb);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String placeID = placeBeanList.get(getAdapterPosition()).getPlaceref();
mClickHandler.onClick(placeID);
}
}
}
Need Help!
public class CategoryNewsListAdapter extends RecyclerView.Adapter<CategoryNewsListAdapter.ViewHolder> {
private List<CategoryNews> actors = new ArrayList<>();
private Context mContext;
private Queue<List<CategoryNews>> pendingUpdates =
new ArrayDeque<>();
**public interface NewsItemClickListener {
void onNewsItemClick(int pos, CategoryNews categoryNews, ImageView shareImageView);
}**
**NewsItemClickListener newsItemClickListener;**
public CategoryNewsListAdapter(List<CategoryNews> personList, Context mContext, NewsItemClickListener newsItemClickListener) {
this.actors.addAll(personList);
this.mContext = mContext;
this.newsItemClickListener = newsItemClickListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View view = inflater.inflate(R.layout.particular_cat_news_list_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("BusinessSubCategoryListAdapter", "Bindviewholder without payload");
final CategoryNews newsCategory = actors.get(position);
holder.news_title.setText(newsCategory.getPostTitle());
holder.news_date.setText(newsCategory.getPostDate());
holder.news_category.setText(newsCategory.getCategoryName());
holder.news_description.setText(newsCategory.getPostContent());
// GlideApp
// .with(mContext).load(newsCategory.getPostImage())
// .placeholder(R.mipmap.ic_launcher) // can also be a drawable
// .error(R.drawable.placeholder_image) // will be displayed if the image cannot be loaded
// .crossFade()
// .into(holder.news_image);
Picasso.with(mContext).load(newsCategory.getPostImage()).placeholder(R.drawable.placeholder_image).error(R.drawable.placeholder_image).into(holder.news_image);
**holder.itemView.setOnClickListener(v -> {
newsItemClickListener.onNewsItemClick(position, newsCategory, holder.news_image);
});**
}
#Override
public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
if (payloads.isEmpty()) {
// if empty, do full binding;
onBindViewHolder(holder, position);
return;
}
Bundle bundle = (Bundle) payloads.get(0);
String newTitle = bundle.getString("NAME_CHANGE");
if (newTitle != null) {
// add some animation if you want
final CategoryNews actor = actors.get(position);
holder.news_title.setText(actor.getPostTitle());
holder.news_date.setText(actor.getPostDate());
holder.news_category.setText(actor.getCategoryName());
holder.news_description.setText(actor.getPostContent());
}
}
public void addItems(List<CategoryNews> newItems) {
// record this value before making any changes to the existing list
int curSize = getItemCount();
// update the existing list
actors.addAll(newItems);
// curSize should represent the first element that got added
// newItems.size() represents the itemCount
notifyItemRangeInserted(curSize, newItems.size());
}
public void updtateItems(List<CategoryNews> newItems) {
// record this value before making any changes to the existing list
int curSize = getItemCount();
// update the existing list
actors.addAll(newItems);
// curSize should represent the first element that got added
// newItems.size() represents the itemCount
notifyItemRangeInserted(0,newItems.size()- getItemCount()-1);
}
#Override
public int getItemCount() {
return actors.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView news_title, news_date, news_category, news_description;
private ImageView news_image;
public ViewHolder(View itemView) {
super(itemView);
news_title = (TextView) itemView.findViewById(R.id.news_title);
news_date = (TextView) itemView.findViewById(R.id.news_date);
news_category = (TextView) itemView.findViewById(R.id.news_category);
news_description = (TextView) itemView.findViewById(R.id.news_description);
news_image = (ImageView) itemView.findViewById(R.id.news_image);
}
}
}
And In your activity
implements NewsItemClickListener
create object NewsItemClickListener newsItemClickListner;
inside oncreate newsItemClickListner=this; (you mush have implemented 1)
pass that object to recyclerview.
In adapter it is received by this
public CategoryNewsListAdapter(List personList, Context mContext, NewsItemClickListener newsItemClickListener) {
this.actors.addAll(personList);
this.mContext = mContext;
this.newsItemClickListener = newsItemClickListener;
}
In your activity
CategoryNewsListAdapter categoryNewsListAdapter= new CategoryNewsListAdapter(list,this,newsItemClickListner)
After doing all this your overriden method will be called when you click in item of recycler view where you have set onclick listner
Okay .. The Problem is Fixed now ...
I was passing the wrong value or maybe the method of passing the ClickHandler was wrong.
The Solution of the Problem :
I Created another Class for ClickHandler-
public class PlaceCardClickHandler implements
FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler,
PlaceListAdapter.PlaceListAdapterOnclickHandler {
Context mContext;
public PlaceCardClickHandler(Context mContext) {
this.mContext = mContext;
}
#Override
public void onClick(String id) {
Intent intentToStartDetail = new Intent(mContext, PlaceDetailActivity.class);
intentToStartDetail.putExtra("id", id);
mContext.startActivity(intentToStartDetail);
}
}
and the change in the Fragment was like this- (Just passed a object from the ClickHandler class)
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, new PlaceCardClickHandler(getContext())));