Android Question ListView item_Click problem

Richard P

Member
Licensed User
Longtime User
I am having problems with a App that i have brought up before. It is a very simple app that calculates the miles per gallon and cost per mile from three editText values entered by the user. This is my latest version in which i was hoping to use a scrollview but abandoned this after many days of work with no success so, reverted back to a two line listview which displays the values above, which are also saved in a database.

My problem started when i wanted to use 'item_click to display the full data set (not added all to the database yet) for the user to view, edit or delete. I have two problems;

1. The previous 'calc' button press that worked fine now throws up an error - 'invalid double' (not consistently and occasionally after pressing 'Next' after having entered litres opens up the alpha keypad, but only rarely

2. After pressing one of the items from the listview it is throwing up an error, although when you hover over the highlighted area of code it is evident that it has identified the listview item selected. Problem is in line 190 - 'syntax error (code 1) ........... '

I have uploaded the zip of the app and would appreciate it if someone could let me know what i am doing wrong?
 

Attachments

  • MPGcalcV5a.zip
    12.1 KB · Views: 274

derez

Expert
Licensed User
Longtime User
Line 190
B4X:
Cursor1 = SQL1.ExecQuery("SELECT * FROM fuelcost ID = " & Value)
should be in the format of line 118, like this:
B4X:
Cursor1 = SQL1.ExecQuery("SELECT * FROM fuelcost  WHERE ID = ?" ,array as String( Value))
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
Line 190
B4X:
Cursor1 = SQL1.ExecQuery("SELECT * FROM fuelcost ID = " & Value)
should be in the format of line 118, like this:
B4X:
Cursor1 = SQL1.ExecQuery("SELECT * FROM fuelcost  WHERE ID = ?" ,array as String( Value))

Thanks for the quick response. I have tried the code you suggested (in line 190 ref item_click) and throws up an 'too many parameters errror'?
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
The Execquery2 clears the error and i go through to the 'edit' layout but it does not display the items answerMPG and answerCost per mile from the listview/sql database in the'Edit' layout. I want to be able to edit the values entered by the user (haven't done the updated to the sql database yet) so will ultimately want to display the original litres, miles and cost for edit, view or delete?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I want to be able to edit the values entered by the user
The Database is not working well, you have to check it.
I propose these changes :
1. Add a panel named editpanel, instead of the edit activity -
B4X:
editpanel.Initialize("")
Activity.AddView(editpanel,0,0,100%x,100%y)
editpanel.Color = Colors.ARGB(255,255,255,255)
editpanel.LoadLayout("Edit.bal")
editpanel.Visible = False
2. populate the list by single lines with the two parameters, like this for example:
B4X:
ListView1.AddSingleLine("MPG= " & answerMPG & ", Cost= " & answerCost)
3. Get the data directly from the listview:
B4X:
Sub ListView1_ItemClick(Position As Int, Value As Object)
    editpanel.Visible = True
    Dim str() As String = Regex.Split(",",ListView1.GetItem(Position))
    edtMiles.Text = str(0).Replace("MPG= ","")
    edtPounds.Text = str(1).Replace(" Cost= ","")
End Sub
4. To go back from edit to main, add
B4X:
Sub btnExit_Click
    editpanel.Visible = False
End Sub

With these you'll be able to see the data in the edittexts and continue to update the DB (after you fix it).
I suspect this line query = "INSERT INTO fuelcost VALUES (NULL, ?, ?)" where the NULL is killing the ID, but I'm not sure.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I suspect this line query = "INSERT INTO fuelcost VALUES (NULL, ?, ?)" where the NULL is killing the ID, but I'm not sure.

The Value 'NULL' is just auto-incrementing the first field .. '(ID INTEGER PRIMARY KEY ..'
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
The Database is not working well, you have to check it.
I propose these changes :
1. Add a panel named editpanel, instead of the edit activity -
B4X:
editpanel.Initialize("")
Activity.AddView(editpanel,0,0,100%x,100%y)
editpanel.Color = Colors.ARGB(255,255,255,255)
editpanel.LoadLayout("Edit.bal")
editpanel.Visible = False
2. populate the list by single lines with the two parameters, like this for example:
B4X:
ListView1.AddSingleLine("MPG= " & answerMPG & ", Cost= " & answerCost)
3. Get the data directly from the listview:
B4X:
Sub ListView1_ItemClick(Position As Int, Value As Object)
    editpanel.Visible = True
    Dim str() As String = Regex.Split(",",ListView1.GetItem(Position))
    edtMiles.Text = str(0).Replace("MPG= ","")
    edtPounds.Text = str(1).Replace(" Cost= ","")
End Sub
4. To go back from edit to main, add
B4X:
Sub btnExit_Click
    editpanel.Visible = False
End Sub

With these you'll be able to see the data in the edittexts and continue to update the DB (after you fix it).
I suspect this line query = "INSERT INTO fuelcost VALUES (NULL, ?, ?)" where the NULL is killing the ID, but I'm not sure.


Thanks for this.
Looking through some of the tutorials and past questions there is always the balance between 1 module with multiple layouts or multiple modules.

I will work on this over the Christmas break, there are some new concepts that i have not used or come across yet so plenty to learn.

Once again thanks for the support.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
there is always the balance between 1 module with multiple layouts or multiple modules.
In this case there is much interface between the two activities, its easier to do all in the same activity.
If you still want two activities you should put the content for the edittexts in global variables and load it from there after starting the edit activity.
 
Upvote 0
Top