I have been able to retrieve values into my form.The values are inserted into a table on save button click and are at the same time retrieved onto the same form too(on the same save click).But the problem now is that on each click the values are getting displayed in a continuous flow.I want to clear the screen(flush the values from form) after the previous click and display only the latest table state values.
Heres the code i am working on:
public void fieldChanged(Field field, int context) //respond to button events
{
if (field == showInputButton) //if first button selected
{
Dialog.alert(TextField1.getText()); //show text from first input field
try
{
//Open or create the database
Database db = DatabaseFactory.openOrCreate("database1.db");
//Insert Data from db
Statement statement1 = db.createStatement("INSERT INTO DirectoryItems(category_id,name,phone,email) VALUES ('1','"+TextField1.getText()+"','"+TextField2.getText()+"','"+TextField3.getText()+"')");
statement1.prepare();
statement1.execute();
statement1.close();
//Retrieve data
try
{
add(new RichTextField("Attempting to retrieve data from " +
"database1.db on the SDCard."));
Statement st = db.createStatement("SELECT name FROM DirectoryItems");
st.prepare();
Cursor c = st.getCursor();
Row r;
int i = 0;
while(c.next())
{
r = c.getRow();
i++;
add(new RichTextField(i + "Name = " + r.getString(0)));
}
if (i==0)
{
add(new RichTextField("No data in the DirectoryItems table."));
}
st.close();
db.close();
}
catch( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
}
Does anyone know this.Please advise guys.
You can go ahead by disabling buttons so that you allow the user to fetch the values only once (i.e the latest values)
Use that in your implementation of field change.Disabling buttons helps retain a good grip over the fetched values(as in your case).