This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I have this restaurant app and I want to make a checkout looking like FoodPanda's (if you have the layout in mind). The recyclerview items should contain a textview containing the quantity of one product, surrounded by two buttons for decrement and increment that quantity, then two textviews with product name and the price.
I've tried this so far in my adapter class:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<Product> product;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void decrementQ(int position);
void incrementQ(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class MyViewHolder extends ViewHolder {
public ImageView incrementQty, decrementQty;
public TextView productName, price, qty;
public MyViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
incrementQty = itemView.findViewById(R.id.incrementProduct);
decrementQty = itemView.findViewById(R.id.decrementProduct);
qty = itemView.findViewById(R.id.qtyProduct);
productName = itemView.findViewById(R.id.productName);
price = itemView.findViewById(R.id.productPrice);
decrementQty.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.decrementQ(position);
}
}
});
incrementQty.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.incrementQ(position);
}
}
});
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Product> thisProduct) {
product = thisProduct;
}
// Create new views (invoked by the layout manager)
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.products, parent, false);
MyViewHolder mv = new MyViewHolder(v, mListener);
return mv;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Product currentProduct = product.get(position);
holder.decrementQty.setImageResource(currentProduct.getDecrementSource());
holder.qty.setText(currentProduct.getQty());
holder.incrementQty.setImageResource(currentProduct.getIncrementSource());
holder.productName.setText(currentProduct.getProductName());
holder.price.setText(currentProduct.getProductPrice());
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return product.size();
}
}
And this is my checkout activity class:
public class checkout extends AppCompatActivity {
private ArrayList<Product> product;
private RecyclerView recyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
product = new ArrayList<>();
product.add(new Product(R.drawable.minus, "1", R.drawable.plus, "Buttered Noodles", "15 RON"));
product.add(new Product(R.drawable.minus, "2", R.drawable.plus, "Teryiaki","20 RON"));
product.add(new Product(R.drawable.minus, "3", R.drawable.plus, "ginger", "3 RON"));
buildCheckout();
}
public void removeItem(int position) {
product.remove(position);
mAdapter.notifyItemRemoved(position);
}
public void buildCheckout(){
recyclerView = findViewById(R.id.checkoutList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
mAdapter = new MyAdapter(product);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
#Override
public void decrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty--;
if(qty < 1)
removeItem(position);
else
q.setText(qty);
}
#Override
public void incrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty++;
q.setText(qty);
}
});
}
}
The problem is, when I try to interact with the increment and decrement buttons, the app keeps stoping and I don't know what should I do next or what I did wrong.
you are setting Int value instead of String value to TextView and you will get ResourceNotFoundException:
#Override
public void incrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty++;
q.setText(qty);//ERROR change to q.setText("" + qty);
}
you have same problem in decrementQ
Related
I am making a feature for my app where the user can add a new food item inside RecyleView. But instead using a new activity I opted out for a popup that appears in the Main Activity where you can enter the food and the price which means the activity doesn't refresh. How can I display the items in the RecycleView without restarting the app?
Code in Main Activity:
public class SecondScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_screen);
myDB = new MyDataBaseHelper(SecondScreen.this);
foodID = new ArrayList<>();
foodName = new ArrayList<>();
foodPrice = new ArrayList<>();
popUp();
//Data from Sql Tables
storeDataInArrays();
customAdapater = new CustomAdapater(SecondScreen.this, foodID, foodName, foodPrice);
recyclerView.setLayoutManager(new LinearLayoutManager(SecondScreen.this));
recyclerView.setAdapter(customAdapater);
//Storing the data from the sql table
public void storeDataInArrays(){
Cursor cursor = myDB.readAllData();
//Gets the count of the rows
if(cursor.getCount() == 0){
Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
}else{
while(cursor.moveToNext()){
foodID.add(cursor.getString(0));
foodName.add(cursor.getString(1));
foodPrice.add(cursor.getString(2));
}
}
}
}
public void PopUP(){
btnAddFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyDataBaseHelper myDB = new MyDataBaseHelper(SecondScreen.this);
myDB.addFood(edtFoodName.getText().toString().trim(),
edtFoodPrice.getText().toString().trim());
//Dismisses the popUp after button click
myDialog.dismiss();
}
});
}
}
Code for RecycleView Adapter
public class CustomAdapater extends RecyclerView.Adapter<CustomAdapater.MyViewHolder> {
Context context;
private ArrayList foodID, foodName, foodPrice;
CheckBox chkItem;
//Constructor
CustomAdapater(Context context, ArrayList foodID, ArrayList foodName, ArrayList foodPrice){
//Declares to global variables that can be used in the MainAcivity
this.context = context;
this.foodID = foodID;
this.foodName = foodName;
this.foodPrice = foodPrice;
}
#NonNull
#Override
public CustomAdapater.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflates the item_row layout
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CustomAdapater.MyViewHolder holder, int position) {
holder.checkItem.setText(String.valueOf(foodName.get(position)));
holder.foodPrice_txt.setText(String.valueOf(foodPrice.get(position) + "$"));
}
#Override
public int getItemCount() {
return foodID.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView foodPrice_txt;
CheckBox checkItem;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
checkItem = itemView.findViewById(R.id.chkItemRow);
foodPrice_txt = itemView.findViewById(R.id.txtPriceRow);
}
}
}
Call notifyItemInserted after adding the item to your database and array lists, to update your adapter.
Like so:
String foodName = edtFoodName.getText().toString().trim();
String foodPrice = edtFoodPrice.getText().toString().trim();
myDB.addFood(foodName, foodPrice);
foodNames.add(foodName);
foodPrices.add(foodPrice);
customAdapater.notifyItemInserted(foodNames.size() - 1);
This question already has answers here:
how to get ID of the items in recyclerview
(2 answers)
Closed 2 years ago.
I have recyclerview on my project . I want get item id . How Can I get item id ? . I used Item touchhelper and I need particular item id . How can I get particular item id .
Activty.java
public class todoactivity extends AppCompatActivity {
TextView title;
Button back;
ImageButton gorevo;
RecyclerView recyclerView;
List<String>Listsx = new ArrayList<>();
TodoActivityAdpter adapterx;
DatabaseHelper4 myDBxxx;
TextView textView;
CheckBox checkBox;
long id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todoactivity);
recyclerView=findViewById(R.id.recyclerviewxx);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapterx=new TodoActivityAdpter(Listsx);
recyclerView.setAdapter(adapterx);
title=findViewById(R.id.titlex);
textView=findViewById(R.id.text_viewx);
gorevo = findViewById(R.id.gorevo);
myDBxxx = new DatabaseHelper4(this);
Cursor datax = myDBxxx.getListContents();
if(datax.getCount() == 0){
}else{
while(datax.moveToNext()){
Listsx.add(datax.getString(1));
ListAdapter listAdapterx = new ArrayAdapter<>(this,R.layout.todoactivity_item,R.id.textitem,Listsx);
adapterx.notifyItemInserted(Listsx.size()-1);
}
}
gorevo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(todoactivity.this);
bottomSheetDialog.setContentView(R.layout.bottomsheetlayout3);
bottomSheetDialog.show();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
EditText editText = bottomSheetDialog.findViewById(R.id.editx);
Button ekle = bottomSheetDialog.findViewById(R.id.ekle);
ekle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editText.getText().toString();
Listsx.add(text);
AddDataxxx(text);
adapterx.notifyItemInserted(Listsx.size()-1);
bottomSheetDialog.hide();
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
});
}
});
back=findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(todoactivity.this, pomodoroscreen.class);
startActivity(i);
overridePendingTransition(0,0);
}
});
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int positionx = viewHolder.getAdapterPosition();
Listsx.remove(positionx);
adapterx.notifyItemRemoved(positionx);
myDBxxx.deleteItem(// I want get id in there);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
public void AddDataxxx(String newEntry) {
boolean insertDatax = myDBxxx.addDataxxx(newEntry);
}
}
MyAdapter.java
public class TodoActivityAdpter extends RecyclerView.Adapter<TodoActivityAdpter.Holder> {
List<String>Listsx;
public TodoActivityAdpter(List<String>itemxxx){
this.Listsx = itemxxx;
}
#NonNull
#Override
public TodoActivityAdpter.Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.todoactivity_item,parent,false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull TodoActivityAdpter.Holder holder, int position) {
holder.textView.setText(Listsx.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.checkBox.isChecked()) {
holder.textView.setTextColor(view.getResources().getColor(R.color.grey));
} else {
holder.textView.setTextColor(view.getResources().getColor(R.color.Color_black));
}
}
});
}
#Override
public int getItemCount() {
return Listsx.size();
}
public class Holder extends RecyclerView.ViewHolder {
CheckBox checkBox;
TextView textView;
List<String>Listsx;
RecyclerView recyclerView;
Context mContext;
public Holder(View view) {
super(view);
textView=view.findViewById(R.id.text_viewx);
checkBox=view.findViewById(R.id.checkbox);
recyclerView=view.findViewById(R.id.recyclerviewxx);
}
}
}
I want get item id in Activty . How Can I get item id . Can you be fast . My actvity is activity.java . My adapter is MyAdapter.java
int positionx = viewHolder.getAdapterPosition();
int id = recyclerView.getChildAt(position).getId();
Listsx.remove(positionx);
adapterx.notifyItemRemoved(positionx);
myDBxxx.deleteItem(id);
maybe
I have a RecyclerView.Adapter which has some Arrays there.
ArrayList with Strings and ArrayList with Integer. Strings are like url and Integer is the photo.
When the app is open for first time the first item is selected.
I have another method for click which makes another item as selected and this works, but the problem is that the first item stays as selected and so for every image click makes as selected, I want only one item to be selected and take a color.
This is my code.
Adapter of RecyclerView
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem;
private ArrayList<Integer> mImages = new ArrayList<>();
private ArrayList<String> mSearchUrl = new ArrayList<>();
private Context mContext;
public ListViewAdapter(ArrayList<Integer> images, ArrayList<String> SearchUrl, Context context) {
mImages = images;
mContext = context;
mSearchUrl = SearchUrl;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.s_engine_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int i) {
selectedItem = 0;
if (selectedItem == i) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
Glide.with(mContext).load(mImages.get(i))
.into(viewHolder.image);
viewHolder.searchUrl.setText(mSearchUrl.get(i));
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
selectedItem = i;
}
});
}
#Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView searchUrl;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.ivEngine);
searchUrl = itemView.findViewById(R.id.ivEngineText);
}
}
}
And this is the MainActivity.class
public void intSearch() {
mImages.add(R.drawable.s_bing);
mSearchUrl.add("https://www.bing.com/search?q=");
mImages.add(R.drawable.s_google);
mSearchUrl.add("https://www.google.com/search?q=");
mImages.add(R.drawable.s_yahoo);
mSearchUrl.add("www.yahoo.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.lvEngines);
recyclerView.setLayoutManager(layoutManager);
ListViewAdapter adapter = new ListViewAdapter(mImages, mSearchUrl, this);
recyclerView.setAdapter(adapter);
}
Initialize your selected item globally
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem = 0;
.....
Then inside your onBindViewHolder whenever you click a new Image notify your adapter for changes in the last selected item cell.
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousSelectedItem = selectedItem;
selectedItem = i;
notifyItemChanged(previousSelectedItem);
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
});
Just remove this line from onBindViewHolder
selectedItem = 0;
and add an else to the background condition, like:
if (selectedItem == i) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}else{
viewHolder.image.setBackgroundColor(“YOUR_DEFAULT_COLOR”);
}
and update the onClick:
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedItem = i;
notifyDataSetChanged();
}
});
I'm working on a E-commerce App to place some online orders. In my Recyclerview there are products, which are added at the previous activity. Here i'm populating the added products in the Recyclerview with the details such as item name, qty, price etc.. There are buttons to increase/decrease the qty of each products and another button to delete the item too.
At the bottom of the Recyclerview Layout there is another textview to Show the grand total. As I'm handling the button slicks in the RecyclerViewAdapter class, while user updating the qty or deleting an item, I have to update the Grand total. I tried some solutions already mentioned here, but the app crashes at random button clicks. Please provide a solution or a better way to do this.
Here is my Adapter class:
public class InvoiceRecyclerViewAdapter extends RecyclerView.Adapter<InvoiceRecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecylerViewAdapter";
List<Products> addedProductsList;
Context mContext;
public InvoiceRecyclerViewAdapter(Context mContext,List<Products> addedProductsList)
{
this.mContext=mContext;
this.addedProductsList=addedProductsList;
}
public InvoiceRecyclerViewAdapter(Context mContext)
{
this.mContext=mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_invoice,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
holder.item_qty.setText("1");
holder.itemname.setText(addedProductsList.get(position).getName());
holder.itemprice.setText("Rs "+addedProductsList.get(position).getPrice());
holder.itemdiscount.setText("Rs "+calculate_dis(position));
holder.itemtotal.setText("Rs "+calculate_total(position));
holder.button_inc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int qty = addedProductsList.get(position).getQty();
qty++;
addedProductsList.get(position).setQty(qty);
holder.item_qty.setText(""+qty);
holder.itemdiscount.setText("Rs "+calculate_dis(position));
holder.itemtotal.setText("Rs "+calculate_total(position));
UpdateTotal();
}
});
holder.button_dec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int qty = addedProductsList.get(position).getQty();
qty--;
if(qty>0){
addedProductsList.get(position).setQty(qty);
holder.item_qty.setText(""+qty);
holder.itemdiscount.setText("Rs"+calculate_dis(position));
holder.itemtotal.setText("Rs"+calculate_total(position));
UpdateTotal();
}
}
});
holder.button_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addedProductsList.get(position).setAddedTocart(false);
//notifyDataSetChanged();
addedProductsList.remove(position);
notifyItemRemoved(position);
UpdateTotal();
}
});
}
#Override
public int getItemCount() {
return addedProductsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView itemname,itemtotal,itemprice,itemdiscount,item_qty;
Button button_cancel,button_inc,button_dec;
ConstraintLayout parent_layout;
public ViewHolder(View itemView) {
super(itemView);
itemname = itemView.findViewById(R.id.textView_item_name);
itemprice = itemView.findViewById(R.id.textView_item_price);
itemdiscount = itemView.findViewById(R.id.textView_item_discount);
itemtotal = itemView.findViewById(R.id.textView_item_total);
button_cancel = itemView.findViewById(R.id.button_cancel);
button_inc=itemView.findViewById(R.id.button_inc);
button_dec=itemView.findViewById(R.id.button_dec);
item_qty = itemView.findViewById(R.id.textView_qty);
parent_layout = itemView.findViewById(R.id.invoice_parent_layout);
}
}
private float calculate_dis(int pos){
float dis = 0;
if(addedProductsList.get(pos).isPrice_g_enabled()){
int qty=addedProductsList.get(pos).getQty();
dis = Float.parseFloat(addedProductsList.get(pos).getPrice())-Float.parseFloat(addedProductsList.get(pos).getPrice_g());
dis = qty*dis;
}
return dis;
}
private float calculate_total(int pos){
int qty=addedProductsList.get(pos).getQty();
float price,total;
if(addedProductsList.get(pos).isPrice_g_enabled()){
price = Float.parseFloat(addedProductsList.get(pos).getPrice_g());
}
else{
price= Float.parseFloat(addedProductsList.get(pos).getPrice());
}
total = price*qty;
return total;
}
private void UpdateTotal(){
TextView txtView =((EditQuantity)mContext).findViewById(R.id.textView_total);
float total=0,price=0;
int qty=0;
for(int i=0;i<addedProductsList.size();i++){
if(addedProductsList.get(i).isAddedTocart()) {
qty = addedProductsList.get(i).getQty();
if(addedProductsList.get(i).isPrice_g_enabled()){
price = Float.parseFloat(addedProductsList.get(i).getPrice_g());
}
else{
price= Float.parseFloat(addedProductsList.get(i).getPrice());
}
}
total=total+(qty*price);
}
txtView.setText("Rs "+total);
}
}
Here is the Main Activity part:
public class EditQuantity extends AppCompatActivity {
ArrayList<Products> products_list;
ArrayList<Products> cart_productslist= new ArrayList<>();
Products added_product;
FloatingActionButton fab_next;
TextView total_textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_quantity);
products_list = getIntent().getParcelableArrayListExtra("products");
Products products;
for(int i=0;i<products_list.size();i++){
products = products_list.get(i);
if(products.isAddedTocart()){
added_product = new Products(products.getName(),products.getPrice(),products.getPrice_g(),products.getImage(),1,true,products.isPrice_g_enabled());
cart_productslist.add(added_product);
}
}
Toolbar toolbar = findViewById(R.id.toolbar_invoice);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Cart");
total_textview = findViewById(R.id.textView_total);
total_textview.setText("Rs "+CalTotal());
fab_next = findViewById(R.id.fab_next);
fab_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
InitRecylerView();
}
#Override
public void onBackPressed() {
}
private void InitRecylerView() {
RecyclerView recyclerView = findViewById(R.id.invoice_recyclerview);
InvoiceRecyclerViewAdapter adapter = new InvoiceRecyclerViewAdapter(
this ,cart_productslist);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public float CalTotal(){
float total=0;
float i_total,price;
for(int i=0;i<cart_productslist.size();i++){
if(cart_productslist.get(i).isAddedTocart()) {
int qty = cart_productslist.get(i).getQty();
if(cart_productslist.get(i).isPrice_g_enabled()){
price = Float.parseFloat(cart_productslist.get(i).getPrice_g());
}
else{
price= Float.parseFloat(cart_productslist.get(i).getPrice());
}
i_total = price * qty;
total = total + i_total;
}
}
return total;
}
I'd suggest going through a listener, moving your onClickListeners and removing the for loops.
So, step by step
In your adapter class declare an interface
public interface OnQuantityChangeListener {
void onQuantityChange( float change );
}
Next add a private OnQuantityChangeListener to your adapter class and change the constructor to add one at creation:
private OnQuantityChangeListener mListener;
public InvoiceRecyclerViewAdapter(Context mContext,List<Products> addedProductsList, OnQuantityChangeListener listener) {
this.mContext=mContext;
this.addedProductsList=addedProductsList;
mListener = listener;
}
public InvoiceRecyclerViewAdapter(Context mContext, OnQuantityChangeListener listener)
{
this.mContext=mContext;
mListener = listener;
}
It's bad for performance to set OnClickListeners in the onBindViewHolder method, because this means you're going to have to add them any time a view pops up onto the screen. Set them in the onCreateViewHolder method instead, that way you'll recycle them. To get your current item you can use the getAdapterPosition() method.
So in the onCreateViewHolder method set the listeners:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_invoice,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.button_inc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Products product = addedProductsList.get(getAdapterPosition());
product.setQty( product.getQty() + 1 );
float difference = product.isPrice_g_enabled() ? Float.parseFloat(product.getPrice_g()) : Float.parseFloat(product.getPrice());
mListener.onQuantityChange( difference );
notifyItemChanged( getAdapterPosition ):// This will call onBindViewAdapter again and change all your strings for you
}
});
viewHolder.button_dec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Products product = addedProductsList.get(getAdapterPosition());
if( product.getQty() == 0 )// Can't remove an item if it's already at 0
return;
product.setQty( product.getQty() - 1 );
float difference = product.isPrice_g_enabled() ? Float.parseFloat(product.getPrice_g()) : Float.parseFloat(product.getPrice());
mListener.onQuantityChange( -difference );
notifyItemChanged( getAdapterPosition ):// This will call onBindViewAdapter again and change all your strings for you
}
});
viewHolder.button_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Products product = addedProductsList.get(getAdapterPosition());
float difference = product.isPrice_g_enabled() ? Float.parseFloat(product.getPrice_g()) : Float.parseFloat(product.getPrice());
mListener.onQuantityChange( -difference * product.getQty() );
product.setQty( 0 );
notifyItemChanged( getAdapterPosition ):// This will call onBindViewAdapter again and change all your strings for you
// You decide at this point if you want to remove the item altogether or just show 0
}
});
return viewHolder;
}
Remember at this point to remove the OnClickListeners from your onBindViewHolder method.
Once this is done head over to your Activity and add a private total:
private float total = 0;
and edit your adapter creation like so:
InvoiceRecyclerViewAdapter adapter = new InvoiceRecyclerViewAdapter(
this ,cart_productslist, new InvoiceRecyclerViewAdapter.OnQuantityChangeListener(){
#Override
void onQuantityChange( float difference ){
total += difference;
total_textview.setText("Rs "+ total);
}
} );
And that about does it. Remember to calculate your first total in your activity once (no escaping the for loop here) and then make sure you save your instance states.
Hope this helps!
I am relatively new to Android programming. I have a RecycleView with CardView in it settled in rows. On click of any row opens a new Activity associated to that row. Everything was working great until I added the filter functionality to this list. When I search the list and then click on one Item, it doesn't open the activity associated with the filtered results. It opens up an Activity related to the Item at that position in Original list.
Example - Original List : AA, BA, CC, DA, ED, FF
Search : 'A' Filtered results: AA, BA, DA
But when I click on item DA it opens up the Activity for CC. I have called notifyDataSetChanged() on the adapter.
I searched and found similar problems but couldn't implement in my code.
Here is the code:
public class MainActivity extends AppCompatActivity implements ExampleAdapter.OnItemClickListener{
public static final String EXTRA_IMG = "imageresource";
public static final String EXTRA_TXT1 = "text1";
public static final String EXTRA_TXT2 = "text2";
public static final String EXTRA_TXT3 = "text3";
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createExampleList();
buildRecyclerView();
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
}
private void filter(String text) {
ArrayList<ExampleItem> filteredList = new ArrayList<>();
for (ExampleItem item : mExampleList) {
if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
private void createExampleList() {
//just creating list
}
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(MainActivity.this);
}
#Override
public void onItemClick(int position) {
Intent detailintent = new Intent(this,DetailActivity.class);
ExampleItem clickedItem = mExampleList.get(position);
detailintent.putExtra(EXTRA_IMG,clickedItem.getImageResource());
detailintent.putExtra(EXTRA_TXT1,clickedItem.getText1());
detailintent.putExtra(EXTRA_TXT2,clickedItem.getText2());
detailintent.putExtra(EXTRA_TXT3,clickedItem.getText3());
startActivity(detailintent);
}
}
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener=listener;
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView7);
mTextView3 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener != null ){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
mListener.onItemClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mTextView3.setText(currentItem.getText3());
}
#Override
public int getItemCount() {
return mExampleList.size();
}
public void filterList(ArrayList<ExampleItem> filteredList) {
mExampleList = filteredList;
notifyDataSetChanged();
}
}
You have two lists, one in your activity and one in your adapter.
After filtering your list, you only notify the adapter and only set the adapters mExampleList to the new, filtered list. The Activity's list remains the same.
When you click on an item, you let the activity handle the click event. But the activity still have the old, unfiltered list so it will send the wrong data to your new activity.
Solution: simply add mExampleList = filteredList next to the line mAdapter.filterList(filteredList); in your filtering method