Passing correct Info to Dialog - android

I am looking at creating a Dialog with a bundle of data. This all work correctly.
I have a number of buttons when clicked opens the dialog with the data and can then be updated.
Trouble I am having is it does not matter which button I press the bundle is only the data for the last row. Any ideas on getting the correct data passed to the Dialog for each button.
I was thinking down the line of setting an id for each view as I pass through the loop but unsure how to call that back again
Code:
for (int i = 0; i < nameInfo.size(); i++) {
// creating the views
View viewItem = (View) inflater.inflate(R.layout.view_item, null);
nameView = (TextView) viewItem.findViewById(R.id.title);
nameView.setId(i);
value1View = (TextView) viewItem.findViewById(R.id.value1);
value1View.setId(i);
value2View = (TextView) viewItem.findViewById(R.id.value2);
value2View.setId(i);
updateButton = (Button) sightmarkView.findViewById(R.id.updatebutton);
updateButton.setId(i);
// Getting the values
nameValue = nameInfo.get(i).toString();
value1 = db.getvalue1('1', nameInfo.get(i).toString());
value2 = db.getvalue2('2', nameInfo.get(i).toString());
// update fields
nameView.setText(nameValue);
value1View.setText(String.valueOf(value1));
value2View.setText(String.valueOf(value2));
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int updateButtonId = updateButton.getId();
bundle = new Bundle();
bundle.putString("name", nameValue);
bundle.putFloat("value1", value1);
bundle.putFloat("value2", value2);
showDialog(SIGHTMARK_DIALOG_ID, bundle);
}
});
pMainlayout.addView(viewItem);
}
Thanks for your time

In each loop (from the for), you are redefining the values of nameValue, value1 and value2.
To fix the problem, just change your code to match this:
// Getting the values
final String nameValue = nameInfo.get(i).toString();
final String value1 = db.getvalue1('1', nameInfo.get(i).toString());
final String value2 = db.getvalue2('2', nameInfo.get(i).toString());

Related

Do calculations on numbers in 1 multiline textView and return result replacing numbers

I am very new to android programming and am trying to complete my first app. It is a recipe converter.
I have stored my recipe details in a SQLite DB and the text for ingredients is just one multiline string separated by carriage returns. I have used a cursor to get the ingredient data into a textview which returns text like (could be numerous variants):
100ml Water
500 g Mince
2 x 400g can crushed tomatoes
etc.
I originally had each Qty, Unit and Ingredient Description stored separately in the database which made life easy when converting but I chose to store it in a multiline string to allow copying and pasting of ingredients from the internet or another source.
I am attempting to extract the numbers and then multiply them by a percentage, then return the new converted numbers, and the corresponding unit and description to get something like this:
(multiplied by 200%)
200ml Water
1000g Mince
4 x 400g can crushed tomatoes
I just don't know how to do it though. Can anyone help please?
Thanks
UPDATE:
I have tried to do something like this to get the numbers.
public void Split() {
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
tvSplit.setText("");
String text = s.replaceAll("\\D+", ",");
tvSplit.append(text);
tvSplit.append("\n");
}
That shows me all of the numbers with a "," between them but it also includes all numbers in the string like in the above example prior to conversion it would show 100,500,2,400 when I only need 100,500,2. Then from that point I'm not sure how I would convert them all. My "fresh to programming mind" thought that I could store these in a temp SQL table by INSERT INTO tablename (id, originalvalues) VALUES (my string ie 100,500,2).
I could then pull them back out, do the calculation, update the table, then add them back into my textview with the remaining string. I haven't got that far yet, so I'm just wondering what the correct way to do it is.
UPDATE 2:
As per my comments, this is the code I used to show an alert dialog with each item listed on a separate line, I then used the selected line to find the number before any " " to then display the text on the screen.
public void PopUpSpinnerDialogue() {
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
String s = tvTest.getText().toString();
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, s.split("\n"));
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
}
It is this idea that I think I can use but I don't know how to code it and then display the results.
UPDATE 3:
The code or at least the idea of the code I am trying to use is this.
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
String[] ingreds = s.split("\n");
tvSplit.setText("");
String[] parts = ingreds.split(" ");
String Qty = parts[0];
String Units = parts[1];
String Ingredients = parts[2];
Integer QtyInt = Integer.parseInt(Qty);}
ingreds.split doesn't work and also, I don't know how to specify splitting the parts for each i.
I ended up using regex. It allowed the data to be entered with or without a space. So I ended up using this code to pull out the Qty of each line, multiply it by a percentage, append the text (units,ingredient description) to the line, then add it to a string array, to add to my alert dialog.
Code is here.
public void Split() {
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
TextView tvTest = (TextView) findViewById(R.id.tvTest);
TextView tvPercent = (TextView) findViewById(R.id.tvPercent);
String tvP = tvPercent.getText().toString();
String tvNumOnly = tvP.replaceAll("\\D+", "");
Integer PercentVal = Integer.parseInt(tvNumOnly);
String s = tvTest.getText().toString();
StringBuilder sb = new StringBuilder();
Pattern p = Pattern.compile("((\\d*) ?(.*)(?:\\n*))");
Matcher m = p.matcher(s);
while (m.find()) {
String Qty = m.group(2) + ".00";
String Ingred = m.group(3);
Float QtyFloat = Float.parseFloat(Qty);
Float newQTY = (QtyFloat * PercentVal) / 100;
String newQTYstr = newQTY.toString();
sb.append(newQTYstr + " " + Ingred + "\n");
}
String[] lines = sb.toString().split("\n");
String[] IngredArray = Arrays.copyOfRange(lines, 0, lines.length - 1);
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, IngredArray);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D.\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
// Toast.makeText(SelectConvertMethod.this, sb, Toast.LENGTH_LONG).show();
}

How to set dynamic edit text in a table row in android

I am creating an application consists of two drop down spinners(Example:Spinner A and Spinner B). By clicking button i want to get subtracted value from spinner A and B with that final subtracted value i want to create table rows with inside edit text fields.i had written some code. When i clicks the button no error was prompting but there was no table creation happening.This is what i did below when user clicks button please help me with this where i did them mistake
get_sheet = (Button)findViewById(R.id.button_get_sheet);
get_sheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner_from_date_text = from_Date.getSelectedItem().toString();
spinner_from_month_text = from_month.getSelectedItem().toString();
spinner_from_year_text = from_year.getSelectedItem().toString();
spinner_to_date_text = to_Date.getSelectedItem().toString();
spinner_to_month_text = to_month.getSelectedItem().toString();
spinner_to_year_text = to_year.getSelectedItem().toString();
from_date.setText(spinner_from_date_text+"/"+spinner_from_month_text+"/"+spinner_from_year_text);
to_date.setText(spinner_to_date_text+"/"+spinner_to_month_text+"/"+spinner_to_year_text);
int value_from = Integer.valueOf(spinner_from_date_text);
int value_to = Integer.valueOf(spinner_to_date_text);
int result_table = value_to-value_from;
sheet.setColumnStretchable(5, true);
for(int i=0;i<result_table;i++)
{
sheet_row = new TableRow(MainActivity.this);
sheet_row.setLayoutParams(new LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,android.widget.TableRow.LayoutParams.WRAP_CONTENT));
for(int j=0 ;j<5;j++)
{
edt_text = new EditText(MainActivity.this);
edt_text.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
edt_text.setBackgroundResource(R.drawable.edt);
}
sheet_row.addView(edt_text);
}
sheet.addView(sheet_row);

How to dynamically add rows and detect and delete clicked?

I'm dynamically adding rows to the table. I have few textViews[] that I add to the row[] and then I add row[] to the table. That's ok, but then I want to detect click on textView[] and delete that row, also I want the numbers of the textViews below deleted ones to decrease.
example:
1 a a a //(I delete this row)
2 b b b
3 c c c
after deleting, I want to have this:
1 b b b
2 c c c
I have tried adding onClickListener to one textView[], but sometimes it deletes row[] and decreases number, sometimes don't.
Can someone write me some example to try out?
EDIT: Here's my code (I think it's all that's needed for this)
This is the code on my button for adding rows:
public TextView textViewKoeficijent[] = new TextView[50];
public TextView textViewBr[] = new TextView[50];
public TextView textViewObrisi[] = new TextView[50];
public TextView textViewTip[] = new TextView[50];
public TextView textViewPar[] = new TextView[50];
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun * fkoeficijent;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
final TableLayout PopisParova = (TableLayout) findViewById(R.id.TableLayout);
final TableRow noviPar[] = new TableRow[50];
LayoutParams paramsBroj = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
paramsBroj.setMargins(4, 0, 2, 4);
LayoutParams paramsPar = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsPar.setMargins(2, 0, 2, 4);
LayoutParams paramsKoef = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsKoef.setMargins(2, 0, 4, 4);
//onclicklistener:
OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
brpara --;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
PopisParova.removeViewAt(id);
//PopisParova.removeView(noviPar[id]);
for(i=1; i<=brpara; i++){
if(i>id){
String bri = String.valueOf(i-1);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i-1);
}
}
}};
{
textViewBr[brpara] = new TextView(MainActivity.this);
textViewBr[brpara].setLayoutParams(paramsBroj);
textViewBr[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewBr[brpara].setBackgroundColor(0xFFFFFFFF);
brojPara = String.valueOf(brpara);
textViewBr[brpara].setText(brojPara);
textViewBr[brpara].setId(brpara);
}
{
textViewPar[brpara] = new TextView(MainActivity.this);
textViewPar[brpara].setLayoutParams(paramsPar);
textViewPar[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewPar[brpara].setBackgroundColor(0xFFFFFFFF);
textViewPar[brpara].setText(par);
textViewPar[brpara].setId(brpara);
}
{
textViewTip[brpara] = new TextView(MainActivity.this);
textViewTip[brpara].setLayoutParams(paramsPar);
textViewTip[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewTip[brpara].setBackgroundColor(0xFFFFFFFF);
textViewTip[brpara].setText(tip);
textViewTip[brpara].setId(brpara);
}
{
textViewKoeficijent[brpara] = new TextView(MainActivity.this);
textViewKoeficijent[brpara].setLayoutParams(paramsPar);
textViewKoeficijent[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewKoeficijent[brpara].setBackgroundColor(0xFFFFFFFF);
textViewKoeficijent[brpara].setText(koeficijent);
textViewKoeficijent[brpara].setId(brpara);
}
{
textViewObrisi[brpara] = new TextView(MainActivity.this);
textViewObrisi[brpara].setLayoutParams(paramsKoef);
textViewObrisi[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewObrisi[brpara].setBackgroundColor(0xFFFFFFFF);
textViewObrisi[brpara].setText("X");
textViewObrisi[brpara].setId(brpara);
textViewObrisi[brpara].setClickable(true);
textViewObrisi[brpara].setOnClickListener(onClickListener);
}
noviPar[brpara] = new TableRow(MainActivity.this);
noviPar[brpara].addView(textViewBr[brpara]);
noviPar[brpara].addView(textViewPar[brpara]);
noviPar[brpara].addView(textViewTip[brpara]);
noviPar[brpara].addView(textViewKoeficijent[brpara]);
noviPar[brpara].addView(textViewObrisi[brpara]);
PopisParova.addView(noviPar[brpara]);
brpara++;
editTextPar.setText("");
editTextTip.setText("");
editTextKoeficijent.setText("");
EDIT [2]:
brpara is my counter so I know which is number of row which is being added. I have a max of 20 rows.
Also, my loop is working perfectly for deleting one row, but when I delete multiple rows at once, it only changes my row numbers for the first time and after that only deletes rows.
What are you doing is this:
Let's say that brpara = 3.
You click the view with id = 2.
You execute brbara --; So brbara is now 2.
You check if id != brpara they are NOT so no renumbering. Logical Problem!
Furthermore you have this loop:
for(i=id+1; i<=brpara; i++){
i=id+1;
String bri = String.valueOf(id);
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);}
You say that i must be id+1 (in our example 3) to < brpara (in our example 2) so 3 <= 2 it never executes, if however id and brpara allows loop execution you resetting the i inside the loop
i=id+1;
If for example the loop starting with i = id+1 let's say id=2 so it is now 3 and go inside the loop the i will be always 3 because you are resetting it inside the loop.
It is not clear from your code how are you using brpara but the loop for renumbering is wrong. So let's assume that brpara is ALWAYS the last index in the array then I suggest the following code:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
//FIRST RENUMBER ALL VIEWS FROM id+1 to brpara
if(id!=brpara){
for(i=id+1; i<=brpara; i++){
String bri = String.valueOf(i - 1); //<--HERE MAYBE MUST BE i-1 NOT id...
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);} //THIS DOESN'T FEEL RIGHT MAYBE i INSTEAD OF id? OR i - 1?
}
//AND FINALY REMOVE VIEW AND DECREASE brpara...
PopisParova.removeViewAt(id);
brpara --;
}
My suggestion assumes that brpara is always the last index and you are renumbering from the clicked view + 1 to the end. However there is a more elegant solution which is to create a function that renumbers the whole array from top to bottom regardless the view clicked and call it whenever a view gets added or removed. That way you will not have to manage brpara at all but this depends on what exactly you are trying to achieve.
Based on your Edits
Try this:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if((brpara - 1)==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
} else {
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
PopisParova.removeViewAt(id);
brpara --;
for(i=1; i<=brpara; i++){
String bri = String.valueOf(i);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i);
}
}
}
Since brpara is a counter you should decrease it only when you are actually remove the view. You may have to change the
if((brpara - 1)==1){
with
if(brpara==1){
based on your needs. But now the loop seems to be OK.
Hope this helps...
The only possible solution is to replace deleted rows, and their data, with rows following, and their data and delete last row. So it looks like the deleted row is really deleted.
row[x] = row[x+1] // want to delete this
row[x+1] = row[x+2]
//... and like that until the last row
row[last] // delete this row

Problems with putExtra and putStringArrayList

I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}

SQLite Update Not Applying Android

When I click on a ListItem, it opens up a custom dialog with 4 EditText fields. The fields are set with current data depending on the row that is clicked. The purpose of the dialog is to allow the user to update the data (it is a financial app). I am having trouble actually applying the update when the user clicks "submit" in the dialog. There are no errors in the app when I run. Here is the onclick method:
protected void onListItemClick(ListView l, View v, int position, long id) {
List<Debt> values = datasource.getAllDebt();
Debt item = values.get(position);
final long boxId = item.getId();
// final String BoxId = String.valueOf(boxId);
final String BoxName = item.getName();
final String BoxBalance = item.getBalance();
final String BoxApr = item.getApr();
final String BoxPayment = item.getPayment();
// set up dialog
final Dialog dialog = new Dialog(manageDebts.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Edit Debt Details");
dialog.setCancelable(true);
// set up text
EditText et1 = (EditText) dialog.findViewById(R.id.editText1);
EditText et2 = (EditText) dialog.findViewById(R.id.editText2);
EditText et3 = (EditText) dialog.findViewById(R.id.editText3);
EditText et4 = (EditText) dialog.findViewById(R.id.editText4);
et1.setText(BoxName);
et2.setText(BoxBalance);
et3.setText(BoxApr);
et4.setText(BoxPayment);
// set up button
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
datasource.updateDebt(boxId, BoxName, BoxBalance, BoxApr,
BoxPayment);
dialog.dismiss();
}
});
dialog.show();
}
The Update Method in my Database helper class is shown here:
public boolean updateDebt(long updateId, String debt_name, String debt_total, String apr, String payment)
{
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_ID, updateId);
values.put(MySQLiteHelper.COLUMN_DEBT_NAME, debt_name);
values.put(MySQLiteHelper.COLUMN_DEBT_TOTAL, debt_total);
values.put(MySQLiteHelper.COLUMN_APR, apr);
values.put(MySQLiteHelper.COLUMN_PAYMENT, payment);
return database.update(MySQLiteHelper.TABLE_DEBT, values, MySQLiteHelper.COLUMN_ID + " = " + updateId, null) > 0;
}
I have verified that the COLUMN_ID and the updateId are pointing to the correct rows in the ListView and the SQL database.
Does someone see something I am not?
Perhaps you are violating a constraint with your update? Just a guess without seeing the DB code.
EDIT
Lose the single quotes around the row id variable, that is making the DB treat it as a string, and a string compared to a number is a fail.
This will work better:
String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{ String.valueOf(updateId) };
return database.update(MySQLiteHelper.TABLE_DEBT,
values, whereClause, whereArgs) > 0;
The String.valueOf() call simply converts the ID to a String value.
I test your code in my computer.
The update method works fine.
So I think you should post more code .
or You should check your logic.
I was missing a step between 2 and 3.
Setting the variable again, inside the onClick method. Once the user pressed the update button, the variable had to be RE-SET to the new value of the EditText field. Like this (simplified version):
String name = null;
EditText et;
name = debt.getName();
et.setText(name);
onclick {
***name = et.getText().toString();***
datasource.updateDebt(name);
}

Categories

Resources