在Android中关闭警报对话框后,如何在活动调用时刷新TextView?

时间:2014-08-08 09:05:02

标签: java android android-activity alertdialog

我有一个警告对话框,允许用户编辑TextView。目前,一旦警报对话框关闭,用户必须点击后退按钮,然后重新输入要更新的TextView的活动。我在SO上尝试了很多解决方案,但似乎都没有。当用户单击“保存更改”时,TextView应该更新。

致电活动:

package com.group1.workouttracker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;

public class DayActivity extends Activity {
    //does not extend ListActivity, so list functions must be called by myList object

    private String buttonClicked;
    private String thisSummary;
    private Intent intent;
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_day);

        db = DatabaseHelper.getInstance(getApplicationContext());

        intent = getIntent();
        buttonClicked = intent.getStringExtra("Day");

        Button buttonCreateExercise = (Button) findViewById(R.id.buttonAddExercise);
        buttonCreateExercise.setOnClickListener(new OnClickListenerCreateExercise(buttonClicked));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void readSummary(String buttonClicked) {
        TextView textViewSummary = (TextView) findViewById(R.id.textViewSummary);
        textViewSummary.setOnLongClickListener(new OnLongClickListenerEditSummary(buttonClicked));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        thisSummary = db.readSummary(buttonClicked).getSummary();
        TextView summary = (TextView) findViewById(R.id.textViewSummary);
        summary.setOnLongClickListener(new OnLongClickListenerEditSummary(buttonClicked));
        summary.setText(thisSummary);
    }
}

长按激活的警报对话框:

package com.group1.workouttracker;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;

public class OnLongClickListenerEditSummary implements View.OnLongClickListener {

    Context context;
    String dayClicked = "";

    @Override
    public boolean onLongClick(View view) {
        context = view.getContext();

        final DatabaseHelper db = DatabaseHelper.getInstance(context);
        ObjectDay objectDay = db.readSummary(dayClicked);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.edit_summary_form, null, false);

        final long dayId = objectDay.getId();
        final String dName = objectDay.getDayName();
        final EditText editTextSummary = (EditText) formElementsView.findViewById(R.id.editTextSummary);

        final CharSequence[] items = { "Edit", "Delete" };

        new AlertDialog.Builder(context).setTitle("Exercise");
        new AlertDialog.Builder(context)
                .setView(formElementsView)
                .setTitle("Edit Summary for " + dayClicked + ":")
                .setPositiveButton("Save Changes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                ObjectDay objectDay = new ObjectDay();
                                objectDay.setId(dayId);
                                objectDay.setDayName(dName);
                                objectDay.setSummary(editTextSummary.getText().toString());

                                boolean updateSuccessful = DatabaseHelper.getInstance(context).updateSummary(objectDay);

                                if(updateSuccessful) {
                                    Toast.makeText(context, "Summary was updated.", Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    Toast.makeText(context, "Unable to update summary.", Toast.LENGTH_SHORT).show();
                                }

                                //dialog.cancel();
                                dialog.dismiss();
                            }

                        }).show();

        return false;
    }

    public void editRecord(final String dName) {

        final DatabaseHelper db = DatabaseHelper.getInstance(context);
        ObjectDay objectDay = db.readSummary(dName);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.edit_summary_form, null, false);

        final EditText editTextDay = (EditText) formElementsView.findViewById(R.id.editTextSummary);
        final EditText editTextSummary = (EditText) formElementsView.findViewById(R.id.editTextSummary);

        editTextSummary.setText(objectDay.getSummary());
    }

    public OnLongClickListenerEditSummary(String dayClicked) {
        this.dayClicked = dayClicked;
    }

}

编辑:通过添加以下代码,我能够正常工作:

的活动:

public void passThrough(ObjectDay objDay) {
    textViewSummary.setText(objDay.getSummary());
}

在.setpositive按钮中:

((DayActivity) context).passThrough(objectDay);

1 个答案:

答案 0 :(得分:1)

打开一个对话框不会触发Activity的onPause / onResume(我不确定对话片段是否属实)。相反,您可以将onDismissListener(可以是Activity的成员变量或匿名)应用于对话框。

当对话框关闭时(无论如何),您将获得一些信息,您可以更新文本字段。或者,您可以从正面点击监听器中执行相同的操作。

无论

alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //UPDATE FROM HERE (call a method or manipulate an Activity member var)
    }
});

OR

alert.setOnDismissListener(new OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
        //UPDATE FROM HERE (Check DialogInterface  for positive if you want)
    }
});

编辑,例如:

class Whatever extends Activity {

    private TextView mTextView;
    private MyPassThroughListener mPassThroughListener = new MyPassThroughListener() {
           @Override
           public function passThrough(ObjectDay objDay) {
                  mTextView.setText(objDay.getSummary());    
           }
    }

    protected void onCreate(Bundle savedInstanceState) {
        ....
        buttonCreateExercise.setOnClickListener(new OnClickListenerCreateExercise(mPassThroughListener ));


    }

}

class OnLongClickListenerEditSummary {
   MyPassThroughListener mPassThroughListener;
   ...

   public OnLongClickListenerEditSummary (MyPassThroughListener passThroughListener) {
       mPassThroughListener = passThroughListener;
   }

   @Override
   public boolean onLongClick(View view) {
      ....

       new AlertDialog.Builder(context)
            .setPositiveButton("Save Changes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            ObjectDay objectDay = new ObjectDay();
                            objectDay.setId(dayId);
                            objectDay.setDayName(dName);
                            objectDay.setSummary(editTextSummary.getText().toString());

                            mPassThroughListener.passThrough(objectDay);

                            dialog.dismiss();
                        }

     ...
}

}

public interface MyPassThroughListener {      public function passThrough(ObjectDay objDay); }