Update data using SQL android studio - android-studio

DAO
#Query("SELECT * FROM shopping WHERE modified = 1")
fun getModify(shopping: Shopping)
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun modify(shopping: Shopping)
Implementation
binding.btl2.setOnClickListener {
val toast = Toast.makeText(applicationContext, "HelloTEST", Toast.LENGTH_SHORT)
toast.show()
viewModel.getModify()
viewModel.modify(
Shopping(
product = binding.edl1.text.toString(),
quantity = binding.edl2.text.toString(),
price = binding.edl3.text.toString(),
bought = binding.ch1.isChecked
))
}
How to implement it well? I have error with viewModel.getModify().
I would like to run these two methods getModify and then modify() after one click.
So program should choose from database only these which are checked as true(1) and then using button modify.

Related

Call a class from a string flutter

I have several cities, and when I click on on a specific city I want that the next page shows the model from that city. As each city contains alot of models, I have a seperate class for each models in the city.
List<City> cities = City.fetchAll();
for (i = 0; i < cities.length; i++) {
if (cityID == cities[i].id) {
// returns 'Paris' when paris is clicked on
String city = cities[i].name;
}
}
// Then I want to call the class TestParis and fetch the the list from that model:
final test = TestParis.fetchAll();
// or it could be another city:
final test = TestMadrid.fetchAll();
//
final test = 'Test${city}'.toClass.fetchAll();
So basically convert string to initate a class. How can I do that?
Not possible in Flutter because reflactions not available in flutter ... in dart only there is the mirror package ... but here you can only build a map with constructor functions something like that
final test = {
'TestParis' : TestParis.fetchAll
}['Test${city}'].call() ;

AndroidStudio kotlin AlertDialogue with spinner populated with setItems

I want to show a modal dialogue for the user to select a Bluetooth device in the case that I can't guess it from the device names.
It appears that AlertDialogue has the facility to show a spinner / dropdown.
The alert dialogue builder has a method setItems which I seem to think is what I need, however, its parameter is CharSequence[] but I have some sort of array of strings (I can't tell exactly what I have because everything is just val).
private fun showDialog() {
val names = (bta!!.bondedDevices).map { z -> z.name };
// What is the type of names? How can you find this out?
// How can you make it into a CharSequence[]?
val ab = AlertDialog.Builder(this);
ab.setTitle("Select device");
ab.setIcon(android.R.drawable.ic_dialog_alert); // I'd prefer a question mark.
ab.setPositiveButton("Select"){dialogueInterface, which -> Toast.makeText(applicationContext, "Selected", Toast.LENGTH_LONG).show()};
ab.setNeutralButton("Cancel"){dialogueInterface, which -> Toast.makeText(applicationContext, "Cancelled", Toast.LENGTH_LONG).show()};
ab.setItems(names); // None of the following functions can be called with the arguments supplied.
val a = ab.create();
a.setCancelable(false);
a.show();
}
I think this works in Java, but it doesn't in kotlin
CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
So:
In AndroidStudio how can you tell the type of a variable? (In VisualStudio if you hover over a var then the tooltip tells you.)
How in kotlin do you make a CharSequence[]?
fun elmFind() {
// Find device
val pairedDevices: Set<BluetoothDevice>? = bta!!.bondedDevices
// Try to guess.
for (device in pairedDevices!!) {
if (device.name.contains("obd")) {
elmConnect(device.name);
return;
}
}
// Still going therefore didn't find one therefore ask.
val cs: Array<CharSequence> = pairedDevices.map { z -> z.name }.toTypedArray()
var elmDeviceName: String = ""
val ab = AlertDialog.Builder(this);
ab.setTitle("Select device");
ab.setIcon(android.R.drawable.ic_dialog_alert);
ab.setPositiveButton("Select") { dialogueInterface, which ->
elmConnect(elmDeviceName);
};
ab.setNeutralButton("Cancel") { dialogueInterface, which ->
Toast.makeText(
applicationContext,
"Cancelled",
Toast.LENGTH_LONG
).show()
};
ab.setItems(cs) { dialog, which -> elmDeviceName = cs[which].toString() };
val a = ab.create();
a.setCancelable(false);
a.show();
}
Will this work with the local variable elmDeviceName being used to get the value of the selected item in the case when the OK button is pressed?

How to define multiple sharedPreferences?

I have managed to get the sharedPreferences saving values. But i don't know how to make it reference the text i am clicking on. In the // Close Alert Window section when i click ok to change the text. Ok dismisses alert dialog, then suppose to add the new price to list in sharedPreferences.
In the putString() if i use putString("Price$it", input.text.toString()).applyit doesn't appear to do anything. However if i use "Price1" any text i change is saved and upon reopening the app Price1is changed to the new price. So i know the method is working. i just have no clue how to save the particular text i am editing. I hope this makes sense. Thanks for your time.
// Created Private Price List
val sharedPreferences = getSharedPreferences("priceList", Context.MODE_PRIVATE)
//Price
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
tv.text = sharedPreferences.getString("Price$it","0.00")
}
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
tv.setOnLongClickListener {
//Alert Window
val alertDialog = AlertDialog.Builder(this#MainActivity).create()
alertDialog.setTitle("NEW PRICE")
val input = EditText(this#MainActivity)
//Alert Submit on Enter
input.setOnKeyListener { v, keyCode, event ->
if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
// Input changes text
tv.text = input.text
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
else -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
// Close Alert Window
alertDialog.dismiss()
// TODO Save Price Table //THIS PART vvv
sharedPreferences.edit().putString("Price1", input.text.toString()).apply()
}
false
}
val lp = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
input.layoutParams = lp
alertDialog.setView(input)
alertDialog.show()
return#setOnLongClickListener true
}
}
You are shadowing it. In your scope you are referencing the argument of tv.setOnLongClickListener. Specify the argument name so it's not shadowed by inner lambdas.
(1..912).forEach { index ->
...
sharedPreferences.edit().putString("Price$index", input.text.toString()).apply()
}

Insert data to grid in acumatica

I have a grid (show the devices) and action MATERIALS (a grid in a smart panel - show the materials of each device).
In an action Get Devices (code below), I want to load devices from another table to this grid also materials of each device to smart panel MATERIALS.
After the action is executed, just the devices are shown. The panel MATERIALS doesn't have any record, I try to press the Refresh button but there still doesn't have data.
The magic is that after I press Save button in the header, data in MATERIALS is shown correctly.
public PXAction<PSMTPhieuYeuCauSuaChuaMHTBHeader> GetDevices;
[PXUIField(DisplayName = "Get Devices")]
[PXButton(CommitChanges = true)]
protected void getDevices()
{
PSMTRequestRepairMaint thisGraph = PXGraph.CreateInstance<PSMTRequestRepairMaint>();
foreach (var record in PSMTPhieuYeuCauSuaChuaMMTBDeviceSelectedView.Cache.Updated)
{
PSMTPhieuYeuCauSuaChuaMMTBViewDevice device = (PSMTPhieuYeuCauSuaChuaMMTBViewDevice)record;
if (device.Check.HasValue && device.Check.Value)
{
PSMTPhieuYeuCauSuaChuaMMTBLineGrid line = (PSMTPhieuYeuCauSuaChuaMMTBLineGrid)thisGraph.PSMTPhieuYeuCauSuaChuaMMTBLineGridView.Cache.CreateInstance();
line = PSMTPhieuYeuCauSuaChuaMMTBLineGridView.Insert(line);
line.DeviceID = device.HeaderID;
line.FromDate = device.LastMaintenanceDate;
line.ToDate = device.LastMaintenanceDate;
device.Check = false;
line = PSMTPhieuYeuCauSuaChuaMMTBLineGridView.Update(line);
var materials = PXSelect<PSMTAllowedReplacementMaterial,
Where<PSMTAllowedReplacementMaterial.headerID,
Equal<Required<PSMTPhieuYeuCauSuaChuaMHTBHeader.soMay>>>>.Select(this, line.DeviceID);
foreach (var item in materials)
{
var allowedMaterial = (PSMTAllowedReplacementMaterial)item;
PSMTPhieuYeuCauSuaChuaMMTBMaterial material = (PSMTPhieuYeuCauSuaChuaMMTBMaterial)thisGraph.PSMTPhieuYeuCauSuaChuaMMTBMaterialView.Cache.CreateCopy(new PSMTPhieuYeuCauSuaChuaMMTBMaterial());
material = PSMTPhieuYeuCauSuaChuaMMTBMaterialView.Insert(material);
//material.LineGridID = line.GridID;
material.MaterialID = allowedMaterial.MaterialID;
material.Quantity = allowedMaterial.Quantity;
material = PSMTPhieuYeuCauSuaChuaMMTBMaterialView.Update(material);
}
}
}
}
Grid
Panel

Entity Framework 5: Context updated, but not View

I use Unit of Work and Repository patterns in my software.
There is a dataGridView on form and it gets data from next way:
acceptedBonusesGrid.DataSource = _unitOfWork.DocumentRepository.GetDocumentsInfo();
GetDocumentsInfo is in DocumentRepository. GetDocumentsInfoes - is complex view with joins:
public IQueryable<GetDocumentsInfo> GetDocumentsInfo()
{
return _context.GetDocumentsInfoes;
}
When user double-clicks on record, it's form for editing this record appears. I send unit of work to form:
CorrectionAcceptedForm corrForm = new CorrectionAcceptedForm(_unitOfWork, docNum);
Form opens and link to unit of work is in use:
public CorrectionAcceptedForm(UnitOfWork unitOfWork, int docNum)
{
InitializeComponent();
_unitOfWork = unitOfWork;
_documentData = _unitOfWork.DocumentRepository.GetById(docNum);
AssignValues();
}
User makes changes in the record and saves changes:
private void SaveBill(int billId)
{
if (ValidateChildren())
{
SaveSupplierAddress();
Bill billData = _documentData.Bills.FirstOrDefault(bill => bill.BillId == billId);
if (billData != null)
{
billData.Description = descriptionTextbox.Text;
billData.Price = Convert.ToDecimal(priceTextbox.Text.Replace('.', ','));
_unitOfWork.Save();
}
}
}
When this correction form closes, then datagrid on main form reloads:
acceptedBonusesGrid.DataSource = null;
acceptedBonusesGrid.Columns.Clear();
acceptedBonusesGrid.DataSource = _unitOfWork.DocumentRepository.GetDocumentsInfo();
But after reload - all rows in datagrid are old, that was before making changes.
What is the problem?
After 3 months I have discovered problem.
GetDocumentsInfo() has to be like this:
public IQueryable<GetDocumentsInfo> GetDocumentsInfo()
{
_context.GetDocumentsInfoes.MergeOption = MergeOption.OverwriteChanges;
return _context.GetDocumentsInfoes;
}
Now my DataGrid updates when I make changes in child form.

Resources