I have a scenario where I have to insert the data into the database but the data is not getting stored in the Database. Its returning null.
Here is my sample code:
package com.example.abcd.products;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import com.example.constant.CC;
import com.example.database.ProductDatabase_adapter;
import com.example.dataset.ProductDataset;
import com.example.point_of_sale.R;
import com.example.point_of_sale.commons.BaseActivity;
import com.example.point_of_sale.commons.Popups;
public class AddProducts extends BaseActivity {
private ArrayList<ProductDataset> items = new ArrayList<ProductDataset>();
RadioGroup radioGroup;
EditText item_code;
EditText item_name;
Spinner item_category_add_product;
EditText item_description;
Spinner spinner_unit_add_product;
EditText cost_price;
EditText selling_price;
EditText product_offer;
EditText sell_price;
EditText tax_type;
EditText stock_count;
EditText sell_promotion;
Spinner spinner_Reward_Points_add_product;
EditText reward_points;
CheckBox checkBox_shipping_address;
View customView;
int product_id;
boolean is_edit_mode = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_products);
initViews(); // initializations
String[] item_category = { "category1", "category2", "category3",
"category4", };
String[] unit = { "unit1", "unit2", "unit3", "unit4", };
String[] reward_points = { "5%", "10%", "15%", "20%" };
ArrayAdapter<String> item_category_add_product_adpter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, item_category);
item_category_add_product_adpter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item_category_add_product.setAdapter(item_category_add_product_adpter);
item_category_add_product
.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String selction = (String) item_category_add_product
.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> spinner_add_product_array_list = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, unit);
spinner_add_product_array_list
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_unit_add_product.setAdapter(spinner_add_product_array_list);
spinner_unit_add_product
.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> spinner_Reward_Points_add_product_array_list_ = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, reward_points);
spinner_Reward_Points_add_product_array_list_
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_Reward_Points_add_product
.setAdapter(spinner_Reward_Points_add_product_array_list_);
spinner_Reward_Points_add_product
.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String Spinner_client_select_discount = (String) spinner_Reward_Points_add_product
.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Custom Actionbar starts
customActionBar();
// Custom Actionbar ends
Bundle extras = getIntent().getExtras();
if (extras != null) {
is_edit_mode = true;
product_id = extras.getInt("product_id");
fillproductDetail(product_id);
}
}
public void customActionBar() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar ab = getSupportActionBar();
ab.setTitle("Add Products");
}
public void initViews() {
item_code = (EditText) findViewById(R.id.item_code);
item_name = (EditText) findViewById(R.id.item_name);
item_category_add_product = (Spinner) findViewById(R.id.item_category_add_product);
item_description = (EditText) findViewById(R.id.item_description);
spinner_unit_add_product = (Spinner) findViewById(R.id.spinner_unit_add_product);
cost_price = (EditText) findViewById(R.id.cost_price);
selling_price = (EditText) findViewById(R.id.selling_price);
product_offer = (EditText) findViewById(R.id.product_offer);
sell_price = (EditText) findViewById(R.id.sell_price);
tax_type = (EditText) findViewById(R.id.tax_type);
stock_count = (EditText) findViewById(R.id.stock_count);
sell_promotion = (EditText) findViewById(R.id.sell_promotion);
spinner_Reward_Points_add_product = (Spinner) findViewById(R.id.spinner_Reward_Points_add_product);
reward_points = (EditText) findViewById(R.id.reward_points);
checkBox_shipping_address = (CheckBox) findViewById(R.id.checkBox_shipping_address);
}
public void validateAndSaveCategory() {
String item_code_val = item_code.getText().toString();
String item_name_val = item_name.getText().toString();
String item_description_val = item_description.getText().toString();
String cost_price_val = cost_price.getText().toString();
String selling_price_val = selling_price.getText().toString();
String product_offer_val = product_offer.getText().toString();
String tax_type_val = tax_type.getText().toString();
String stock_count_val = stock_count.getText().toString();
String sell_promotion_val = sell_promotion.getText().toString();
// String reward_points_val = reward_points.getText().toString();
ProductDataset pObj = new ProductDataset();
// Validation starts
String errors = "\n";
if (item_code_val.equals("-None-")) {
errors = "Please specify a Item Code \n ";
}
else if (item_name_val.equals("")) {
errors = "Please specify a name \n ";
}
else if (item_description_val.equals("")) {
errors = "Please specify a Item description \n ";
}
else if (cost_price_val.equals("")) {
errors = "Please specify a Cost Price \n ";
}
else if (selling_price_val.equals("")) {
errors = "Please specify a Selling Price \n ";
}
else if (product_offer_val.equals("")) {
errors = "Please specify a Offer Type \n ";
}
else if (tax_type_val.equals("")) {
errors = "Please specify the type of the tax \n ";
}
else if (stock_count_val.equals("")) {
errors = "Please specify the stock count \n ";
}
else if (sell_promotion_val.equals("")) {
errors = "Please specify the sell promotion \n ";
}
// if (reward_points_val.equals("")) {
// errors += "Please specify the reward points \n ";
// }
// if (imageViewName.equals("")) {
// errors += "Please specify a Image \n ";
// }
// if (spinner_Reward_Points_add_product_val.equals("")) {
// errors += "Please specify a Reward Points \n ";
// }
// Validation ends
if (!errors.equals("\n")) {
Popups.showPopup(errors, AddProducts.this);
} else {
// pObj.setCategory_id("1");
pObj.setItem_code(item_code.getText().toString());
pObj.setItem_name(item_name.getText().toString());
pObj.setItem_description(item_description.getText().toString());
pObj.setCost_price(cost_price.getText().toString());
pObj.setSelling_price(selling_price.getText().toString());
pObj.setProduct_offer(product_offer.getText().toString());
pObj.setTax_type(tax_type.getText().toString());
pObj.setStock_count(stock_count.getText().toString());
pObj.setSell_promotion(sell_promotion.getText().toString());
pObj.setReward_points((String) spinner_Reward_Points_add_product
.getSelectedItem());
pObj.setReward_promotion(reward_points.getText().toString());
pObj.setUnit((String) spinner_unit_add_product.getSelectedItem());
// pObj.setPhoto_add(imageViewName.getText().toString());
ProductDatabase_adapter mDbHelper1 = new ProductDatabase_adapter(
AddProducts.this);
mDbHelper1.createDatabase();
mDbHelper1.open();
boolean pObj_inserted = false;
if (!is_edit_mode) {
Log.d(CC.LOGTAG, "" + pObj.toString());
pObj_inserted = mDbHelper1.insert_product_data(pObj);
mDbHelper1.close();
} else {
Log.d(CC.LOGTAG, "" + pObj.toString());
mDbHelper1.open();
items = mDbHelper1.getAllProduct("detail", "" + product_id);
pObj.setProduct_id(items.get(0).getProduct_id());
pObj_inserted = mDbHelper1.update_product_data(pObj);
mDbHelper1.close();
}
if (pObj_inserted) {
Popups.showPopup("Product Saved Successfully !",
AddProducts.this);
// category_item.setText("");
// category_name.setText("");
item_code.setText("");
item_name.setText("");
item_description.setText("");
cost_price.setText("");
selling_price.setText("");
product_offer.setText("");
tax_type.setText("");
stock_count.setText("");
sell_promotion.setText("");
reward_points.setText("");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.done_cancel_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();overridePendingTransition(CC.close, CC.close2);
return true;
case R.id.add_action_bttn:
validateAndSaveCategory();
return true;
case R.id.cancel_action_bttn:
refreshActivity(getIntent());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void fillproductDetail(int pro_id) {
ProductDatabase_adapter mDbHelper = new ProductDatabase_adapter(
AddProducts.this);
mDbHelper.createDatabase();
mDbHelper.open();
items = mDbHelper.getAllProduct("detail", "" + pro_id);
mDbHelper.close();
Log.d(CC.LOGTAG, "" + items.toString());
item_code.setText(items.get(0).getItem_code());
item_name.setText(items.get(0).getItem_name());
item_description.setText(items.get(0).getItem_description());
cost_price.setText(items.get(0).getCost_price());
selling_price.setText(items.get(0).getSelling_price());
product_offer.setText(items.get(0).getProduct_offer());
tax_type.setText(items.get(0).getTax_type());
stock_count.setText(items.get(0).getStock_count());
sell_promotion.setText(items.get(0).getSell_promotion());
reward_points.setText(items.get(0).getReward_points());
is_edit_mode = true;
}
}
I am stuck. Please help so that I can proceed further. Thanks in advance
Aucun commentaire:
Enregistrer un commentaire