Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String TAG = MainActivity.class.getSimpleName();
- private SqliteDatabase mDatabase;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- FrameLayout fLayout = (FrameLayout) findViewById(R.id.activity_to_do);
- RecyclerView productView = (RecyclerView)findViewById(R.id.product_list);
- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
- productView.setLayoutManager(linearLayoutManager);
- productView.setHasFixedSize(true);
- mDatabase = new SqliteDatabase(this);
- List<Product> allProducts = mDatabase.listProducts();
- if(allProducts.size() > 0){
- productView.setVisibility(View.VISIBLE);
- ProductAdapter mAdapter = new ProductAdapter(this, allProducts);
- productView.setAdapter(mAdapter);
- }else {
- productView.setVisibility(View.GONE);
- Toast.makeText(this, "There is no product in the database. Start adding now", Toast.LENGTH_LONG).show();
- }
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- addTaskDialog();
- }
- });
- }
- private void addTaskDialog(){
- LayoutInflater inflater = LayoutInflater.from(this);
- 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);
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Add new product");
- builder.setView(subView);
- builder.create();
- builder.setPositiveButton("ADD 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(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
- }
- else
- {
- final int quantity = Integer.parseInt(quantityStr);
- if(quantity<=0)
- {
- Toast.makeText(MainActivity.this,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
- }
- else {
- Product newProduct = new Product(name, quantity);
- mDatabase.addProduct(newProduct);
- //refresh the activity
- finish();
- startActivity(getIntent());
- }
- }
- }
- });
- builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MainActivity.this, "Task cancelled", Toast.LENGTH_LONG).show();
- }
- });
- builder.show();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(mDatabase != null){
- mDatabase.close();
- }
- }
- }
- //product.java(in connection to sqlite)
- public class Product {
- private int id;
- private String name;
- private int quantity;
- public Product(String name, int quantity) {
- this.name = name;
- this.quantity = quantity;
- }
- public Product(int id, String name, int quantity) {
- this.id = id;
- this.name = name;
- this.quantity = quantity;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getQuantity() {
- return quantity;
- }
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
- }
- //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());
- 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);
Advertisement
Add Comment
Please, Sign In to add comment