简体   繁体   中英

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.

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

Either setText("") or setText(null) — either one of those would work. I use setText(null) for readability reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM