I am new in RecyclerView. We can get clicked item position in RecyclerView, but can we set and get item's ID of RecyclerView? Because in my RecyclerView, the item has custom ID provided by server. I've try this code :
FragmentListProduct.java
productData = new JSONArray();
linearLayoutManager = new LinearLayoutManager(fragmentActivity);
lpAdapter = new ListProductAdapter(fragmentActivity, R.layout.list_product_grid, productData, null,
new ListProductHolder.OnProductClickListener() {
#Override
public void onProductClick(View v, long id) {
Toast.makeText(fragmentActivity, "id:"+id, Toast.LENGTH_SHORT).show();
MainActivity.setIdToRead((int) id);
MainActivity.replaceFragment(FragmentListProduct.this, new FragmentViewProduct());
}
});
rvProductList = (RecyclerView) mainView.findViewById(R.id.rvProductList);
rvProductList.setLayoutManager(linearLayoutManager);
rvProductList.setAdapter(lpAdapter);
rvProductList.setHasFixedSize(true);
ListProductHolder.java
public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvTitle;
TextView tvPrice;
ImageView ivImage;
private OnProductClickListener onProductClickListener;
ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
super(itemView);
this.onProductClickListener = onProductClickListener;
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onProductClickListener.onProductClick(view, getItemId());
}
public static interface OnProductClickListener {
public void onProductClick(View v, long id);
}
}
ListProdutAdapter.java
public class ListProductAdapter extends RecyclerView.Adapter<ListProductHolder> {
private Activity activity;
private int layoutResource;
private JSONArray productDataArray;
private StringSignature downloadSignature;
private ListProductHolder.OnProductClickListener onProductClickListener;
public ListProductAdapter(Activity activity, int layoutResource, JSONArray productDataArray, StringSignature downloadSignature, ListProductHolder.OnProductClickListener onProductClickListener) {
this.activity = activity;
this.layoutResource = layoutResource;
this.productDataArray = productDataArray;
this.downloadSignature = downloadSignature;
this.onProductClickListener = onProductClickListener;
}
#Override
public ListProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(activity).inflate(layoutResource, parent, false);
return new ListProductHolder(v, onProductClickListener);
}
#Override
public void onBindViewHolder(ListProductHolder holder, int position) {...}
#Override
public long getItemId(int i) {
long itemId = -1;
try {
itemId = productDataArray.getJSONObject(i).getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
return itemId;
}
}
But when i tried it, the ID is always -1. How to solve this? or is there another solution i can do?
Add click listener in the on bind view holder as view will be reused you have to set click for each view reused. Add click listener to root layout
#Override
public void onBindViewHolder(ListProductHolder holder, int position) {
final long itemId = productDataArray.getJSONObject(position).getInt("id");
holder.rootLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onProductClickListener.onProductClick(view, itemId);
}
});
}
You can use
itemView.setTag(your_id);
and onClick you need to use
view.getTag();
Check the below example
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.LogHolder>
{
public RecyclerAdapter()
{
}
#UiThread
protected void dataSetChanged()
{
notifyDataSetChanged();
}
#Override
public int getItemCount() {
//you can pass ur server response array here
//int count = (csMainJsonArr != null) ? csMainJsonArr.length() : 0;
int count = 20;
return count;
}
public class LogHolder extends RecyclerView.ViewHolder
{
TextView nameTxt = null;
TextView contactTxt = null;
TextView statusTxt = null;
ImageView listImg = null;
public LogHolder(View itemView)
{
super(itemView);
nameTxt = (TextView)itemView.findViewById(R.id.custom_NameTxt);
contactTxt = (TextView)itemView.findViewById(R.id.custom_ContactTxt);
statusTxt = (TextView)itemView.findViewById(R.id.custom_StatusTxt);
listImg = (ImageView)itemView.findViewById(R.id.custom_listImg);
}
}
#Override
public LogHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LogHolder holder = new LogHolder(inflater.inflate(R.layout.custom_design, parent, false));
return holder;
}
#Override
public void onBindViewHolder(LogHolder holder, int position)
{
//just get data from your json arrahy and add here
holder.nameTxt.setText("name "+position);
holder.contactTxt.setText("contact "+position);
holder.statusTxt.setText("status "+position);
//you need to set tag for holder image and holder item to get position of item
holder.listImg.setTag(position);
holder.itemView.setTag(position);
holder.listImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//create your own action
// using view.getTag() you can get the position of item clicked
int position = (int) view.getTag();
Log.d("=======", String.valueOf(position));
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//create your own action
//using view.getTag() you can get the position of item clicked
int position = (int) view.getTag();
Log.d("=======", String.valueOf(position));
}
});
}
}
and don't forget to declare this.
csRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
csAdapter = new RecyclerAdapter();
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
//manager.setReverseLayout(true);
//manager.setStackFromEnd(true);
csRecyclerView.setLayoutManager(manager);
csRecyclerView.setAdapter(csAdapter);
Does the method getItemId() int ListProductAdapter have any other code? Is it same with getItemId(int i) in ListProductAdapter.In my opinion,what you need to do is just replace getItemId() with productDataArray.getJSONObject(getLayoutPosition()).getInt("id").
public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvTitle;
TextView tvPrice;
ImageView ivImage;
private OnProductClickListener onProductClickListener;
ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
super(itemView);
this.onProductClickListener = onProductClickListener;
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onProductClickListener.onProductClick(view, productDataArray.getJSONObject(getLayoutPosition()).getInt("id"));
}
public static interface OnProductClickListener {
public void onProductClick(View v, long id);
}
}
Related
I need to get the data specifically the Row Id which I have previously set to the textview in Recyclerview .
Here I got the position of the adapter which I have left swiped .by using that I want to get the hidden id which I was set previously and delete the data in SQlite.
RecyclerviewAdapter.java
public class RecyclerviewAdapter_list extends RecyclerView.Adapter<RecyclerviewAdapter_list.MyViewHolder> {
private Context mContext;
private List<myAlarms> myAlarmsdata;
Database_Helper database_helper;
public RecyclerviewAdapter_list(Context mContext, List<myAlarms>myAlarmsdata){
this.mContext = mContext;
this.myAlarmsdata =myAlarmsdata;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.cardview_alarm, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myAlarms myAlarms =myAlarmsdata.get(position);
String mTimeSplit = myAlarms.getALARM_TIME();
SpannableString mtime= new SpannableString(mTimeSplit);
mtime.setSpan(new RelativeSizeSpan(2f), 0,5, 0);
holder.mTime.setText(mtime);
holder.mTitle.setText(myAlarms.getALARM_TITLE());
holder.mTitle.setTag(myAlarms.getALARM_ID());
String mStatus = myAlarms.getALARM_STATUS();
if(mStatus.equals("Active") && !holder.mbtn.isChecked()){
holder.mbtn.setChecked(true);
}else{
holder.mbtn.setChecked(false);
}
}
#Override
public int getItemCount() {
return myAlarmsdata.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTime,mTitle;
com.suke.widget.SwitchButton mbtn;
public RelativeLayout viewBackground;
public RelativeLayout viewForeground;
public MyViewHolder(View itemView) {
super(itemView);
mTime = (TextView) itemView.findViewById(R.id.mTime);
mTitle = (TextView) itemView.findViewById(R.id.mTitle);
mbtn = (com.suke.widget.SwitchButton)itemView.findViewById(R.id.mbtn);
viewBackground = itemView.findViewById(R.id.view_background);
viewForeground = itemView.findViewById(R.id.view_foreground);
database_helper = new Database_Helper(mContext);
mbtn.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if(isChecked == false) {
String mStatus = "InActive";
Integer mRowId = (Integer) mTitle.getTag();
database_helper.updateStatus(mRowId,mStatus);
}else{
if(isChecked == true){
String mStatus = "Active";
Integer mRowId = (Integer) mTitle.getTag();
database_helper.updateStatus(mRowId,mStatus);
}
}
}
});
}
}
public void removeItem(int position) {
myAlarmsdata.remove(position);
notifyItemRemoved(position);
//need to get the data by using the adapter position
}
}
Can anyone help me to get the data by using the adapter position ...
You have it in myAlarmsdata.
public void removeItem(int position) {
myAlarms myAlarms = myAlarmsdata.get(position);
int id = myAlarms.getALARM_ID();
myAlarmsdata.remove(position);
notifyItemRemoved(position);
// use id to get the data by using the adapter position
}
Did I undestand you correctly?
I have two buttons and one textview in each child row of Recyclerview. Those two buttons are used as triggers to count number and then write the number in textview.
Issue: Text-view value is not Updated while scrolling and the item-count only give the count of items in the list which is visible.
public class RecyclerAdapterItem extends RecyclerView.Adapter<RecyclerAdapterItem.ItemViewHolder> {
private LayoutInflater inflater;
private Context ctx;
public RecyclerAdapterItem(Context ctx) {
inflater = LayoutInflater.from(ctx);
this.ctx = ctx;
}
#Override
public RecyclerAdapterItem.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recyclerviewitem_item_tab, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final RecyclerAdapterItem.ItemViewHolder holder, int position) {
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
holder.Itemname.setText(ItemFragment.itemArrayList.get(position).getItem_name().toString());
Float rt=Float.valueOf(ItemFragment.itemArrayList.get(position).getRate());
holder.Rate.setText(String.valueOf(df.format(rt)));
holder.qty.setText(String.valueOf(ItemFragment.itemArrayList.get(position).getQty()));
}
#Override
public int getItemCount() {
return ItemFragment.itemArrayList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected Button btn_plus, btn_minus;
private TextView Itemname,Rate, qty;
public ItemViewHolder(View itemView) {
super(itemView);
Itemname = (TextView) itemView.findViewById(R.id.Itemname);
Rate=(TextView) itemView.findViewById(R.id.Rate);
qty = (TextView) itemView.findViewById(R.id.Qty);
btn_plus = (Button) itemView.findViewById(R.id.plus);
btn_minus = (Button) itemView.findViewById(R.id.minus);
btn_plus.setTag(R.integer.btn_plus_view, itemView);
btn_minus.setTag(R.integer.btn_minus_view, itemView);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == btn_plus.getId()){
View tempview = (View) btn_plus.getTag(R.integer.btn_plus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) + 1;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
} else if(v.getId() == btn_minus.getId()) {
View tempview = (View) btn_minus.getTag( R.integer.btn_minus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) - 1;
if(number<0)
number =0;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
}
}
}
}
This is the recycleradapter code.The mentioned textview is qty.
If your code is working then only try this method and try to check update..
notifyDataSetChanged();
other wise make interfaces to puls and mins value update like below code ..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(Item str);//pass your object types.
}
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
Item item=mDataset.get(position);
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setPlus(true);
onItemClickListner.onClick(data);
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setMinus(true);
onItemClickListner.onClick(data);
}
});
}
then after define adapter object and bind into recylcer view then call below code ..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
#Override
public void onClick(Item str) {
if (str.getPlus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
if (str.getMinus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
}
});
Move the click listeners for the buttons to BindView method
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDataset.get(position).setQty(mDataset.get(position).getQty() + 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDataset.get(position).getQty()==0)
{
holder.qty.setText(String.valueOf(0));
}
else
{
mDataset.get(position).setQty(mDataset.get(position).getQty() - 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
}
});
}
previously I was using firebase recycler adapter and I used to get my child value using
final String NameKey = getRef(position).getKey();
and send that value using putExtraIntent and get that value in another activity.
now I am using custom recycler adapter and I am unable to use this code to get key value and use it in putExtraIntent. How can i do this ?
previously using firebase adapter
` FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter(options){
#Override
public NameHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.author_list_cv, parent, false);
return new NameHolder(view);
}
#Override
protected void onBindViewHolder(NameHolder holder, int position, AuthorsCLA model) {
final String AuthorNameKey = getRef(position).getKey();
holder.name.setText(model.getAuthorname());
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
quotesListIntent.putExtra("nameID", AuthorNameKey);
startActivity(quotesListIntent);
}
}); `
now I am trying this
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final AuthorsCLA authorsModel = authorlist.get(holder.getAdapterPosition());
final String AuthorNameKey = getRef(position).getKey();
switch (holder.getItemViewType()) {
case A: {
NamesHolder nameHolder = (NamesHolder) holder;
nameHolder.name.setText(authorsModel.getAuthorname());
nameHolder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AuthorQuotesDetailActvity.class);
intent.putExtra("nameID",AuthorNameKey );
mcontext.startActivity(intent);
Toast.makeText(view.getContext(), authorsModel.getAuthorname(), Toast.LENGTH_SHORT).show();
}
});
but getRef is showing an error and when I hover on it, it says cannot resolve method getRef(int).
I think the problem is your using the the new
com.android.support:recyclerview-v7:
Recyclerview and it works a bit differerat. Here´s a tutorial and basically from that lesson:
(the onCreateViewHolder is setting the ViewHolder and no need for the GetRef() Which however doesn't exist if you dont create a custom one
public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> {
List<Data> list = Collections.emptyList();
Context context;
public Recycler_View_Adapter(List<Data> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
View_Holder holder = new View_Holder(v);
holder .itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = holder .getAdapterPosition();
// This is org.greenrobot.eventbus
Application.getInstance().getEventBus().post(new OnHistoryClickEvent(position));
}
});
holder .itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
int position = holder .getAdapterPosition();
// This is org.greenrobot.eventbus
Application.getInstance().getEventBus().post(new OnHistoryLongClickEvent(position));
return true;
}
});
return holder;
}
#Override
public void onBindViewHolder(View_Holder holder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
holder.title.setText(list.get(position).title);
holder.description.setText(list.get(position).description);
holder.imageView.setImageResource(list.get(position).imageId);
//animate(holder);
}
#Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, Data data) {
list.add(position, data);
notifyItemInserted(position);
}
// Remove a RecyclerView item containing a specified Data object
public void remove(Data data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}
}
The View_Holder
public class View_Holder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
TextView description;
ImageView imageView;
View_Holder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
Use this
nameHolder.name.setTag(position);
nameHolder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = (int) view.getTag();
String AuthorNameKey = getRef(position).getKey()
Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
quotesListIntent.putExtra("nameID", AuthorNameKey);
startActivity(quotesListIntent);
}
});
when i notify my recycleview using notifyDataSetChange() its automatically scroll to top.
I am make this adapter for multi pane purpose.
the app is only for tablet.
so can any please give me the solution for this.??
here is my recycleview adapter.
public class PickersListAdapter extends RecyclerView.Adapter<PickersListAdapter.DataObjectHolder>
{
private Context context;
private ArrayList<PickersModel> pickersModelArrayList;
private ItemsListFragment fragment;
public PickersListAdapter(Context context, ArrayList<PickersModel> pickersModelArrayList, ItemsListFragment itemsListFragment)
{
this.context = context;
this.pickersModelArrayList = pickersModelArrayList;
fragment = itemsListFragment;
}
#Override
public PickersListAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, final int viewType)
{
View view = LayoutInflater.from(context).inflate(R.layout.inflate_list_pickers, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(final PickersListAdapter.DataObjectHolder holder, final int position)
{
holder.myBackground.setSelected(pickersModelArrayList.get(position).isClicked());
holder.checkBox.setChecked(pickersModelArrayList.get(position).isChecked());
holder.txtItemName.setText(pickersModelArrayList.get(position).getItemName());
holder.txtitemQuantity.setText(pickersModelArrayList.get(position).getQuantity());
Log.e("image url...", pickersModelArrayList.get(position).getItemIcon());
Glide.with(context).load(pickersModelArrayList.get(position).getItemIcon()).placeholder(R.drawable.ic_item_icon).into(holder.imageView);
holder.myBackground.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
for (int i = 0; i < pickersModelArrayList.size(); i++)
{
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
}
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount()
{
return pickersModelArrayList.size();
}
public class DataObjectHolder extends RecyclerView.ViewHolder
{
private CheckBox checkBox;
private ImageView imageView;
private TextView txtItemName, txtitemQuantity;
private LinearLayout myBackground;
public DataObjectHolder(View itemView)
{
super(itemView);
myBackground = (LinearLayout) itemView.findViewById(R.id.myBackground);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxPickersItems);
imageView = (ImageView) itemView.findViewById(R.id.imgItemIconPickerItems);
txtItemName = (TextView) itemView.findViewById(R.id.txtItemNamePickerItems);
txtitemQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantityPickerItems);
}
}
}
Use setTag(), getTag() and then use different functions to notify.
Try something like this:
holder.myBackground.setTag(position);
holder.myBackground.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (null != v.getTag()) {
int position = (int) v.getTag();
for (int i = 0; i < pickersModelArrayList.size(); i++) {
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
}
notifyItemChanged(position);
}
}
});
here is my code. i need the index of my arraylist(orderList) in the click of a view.
Complete code of adapter class.i need orderList's index instead of position in the layout
public class OrdersList_Adapter extends
RecyclerView.Adapter {
private static ArrayList<Order> orderList;
private static OnItemClickListener monItemClickListener;
Context context;
int pos;
public OrdersList_Adapter(ArrayList<Order> orderlist, Context context) {
orderList = orderlist;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_list_item, parent, false);
OrdersList_Adapter.ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
//animate(holder);
// pos=position;
int id = orderList.get(position).getId();
String orderStatus = orderList.get(position).getOrderStatus();
String pickup_name = orderList.get(position).getName_pickup();
if (id > 0 && !pickup_name.isEmpty() && !deliver_line1.isEmpty() && !dist_deliver.isEmpty()) {
holder.txtId.setText(String.valueOf(id));
holder.txtStatus.setText(orderStatus);
/* holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
monItemClickListener.onItemClick(pos);
}
});*/
}
}
#Override
public int getItemCount() {
return orderList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtId, txtStatus;
View v=itemView;
public ViewHolder(View v) {
super(v);
txtId = (TextView) v.findViewById(R.id.tv_orderId);
txtStatus = (TextView) v.findViewById(R.id.tv_orderStatus);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
monItemClickListener.onItemClick(getAdapterPosition());
}
});
}
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.monItemClickListener = listener;
}
}
public void onBindViewHolder(ViewHolder holder, int position) {
int id = orderList.get(position).getId();
String orderStatus = orderList.get(position).getOrderStatus();
if (id > 0) {
holder.txtId.setText(String.valueOf(id));
holder.txtStatus.setText(orderStatus);
}
holder.view.setOnClickListener(new Listener(position));
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtId, txtStatus;
View view = v;
public ViewHolder(View v) {
super(v);
txtId = (TextView) v.findViewById(R.id.tv_orderId);
txtStatus = (TextView) v.findViewById(R.id.tv_orderStatus);
this.view = v;
}
class Listener implements View.OnClickListener
{
int position;
Listener(int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// Here you get the different position
onItemClickListener.onItemClick(position);//here i get adapter position
}
}
}