I have created an android app in java using eclipse which creates a Sqlite database and lets the user add data to it. Users can also search for existing data. I used android cursor objects. How do I clear the text box after a query is completed ? For example, I type "value" to retrieve all records associated with that value. Now I want to perform another search. When I click on the text box, I would like the text box to be cleared for my next search.
This is part of my code for inserting and retrieving data :
db.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT," + " number TEXT);");
Cursor cur = db.rawQuery("INSERT INTO " + MY_DATABASE_TABLE + " (name, number)" + " VALUES ( \"" + newname + "\", \"" + newnumber + "\");", null);
cur.moveToFirst();
while (cur.isAfterLast() == false)
{
result.append("\n" + "Id: " + cur.getString(0) + " | " + "Name: " + cur.getString(1) + " | " + "Phone Number: " + cur.getString(2) + "\n");
cur.moveToNext();
}
cur.close();
Cursor c = db.rawQuery("SELECT * FROM " + MY_DATABASE_TABLE, new String [] {} );
c.moveToFirst();
while (c.isAfterLast() == false) {
result.append("\n" + "Id: " + c.getString(0) + " | " + "Name: " + c.getString(1) + " | " + "Phone Number: " + c.getString(2) + "\n");
c.moveToNext();
}
c.close();
Thanks.
just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box. this should be the accepted answer, +1 for simplicity.
The most basic way to do this would be to simply reset your EditText views. If you have logic that drives the update of these fields, then resetting them to an empty String and requesting a "recalculation" to update the hints.
First you probably don't want to clear your database to just clear a textView. So the relevant code would be inside your activity.
So if you are using a textView you would do something like this in code
// Global variable
TextView yourTexView;
// In your activities on Create
yourTexView = (TextView) findViewById(R.id.yourTexView );
yourTexView .setOnClickListener(yourClickListener)
// Somewhere In the activities class
/**
* Tour Click Handler - This will clear the textview whenever someone clicks it.
*/
private OnClickListener yourClickListener= new OnClickListener() {
public void onClick(View v) {
yourTexView.setText("");
}
};
// In your xml file - change as you need
<TextView
android:id="@+id/yourTexView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
Hopefully this helps, Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With