Answer the question
In order to leave comments, you need to log in
What is the error when saving the value to the database?
Hello! I'm finishing a small application for accounting for goods. The last step remains. I need to save the values changed in the fields to the database.
Here is my activity code. So far I have written the code for only one button.
public class InnerActivity extends AppCompatActivity {
private ApparatDatabaseHelper dbHelper = new ApparatDatabaseHelper(this);
//private ProductsAdapter productsAdapter = new ProductsAdapter();
public static final String EXTRA_PRODUCT = "extraProduct";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner_product);
int idNumber = (Integer)getIntent().getExtras().get(EXTRA_PRODUCT);
SQLiteDatabase db = dbHelper.getReadableDatabase();
final Cursor cursor = db.query ("products", null, "id = ?", new String[] { Integer.toString(idNumber+1) }, null, null, null);
//final Cursor cursor = db.query ("products", null, "id = 1", null, null, null, null);
final List<Product> products = new ArrayList<>();
if (cursor.moveToFirst()) {
//Получение данных напитка из курсора
String nameText = cursor.getString(1);
int position = cursor.getInt(2);
int factCount = cursor.getInt(3);
int stockCount = cursor.getInt(4);
TextView name = (TextView) findViewById(R.id.productName);
name.setText(nameText);
TextView pos = (TextView) findViewById(R.id.posCount);
pos.setText(String.valueOf(position));
TextView fact = (TextView) findViewById(R.id.factAppCount);
fact.setText(String.valueOf(factCount));
TextView stock = (TextView) findViewById(R.id.stockCount);
stock.setText(String.valueOf(stockCount));
}
cursor.close();
}
public void pluseOneFact(View view) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
int productNo = (Integer)getIntent().getExtras().get("productNo");
// Do something in response to button click
TextView factPlus = (TextView) findViewById(R.id.factAppCount);
String newfactplus = factPlus.getText().toString();
int intNewFact = Integer.valueOf(newfactplus);
int plusNewFact = intNewFact + 1;
factPlus.setText(String.valueOf(plusNewFact));
ContentValues productValues = new ContentValues();
productValues.put("product", plusNewFact);
db.update("products", productValues,
"id = ?", new String[] {Integer.toString(productNo)});
db.close();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question