简体   繁体   中英

How to refresh my Textview from another activity

In my app, I am deleting data from my databasehelper class, and I want the Textview in the mainActivity to display the new changed data.So far this is my code.

public void DeleteData(){

    bDelete.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        Integer deletedRows = myDb.deleteData(etID.getText().toString());

        if(deletedRows > 0) {
            reload.Redraw();
            Toast.makeText(MainActivity.this, "Shit should work", Toast.LENGTH_LONG).show();
        }
    }})

This is in my second activity, where I call the refresh function

public void Redraw() {
    displayMsg.postInvalidate();
}

Just in case, my text

  public void nexttime() {
    Cursor res = myDb.GetFirstTime();
    if (res.getCount() == 0) {
      displayMsg.setText("No Appointment was found");
        return;
    }
    StringBuffer buffer = new StringBuffer();
    while (res.moveToNext()) {

      StringBuffer nextA = buffer.append("Date :" + res.getString(4) + "\n");
      displayMsg.setText(nextA);
}}

Have your main activity update its own TextView in its onResume() .

Whenever one activity is running, other activities do not exist. More precisely, they may exist, but you can never depend on it. Never attempt to directly access one activity instance from another.

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