RealmSearchAdapter not recylcing the view - android

I have a gridview with items that I can increase or diminish its values, what is happening is when I change the value of the item and scroll down the screen
and scroll back the change that I made is gone, and if I scroll down and up a couple times I can see the change that I made again, what am I missing here?
public class SearchItemViewAdapter extends RealmSearchAdapter<Itens, SearchItemViewAdapter.ViewHolder> implements RealmChangeListener {
public SparseBooleanArray mSparseBooleanArray;
public SearchItemViewAdapter(Activity context, Realm realm, String filterColumnName) {
super(context, realm, filterColumnName);
mSparseBooleanArray = new SparseBooleanArray();
}
#Override
public void onChange() {
notifyDataSetChanged();
}
public RealmList<Itens> getCheckedItems() {
RealmList<Itens> mTempArry = new RealmList<>();
for (int i = 0; i < realmResults.size(); i++) {
if (mSparseBooleanArray.get(i)) {
mTempArry.add(realmResults.get(i));
}
}
return mTempArry;
}
public class ViewHolder extends RealmSearchViewHolder {
public SearchItemView itemView;
public ViewHolder(FrameLayout container, TextView footerTextView) {
super(container, footerTextView);
}
public ViewHolder(SearchItemView itemView) {
super(itemView);
this.itemView = itemView;
}
}
#Override
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
ViewHolder vh = new ViewHolder(new SearchItemView(viewGroup.getContext()));
return vh;
}
#Override
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
final Itens item = realmResults.get(position);
viewHolder.itemView.bind(item);
viewHolder.itemView.check.setTag(position);
viewHolder.itemView.check.setChecked(mSparseBooleanArray.get(position));
viewHolder.itemView.check.setOnCheckedChangeListener(mCheckedChangeListener);
}
CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
#Override
public ViewHolder onCreateFooterViewHolder(ViewGroup viewGroup) {
View v = inflater.inflate(R.layout.footer_view, viewGroup, false);
return new ViewHolder(
(FrameLayout) v,
(TextView) v.findViewById(R.id.footer_text_view));
}
#Override
public void onBindFooterViewHolder(ViewHolder holder, int position) {
super.onBindFooterViewHolder(holder, position);
holder.itemView.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
}
}
SearchItemView
public class SearchItemView extends RelativeLayout {
#Bind(R.id.codigo)
TextView codigo;
#Bind(R.id.nomeproduto)
TextView nome;
#Bind(R.id.preco)
TextView preco;
#Bind(R.id.valor)
TextView itemq;
#Bind(R.id.more)
ImageView more;
#Bind(R.id.less)
ImageView less;
#Bind(R.id.check)
CheckBox check;
final int nStart = 0;
final int nEnd = 10000000;
public SearchItemView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.add_itens_adapter, this);
ButterKnife.bind(this);
}
public void bind(Itens item) {
nome.setText(item.getNome());
codigo.setText(item.getCodigo());
preco.setText(item.getPreco());
more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getString = String.valueOf(itemq.getText());
int curent = Integer.parseInt(getString);
if (v == more) {
if (curent < nEnd) {
curent++;
itemq.setText(String.valueOf(curent));
}
}
}
});
less.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getString = String.valueOf(itemq.getText());
int curent = Integer.parseInt(getString);
if (v == less) {
if (curent > nStart) {
curent--;
itemq.setText(String.valueOf(curent));
}
}
}
});
}
}

Related

BaseAdapter inside RecyclerView shows only one item issue

I have RecyclerView and there is a problem with that but inside extends BaseAdapter listview shows only the first element of data, after taking the only first item, the adapter does not go on putting data.
public class AdapterCorporationPrivilegeDescriptions extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ModelPrivilegeDescription> itemList;
private ListPrivilegeParentListener parentListener;
private Context context;
private AdapterCorporationPrivilegeDescriptionnn adapter;
private boolean visibility;
private int width;
public AdapterCorporationPrivilegeDescriptions(Context context,
int width,
List<ModelPrivilegeDescription> itemList,
ListPrivilegeParentListener parentListener) {
this.itemList = itemList;
this.width = width;
this.parentListener = parentListener;
this.context = context;
}
public void setData(List<ModelPrivilegeDescription> itemList) {
this.itemList = itemList;
notifyDataSetChanged();
}
public void setVisibilityListView(boolean visibility) {
this.visibility = visibility;
}
#Override
public AdapterCorporationPrivilegeDescriptions.AdapterCorporationPrivilegeListHolder
onCreateViewHolder(ViewGroup parent, int viewType) {
return new AdapterCorporationPrivilegeDescriptions.AdapterCorporationPrivilegeListHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_corporation_privilege_descriptions, parent, false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((AdapterCorporationPrivilegeDescriptions.AdapterCorporationPrivilegeListHolder) holder).setData(itemList.get(position));
}
#Override
public int getItemCount() {
return itemList == null ? 0 : itemList.size();
}
private class AdapterCorporationPrivilegeListHolder extends RecyclerView.ViewHolder {
private HTextView text;
private HImageView icon;
private HSwipeableListView listView;
private AdapterCorporationPrivilegeListHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.itemCorporationPrivilegeDescriptionsText);
icon = itemView.findViewById(R.id.itemCorporationPrivilegeDescriptionsIcon);
listView = itemView.findViewById(R.id.itemCorporationPrivilegeDescriptionsListView);
}
private SwipeMenuItem getDeleteItem() {
SwipeMenuItem swipeMenuItem = new SwipeMenuItem(context);
swipeMenuItem.setWidth(width);
swipeMenuItem.setBackground(R.color.red_delete);
swipeMenuItem.setIcon(R.drawable.ic_delete);
swipeMenuItem.setTitle(App.getStrWithId(R.string.payment_transfer_delete_button));
swipeMenuItem.setTitleColor(Color.WHITE);
swipeMenuItem.setTitleSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()));
return swipeMenuItem;
}
HSwipeableListView.OnMenuItemClickListener onMenuItemClickListener = new HSwipeableListView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
if (index == 0) {
App.getInstance().getBusInstance().post(new CorporationPrivilegeDescriptionEvent(position));
}
return false;
}
};
public void setData(final ModelPrivilegeDescription data) {
SwipeMenuCreator creator = new SwipeMenuCreator() {
#Override
public void create(SwipeMenu menu) {
menu.addMenuItem(getDeleteItem());
}
};
listView.setDividerHeight(0);
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(onMenuItemClickListener);
listView.setSwipeEnabled(!App.getInstance().isTalkBackEnabled());
//listView.setVisibility(visibility ? View.VISIBLE : View.GONE);
text.setText(data.transactionName);
if (data.innerList != null && !data.innerList.isEmpty()) {
AdapterCorporationPrivilegeDescriptionnn adapter = new AdapterCorporationPrivilegeDescriptionnn(context, data.innerList, childListener);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
listView.setVisibility(View.GONE);
icon.setVisibility(View.INVISIBLE);
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float positionClosed = 0, positionOpened = 180;
if (listView.getVisibility() == View.VISIBLE) {
parentListener.onItemSelected(data);
listView.setVisibility(View.GONE);
icon.setRotation(positionClosed);
} else {
listView.setVisibility(View.VISIBLE);
icon.setRotation(positionOpened);
}
}
});
}
private final AdapterCorporationPrivilegeDescriptionnn.CorporationPrivilegeDescriptionListener childListener = new AdapterCorporationPrivilegeDescriptionnn.CorporationPrivilegeDescriptionListener() {
#Override
public void onLimitButtonClick(ModelPrivilegeDescriptionAccount item) {
int a = 5;
}
#Override
public void onDetailButtonClick(ModelPrivilegeDescriptionAccount item) {
int a = 5;
}
};
}
}
AdapterCorporationPrivilegeDescriptionnn issue in this class
public class AdapterCorporationPrivilegeDescriptionnn extends BaseAdapter {
private int RESOURCE_ID = R.layout.item_corporation_privilege_description;
private LayoutInflater inflater;
private List<ModelPrivilegeDescriptionAccount> list;
private CorporationPrivilegeDescriptionListener listener;
public AdapterCorporationPrivilegeDescriptionnn(Context context,
List<ModelPrivilegeDescriptionAccount> items,
CorporationPrivilegeDescriptionListener listener) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = items;
this.listener = listener;
}
#Override
public int getCount() {
return list.size();
}
#Override
public ModelPrivilegeDescriptionAccount getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(RESOURCE_ID, null);
HTextView accountNumber = convertView.findViewById(R.id.itemCorporationPrivilegeDescriptionAccountNumber);
HTextView changeLimitButton = convertView.findViewById(R.id.itemCorporationPrivilegeDescriptionChangeLimitButton);
LinearLayout layout = convertView.findViewById(R.id.itemCorporationAuthorityDescriptionLayout);
final ModelPrivilegeDescriptionAccount item = getItem(position);
accountNumber.setText(item.accountNumber);
changeLimitButton.setVisibility(item.isChangeable ? View.VISIBLE : View.GONE);
changeLimitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onLimitButtonClick(item);
}
});
accountNumber.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onDetailButtonClick(item);
}
});
return convertView;
}
public interface CorporationPrivilegeDescriptionListener {
void onLimitButtonClick(ModelPrivilegeDescriptionAccount item);
void onDetailButtonClick(ModelPrivilegeDescriptionAccount item);
}
}

Values out of ScrollView not get accessed to the Fragment from RecyclerAdapter

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()));
}
}
});
}

Change button state according to checkbox selection in recycler view

I am having one recycler view with an adapter class. In the single item view there is a checkbox also. In my layout view it contains one button and recycler view mentioned above. When i checks on any check box in the view the button should get activated (Changing the color to active). When nothing is selected on the view button should become in the form of deactivated. How can i possible? Any example? Code is below
public class PlatformAdapter extends RecyclerView.Adapter<PlatformAdapter.ViewHolder> {
ArrayList<Batch> batches;
ArrayList<CourseSlug> courses;
boolean isInstituteStudent;
public void setBatches(ArrayList<Batch> batches) {
this.batches = batches;
notifyDataSetChanged();
}
public void setCourses(ArrayList<CourseSlug> courses) {
this.courses = courses;
notifyDataSetChanged();
}
public ArrayList<CourseSlug> getCourses() {
return courses;
}
public ArrayList<Batch> getBatches() {
return batches;
}
public void setInstituteStudent(boolean instituteStudent) {
isInstituteStudent = instituteStudent;
}
public boolean isInstituteStudent() {
return isInstituteStudent;
}
public PlatformAdapter() {
courses = new ArrayList<>();
batches = new ArrayList<>();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell_platform_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (isInstituteStudent) {
Batch batch = batches.get(position);
holder.enrolment.setText(batch.getName());
holder.selectEnrollment.setChecked(batch.isPreselect());
} else {
final CourseSlug course = courses.get(position);
holder.enrolment.setText(course.getName());
holder.selectEnrollment.setChecked(course.isPreselect());
}
}
#Override
public int getItemCount() {
return isInstituteStudent ? batches.size() : courses.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView enrolment;
CheckBox selectEnrollment;
public ViewHolder(final View itemView) {
super(itemView);
enrolment = (TextView) itemView.findViewById(R.id.tv_entrollment);
selectEnrollment = (CheckBox) itemView.findViewById(R.id.cb_select_entrollment);
selectEnrollment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int index = getLayoutPosition();
if(isInstituteStudent) {
Batch batch = batches.get(index);
batch.setPreselect(b);
} else {
CourseSlug course = courses.get(index);
course.setPreselect(b);
}
}
});
}
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
}

how to update a boolean value in server with android swipe cards?

I'm a noob in android studios..and currently in learning stage.. i copied the code from #Nirav-Kalola in this community. I need help in passing a boolean value into the server whenever left or right is swiped in android swipe cards.
Below is the code:
public class MainActivity extends AppCompatActivity implements FlingCardListener.ActionDownInterface{
public static MyAppAdapter myAppAdapter;
public static ViewHolder viewHolder;
private ArrayList<Data> al;
private SwipeFlingAdapterView flingContainer;
public static void removeBackground() {
viewHolder.background.setVisibility(View.GONE);
myAppAdapter.notifyDataSetChanged();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add(new Data("http://www.drodd.com/images15/1-7.jpg", "Hello Anuj"));
al.add(new Data("http://www.drodd.com/images15/2-23.jpg", "Welcome to the final problem"));
al.add(new Data("http://www.drodd.com/images15/3-12.jpg","\t here goes the name of the person "));
al.add(new Data("http://www.drodd.com/images15/4-7.jpg", " buhahahahahahahaha"));
al.add(new Data("http://www.drodd.com/images15/5-18.jpg", "miss me?"));
myAppAdapter = new MyAppAdapter(al, MainActivity.this);
flingContainer.setAdapter(myAppAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
}
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
myAppAdapter.notifyDataSetChanged();
}
});
}
#Override
public void onActionDownPerform() {
Log.e("action", "bingo");
}
public static class ViewHolder {
public static FrameLayout background;
public TextView DataText;
public ImageView cardImage;
}
public class MyAppAdapter extends BaseAdapter {
public List<Data> parkingList;
public Context context;
private MyAppAdapter(List<Data> apps, Context context) {
this.parkingList = apps;
this.context = context;
}
#Override
public int getCount() {
return parkingList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
// configure view holder
viewHolder = new ViewHolder();
viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(MainActivity.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
}
Just do your network call in these two methods
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}

How do I get the corresponding index of my list from getAdapterPosition()?

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
}
}
}

Categories

Resources