My application crash when i hit the btnSubmit. and give this Error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference.
code Written Below;
public class RecycleAdoptor extends RecyclerView.Adapter{
Context cont;
DataSource ds;
Double lon = 0.0;
Double lat = 0.0;
RecycleAdoptor(Context paramContext)
{
this.cont = paramContext;
}
private List<Employee> mDocs;
#Override
public RecycleAdoptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("jarvis", "RecylerAdoptor");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.activity_list_items, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public RecycleAdoptor(List<Employee> myDataset, Context context) {
mDocs = myDataset;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Employee emp = mDocs.get(position);
holder.empID.setText(emp.getID());
holder.empName.setText(emp.getEmpName());
holder.empDesignation.setText(emp.getDesignation());
holder.empSpeciality.setText(emp.getSpeciality());
holder.empAddress.setText(emp.getAddress());
}
#Override
public int getItemCount() {
return mDocs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView empID;
public TextView docID;
public TextView empName;
public TextView empAddress;
public TextView empDesignation;
public TextView empSpeciality;
public TextView empStatus;
public Button btnSaveAttd;
public Spinner spinner;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
empID = (TextView) v.findViewById(R.id.emp_id);
empName = (TextView) v.findViewById(R.id.empName);
empDesignation = (TextView) v.findViewById(R.id.empDesignation);
empSpeciality = (TextView) v.findViewById(R.id.empSpecialist);
empAddress = (TextView) v.findViewById(R.id.empAddress);
spinner = (Spinner) v.findViewById(R.id.spinner1);
btnSaveAttd = (Button) v.findViewById(R.id.btnSaveAttd);
btnSaveAttd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Date currentTime = Calendar.getInstance().getTime();
double lon = ProfileActivity.Lon;
double lat = ProfileActivity.Lat;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
cont);
alertDialog.setTitle("Leave application?");
alertDialog.setMessage("Are you sure you want to leave the application?");
alertDialog.setIcon(R.drawable.ic_menu_camera);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
}
}
}
Sorry For bad English.but Need serious help...
I upload my answer. and its work Fine and i mention my prob where i did wrong.
Context cont;
DataSource ds;
Double lon = 0.0;
Double lat = 0.0;
RecycleAdoptor(Context paramContext)
{
this.cont = paramContext;
}
private List<Employee> mDocs;
#Override
public RecycleAdoptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("jarvis", "RecylerAdoptor");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.activity_list_items, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public RecycleAdoptor(List<Employee> myDataset, Context context) {
mDocs = myDataset;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Employee emp = mDocs.get(position);
holder.empID.setText(emp.getID());
holder.empName.setText(emp.getEmpName());
holder.empDesignation.setText(emp.getDesignation());
holder.empSpeciality.setText(emp.getSpeciality());
holder.empAddress.setText(emp.getAddress());
}
#Override
public int getItemCount() {
return mDocs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView empID;
public TextView docID;
public TextView empName;
public TextView empAddress;
public TextView empDesignation;
public TextView empSpeciality;
public TextView empStatus;
public Button btnSaveAttd;
public Spinner spinner;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
empID = (TextView) v.findViewById(R.id.emp_id);
empName = (TextView) v.findViewById(R.id.empName);
empDesignation = (TextView) v.findViewById(R.id.empDesignation);
empSpeciality = (TextView) v.findViewById(R.id.empSpecialist);
empAddress = (TextView) v.findViewById(R.id.empAddress);
spinner = (Spinner) v.findViewById(R.id.spinner1);
btnSaveAttd = (Button) v.findViewById(R.id.btnSaveAttd);
btnSaveAttd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Date currentTime = Calendar.getInstance().getTime();
double lon = ProfileActivity.Lon;
double lat = ProfileActivity.Lat;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());//Here I have to use v.getContext() istead of just cont.
alertDialog.setTitle("Leave application?");
alertDialog.setMessage("Are you sure you want to leave the application?");
alertDialog.setIcon(R.drawable.ic_menu_camera);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
}
}
Related
I want to set the text of a custom build popup.
As soon as I want to set the text, the classic NullPointerException comes. How can I access the text field in my custom popup? How can I change the text of the TextView order_overview_number.setText(...).
How do I have to properly initialize this? TextView order_overview_number = epicDialog.findViewById(R.id.order_overview_number);
this is Unfortunately wrong...
public class UserBestellAdapter extends RecyclerView.Adapter<UserBestellAdapter.ViewHolder> {
ArrayList<Bestellung> bestellung;
Context mContext;
Dialog epicDialog;
public UserBestellAdapter(Context context, ArrayList<Bestellung> list) {
mContext = context;
bestellung = list;
epicDialog = new Dialog(mContext);
}
#NonNull
#Override
public UserBestellAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_bestell, parent, false);
UserBestellAdapter.ViewHolder viewHolder = new UserBestellAdapter.ViewHolder(view);
return viewHolder;
}
#NonNull
#Override
public void onBindViewHolder(#NonNull UserBestellAdapter.ViewHolder holder, final int position) {
//Gesamtpreis: holder.item_betrag.setText(String.valueOf(bestellung.get(position).getBetrag()));
// Datum: holder.item_datum.setText(bestellung.get(position).getDatum());
holder.item_items.setText(bestellung.get(position).getProdukte());
//holder.item_code.setText(bestellung.get(position).getBestellnummer());
String bestellid =bestellung.get(position).getBestellnummer() + "";
holder.item_code.setText(bestellid);
holder.item_betrag.setText(Double.toString(bestellung.get(position).getSumme()));
holder.item_datum.setText(bestellung.get(position).getDatum());
holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView order_overview_number = epicDialog.findViewById(R.id.order_overview_number);
//order_overview_number.setText();
epicDialog.setContentView(R.layout.user_popup_order_overview);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
System.out.println(bestellung.get(position).getBestellnummer());
// HERE IS THE ERROR :(
//order_overview_number.setText(String.valueOf(bestellung.get(position).getDatum()));
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.show();
}
});
}
#Override
public int getItemCount() {
return bestellung.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView item_items, item_betrag, item_datum, item_code;
private ConstraintLayout layout_user_bestellung;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item_items = itemView.findViewById(R.id.items);
item_betrag = itemView.findViewById(R.id.betrag);
item_datum = itemView.findViewById(R.id.datum);
item_code = itemView.findViewById(R.id.code);
layout_user_bestellung = itemView.findViewById(R.id.layout_user_bestellung);
}
}
}
EDIT (didn't work)
I tried to set the text via itemView like the epicDialog.
#Override
public int getItemCount() {
return bestellung.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView item_items, item_betrag, item_datum, item_code;
private ConstraintLayout layout_user_bestellung;
private TextView tw;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item_items = itemView.findViewById(R.id.items);
item_betrag = itemView.findViewById(R.id.betrag);
item_datum = itemView.findViewById(R.id.datum);
item_code = itemView.findViewById(R.id.code);
layout_user_bestellung = itemView.findViewById(R.id.layout_user_bestellung);
tw = itemView.findViewById(R.id.order_overview_number);
}
}
#Override
public void onBindViewHolder(#NonNull UserBestellAdapter.ViewHolder holder, final int position) {
//Gesamtpreis: holder.item_betrag.setText(String.valueOf(bestellung.get(position).getBetrag()));
// Datum: holder.item_datum.setText(bestellung.get(position).getDatum());
holder.item_items.setText(bestellung.get(position).getProdukte());
//holder.item_code.setText(bestellung.get(position).getBestellnummer());
String bestellid =bestellung.get(position).getBestellnummer() + "";
holder.item_code.setText(bestellid);
holder.item_betrag.setText(Double.toString(bestellung.get(position).getSumme()));
holder.item_datum.setText(bestellung.get(position).getDatum());
holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TextView order_overview_number = epicDialog.findViewById(R.id.order_overview_number);
//order_overview_number.setText();
epicDialog.setContentView(R.layout.user_popup_order_overview);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
System.out.println(bestellung.get(position).getBestellnummer());
holder.tw.setText("hallo");
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.show();
}
});
}
Try
holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View v = View.inflate(mContext, R.layout.user_popup_order_overview, null);
EditText order_overview_number = (EditText) v.findViewById(R.id.order_overview_number);
order_overview_number.setText(String.valueOf(bestellung.get(position).getDatum()));
Button btn_order_overview_finish = (Button) v.findViewById(R.id.btn_order_overview_finish);
System.out.println(bestellung.get(position).getBestellnummer());
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.setContentView(v);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
epicDialog.show();
}
});
Good day!
There is a code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<PojoClassPrc> prc;
Context context;
RecyclerViewAdapter(Context context, List<PojoClassPrc> procedures) {
this.prc = procedures;
this.inflater = LayoutInflater.from(context);
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
PojoClassPrc procedures = prc.get(position);
holder.sName1.setText(procedures.getsText1());
holder.sName3.setText(procedures.getsText3());
holder.sName2.setText(procedures.getsText2());
holder.sName1.setTextColor(Color.parseColor("#010101"));
holder.sName2.setTextColor(Color.parseColor("#ACACAC"));
holder.sName3.setTextColor(Color.parseColor("#737373"));
}
#Override
public int getItemCount() {
return prc.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final TextView sName1, sName2, sName3;
final CheckBox sName5;
final CardView sName4;
ViewHolder(View view) {
super(view);
sName1 = (TextView) view.findViewById(R.id.lblListItem);
sName5 = (CheckBox) view.findViewById(R.id.checkBox2);
sName3 = (TextView) view.findViewById(R.id.lblListItem3);
sName2 = (TextView) view.findViewById(R.id.lblListItem2);
sName4 = (CardView) view.findViewById(R.id.item_card);
final boolean[] clicked = {false};
SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean cbSelect = prefs.getBoolean("sName", false);
if (cbSelect){
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
} else {
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
}
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!clicked[0]) {
sName5.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
clicked[0] = true;
savePrefs(true, "sName");
} else {
sName5.setChecked(false);
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
clicked[0] = false;
savePrefs(false, "sName");
}
}});
}
}
private void savePrefs(boolean value, String name) {
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
editor.putBoolean(name, value);
editor.apply();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
sName4 is CardView, the point is that I want to implement such a thing:
I clicked on the card, the color of the text changed and the checkbox was set, but in the second press it is necessary to make it as before, say the checkbox is removed, and the color turns white, I can’t realize it in any way with if else, I don’t catch up with what and how, tell me please!
Thank you in advance!!!
P.S. I have a lot of fragments, so I do everything in the adapter
You can use state of CheckBox by calling method isChecked()
Try this:
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (sName3.isChecked()){
sName3.setChecked(false);
sName3.setTextColor(Color.parseColor("#17fc6b")); // set your color
}else{
sName3.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
}
}});
more about checkbox
I'm creating an app for ordering food, and I created a Dish class and a Dish array adapter.
Currently you can add quantity for a specific dish by clicking on the view.
However, I wish to have 2 buttons to add or sub the quantity.
How can I make the 2 buttons for each dish, without the need to write for each element its ows button code? is there a way to make an "add" and "sub" methods and that the listview will know on which view it was clicked and by that update its quantity?
public Dish(String dishName, int dishPrice, int Image, int quantity) {
mdishName = dishName;
mdishPrice = dishPrice;
mdishPic = Image;
mquantity = quantity;
}
public class DishAdapter extends ArrayAdapter<Dish> {
public DishAdapter(Activity context, ArrayList<Dish> dishes){
super(context, 0, dishes);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout, parent, false);
}
final Dish currenDish = getItem(position);
TextView dishName = (TextView) listItemView.findViewById(R.id.dishName);
dishName.setText(currenDish.getDishName());
TextView dishPrice = (TextView) listItemView.findViewById(R.id.dishPrice);
dishPrice.setText(String.valueOf(currenDish.getDishPrice()));
ImageView image = (ImageView) listItemView.findViewById(R.id.dishPic);
image.setImageResource(currenDish.getDishPic());
image.setVisibility(View.VISIBLE);
TextView quantity = (TextView) listItemView.findViewById(R.id.quantity);
quantity.setText(String.valueOf(currenDish.getQuantity()));
return listItemView;
}
public class DesertsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deserts_activity);
final ArrayList<Dish> dishes = new ArrayList<Dish>();
dishes.add(new Dish("Number Cake",180, R.drawable.cake_number, 0));
dishes.add(new Dish("Ear of Haman", 40, R.drawable.ozen_haman, 0));
dishes.add(new Dish("Alphachores", 35, R.drawable.alphachores, 0));
dishes.add(new Dish("Snow Cookies", 35, R.drawable.snow_cookies, 0));
DishAdapter adapter = new DishAdapter(this, dishes);
final ListView listView = (ListView) findViewById(R.id.deserts_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
dishes.set(position, new Dish(dishes.get(position).getDishName(),
dishes.get(position).getDishPrice(), dishes.get(position).getDishPic(),
dishes.get(position).getQuantity()+1));
TextView quantity = (TextView) view.findViewById(R.id.quantity);
quantity.setText(String.valueOf(dishes.get(position).getQuantity()));
}
});
}
}
You have to create a separate Adaptor Class and then apply button click listeners on the buttons. I am attaching the image for the reference.
and My product Adaptor Is
public class ProductAdaptor extends RecyclerView.Adapter<ProductAdaptor.ViewHolder> {
List<ProductsItem> productsItems;
Context context;
public ProductAdaptor(Context context, List<ProductsItem> productsItems)
{
this.context = context;
this.productsItems = productsItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.single_product_design,viewGroup, false);
return new ProductAdaptor.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
final ProductsItem productsItem = productsItems.get(i);
viewHolder.productname.setText(productsItem.getProductName());
Glide.with(context).load(productsItem.getProductImageUrl()).into(viewHolder.productImage);
viewHolder.stock.setText(productsItem.getStock());
viewHolder.farmername.setText(productsItem.getFarmerName());
// add listener on single Item
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
viewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// apply code to increment the number
// first of all get the value form text counter and increment after that bind on the UI
}
});
viewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// first of all get the value form text counter and decrement after that bind on the UI
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return productsItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView productname;
ImageView productImage;
TextView stock, farmername, specility,qty;
Button btnPlus, btnMinus;
LinearLayout btnAdd;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
productname = itemView.findViewById(R.id.productname);
productImage = itemView.findViewById(R.id.productimage);
stock = itemView.findViewById(R.id.stcokValue);
farmername = itemView.findViewById(R.id.farmerName);
qty = itemView.findViewById(R.id.qty);
specility = itemView.findViewById(R.id.specility);
btnPlus = itemView.findViewById(R.id.plusbutton);
btnMinus = itemView.findViewById(R.id.minusbtn);
btnAdd = itemView.findViewById(R.id.add_button);
}
}
}
product Adapter
public class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder>{
private Context context;
private List<Product> listProducts;
private SqliteDatabase mDatabase;
public ProductAdapter(Context context, List<Product> listProducts) {
this.context = context;
this.listProducts = listProducts;
mDatabase = new SqliteDatabase(context);
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list_layout, parent, false);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
final Product singleProduct = listProducts.get(position);
holder.name.setText(singleProduct.getName());
// holder.quantity.setText(singleProduct.getQuantity());
//when i call quantity(the ABOVE value) the application crashes and when i call only name the field the values gets displayed
holder.editProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editTaskDialog(singleProduct);
}
});
holder.deleteProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDatabase.deleteProduct(singleProduct.getId());
((Activity)context).finish();
context.startActivity(((Activity) context).getIntent());
}
});
}
#Override
public int getItemCount() {
return listProducts.size();
}
private void editTaskDialog(final Product product){
LayoutInflater inflater = LayoutInflater.from(context);
View subView = inflater.inflate(R.layout.add_product_layout, null);
final EditText nameField = (EditText)subView.findViewById(R.id.enter_name);
final EditText quantityField = (EditText)subView.findViewById(R.id.enter_quantity);
if(product != null){
nameField.setText(product.getName());
quantityField.setText(String.valueOf(product.getQuantity()));
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Edit product");
builder.setView(subView);
builder.create();
builder.setPositiveButton("EDIT PRODUCT", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String name = nameField.getText().toString();
final String quantityStr = quantityField.getText().toString();
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
{
Toast.makeText(context, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
}
else
{
final int quantity = Integer.parseInt(quantityStr);
if(quantity<=0)
{
Toast.makeText(context,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
}
else {
mDatabase.updateProduct(new Product(product.getId(), name, quantity));
//refresh the activity
((Activity)context).finish();
context.startActivity(((Activity)context).getIntent());
}
}
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "Task cancelled", Toast.LENGTH_LONG).show();
}
});
builder.show();
}
}
product viewHolder
public class ProductViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView quantity;
public ImageView deleteProduct;
public ImageView editProduct;
public ProductViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.product_name);
deleteProduct = (ImageView)itemView.findViewById(R.id.delete_product);
editProduct = (ImageView)itemView.findViewById(R.id.edit_product);
quantity=(TextView)itemView.findViewById(R.id.product_name2);
}
}
Now the issue here is when is use holder.quantity.setText(singleProduct.getQuantity()) the application crashes.
I also wanted to add date picker dialog and want to display it in the recycler view so for date the code is same like holder.object.setText or something else and can we use a form for alert dialog like
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
dialog.setContentView(R.layout.dialog_dark);
dialog.setCancelable(true);
I have created the database file but the issue is mainly related to recycler view contents and if i wanted multiple holder is it possible to get the contents?
getQuantity() returns a int. So, Android is trying to search for a resource with id == getQuantity() leading to the crash (since you probably any resource with that id.
In other words, you are invoking TextView.setText(int resourceId) and not TextView.setText(String text)).
To fix, change this:
holder.quantity.setText(singleProduct.getQuantity());
To this:
holder.quantity.setText(String.valueOf(singleProduct.getQuantity()));
Android recyclerview item click event after update Recyclerview
Image description here
Mail 1 READ
Mail 2 Dont Read
In the item the click event I want to do this to me
How can you help make
public class Mesajlar_Adapter extends RecyclerView.Adapter<Mesajlar_Adapter.ViewHolder>{
private List<Mesajlar> resultModels;
private Context context;
public Mesajlar_Adapter(List<Mesajlar> resultModels, Context context) {
this.resultModels = resultModels;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mesajlar_listview_desing, parent, false);
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}
public void resetList() {
int size = this.resultModels.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
resultModels.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
public void addItem(Mesajlar item){
this.resultModels.add(item);
this.notifyDataSetChanged();
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mesaj_id.setText(resultModels.get(position).getMESAJ_ID());
holder.kullanici_id.setText(resultModels.get(position).getKULLANICI());
holder.gelen_detay.setText(resultModels.get(position).getDETAY());
holder.kategori.setText(resultModels.get(position).getKATEGORI());
holder.konu.setText(resultModels.get(position).getKONU());
holder.tarih.setText(resultModels.get(position).getKAYIT_TARIHI());
String a = resultModels.get(position).getOKUNDU();
holder.okunma.setText(a);
if (a.equals("Hayir")){
holder.kategori.setTextColor(Color.BLACK);
holder.kategori.setTextSize(19);
holder.konu.setTextColor(Color.BLACK);
holder.konu.setTextSize(19);
holder.tarih.setTextColor(Color.BLACK);
}
}
#Override
public int getItemCount() {
return resultModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView mesaj_id;
protected TextView kullanici_id;
protected TextView gelen_detay;
protected TextView konu;
protected TextView kategori;
protected TextView tarih;
protected CheckBox secim;
protected TextView okunma;
public ViewHolder(View view) {
super(view);
this.okunma =(TextView) view.findViewById(R.id.okunma);
this.mesaj_id = (TextView) view.findViewById(R.id.mesaj_id);
this.kullanici_id = (TextView) view.findViewById(R.id.kullanici_id);
this.gelen_detay = (TextView) view.findViewById(R.id.gelen_detay);
this.konu = (TextView) view.findViewById(R.id.Konu);
this.kategori = (TextView) view.findViewById(R.id.Kategori);
this.tarih = (TextView) view.findViewById(R.id.Tarih);
// this.secim = (CheckBox) view.findViewById(R.id.secim);
view.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i=new Intent(context, Mesaj_Detay_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String detay = gelen_detay.getText().toString();
String mesajid = mesaj_id.getText().toString();
String kullaniciid = kullanici_id.getText().toString();
i.putExtra("detay", detay);
i.putExtra("mesaj_id",mesajid);
i.putExtra("kullanici_id",kullaniciid);
context.startActivity(i);
}
}
}
First of all, add this interface for click event
public interface OnRecyclerItemViewClickListener {
void onClicked(int position, Mesajlar jar);
}
Model
public class Mesajlar {
...
boolean isClicked;
public boolean isClicked() {
return icClicked;
}
public void setClicked(boolean isClicked) {
this.isClicked = isClicked;
}
...
}
Adapter
private OnRecyclerItemViewClickListener listener;
public Mesajlar_Adapter(List<Mesajlar> resultModels, Context context, OnRecyclerItemViewClickListener listener) {
...
this.listener = listener;
}
...
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (resultModels.get(position).isClicked()) {
// change pressed background color here!
} else {
// change default background color here!
}
holder.itemView.setOnClickListener(v -> {
listener.onClicked(position, resultModels.get(position));
});
...
}
Activity
When you create RecyclerView Adapter, add OnRecyclerItemViewClickListener to constructor of adapter in Activity
Mesajlar_Adapter adapter = new Mesajlar_Adapter(list, context, new OnRecyclerItemViewClickListener {
#Override
public void onClicked(int position, Mesajlar jar) {
jar.setClicked(true);
adapter.notifyItemChanged(position);
Intent i = new Intent(YourActivity.this, Mesaj_Detay_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("detay", jar.getDETAY());
i.putExtra("mesaj_id",jar.getMESAJ_ID());
i.putExtra("kullanici_id", jar.getKULLANICI());
startActivity(i);
}
});
Instead of handling click event in adapter level you can use interface and do it in activity level. Pass the adapter position to the activity, from activity you can do the actual process
see the sample http://wiki.workassis.com/android-recyclerview-example/