Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to clear textView?

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.

like image 491
user1170168 Avatar asked Sep 26 '12 21:09

user1170168


People also ask

How do I clear a textbox in Android?

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.

How do you clear form data after submit in Android?

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.


1 Answers

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

like image 51
Dave Avatar answered Sep 23 '22 23:09

Dave