gpt4 book ai didi

java - 在 Android 中关闭警报对话框后,如何在 Activity 调用上刷新 TextView?

转载 作者:行者123 更新时间:2023-11-30 02:45:02 26 4
gpt4 key购买 nike

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

通话 Activity :

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;
}

}

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

Activity :

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

在 .setpositive 按钮中:

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

最佳答案

打开对话框不会触发 Activity 的 onPause/onResume(我不确定对话框 fragment 是否如此)。相反,您可以申请 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)
}
});

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)功能传递(ObjectDay objDay);

关于java - 在 Android 中关闭警报对话框后,如何在 Activity 调用上刷新 TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25199733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com