如何将AskDialog中的数据恢复到活动状态

时间:2015-04-27 16:47:49

标签: android dialog

我有一个带有listview的activity1,点击列表中的一个项目后,它会显示自己的askDialog和2个选项(按钮)。我需要找回我点击的哪一个从我的activity1打开另一个活动列表。我怎样才能做到这一点? 我的AskDialog:

public class AskDialog extends Dialog implements
    android.view.View.OnClickListener {

public Activity c;

private Button edit;
private Button delete;
private int position;
private boolean tariff;

public AskDialog(Activity a,int position,boolean tariff) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
    this.position = position;
    this.tariff = tariff;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.ask_dialog);
    edit = (Button) findViewById(R.id.editBtn);
    if(tariff){
        edit.setText("Zobrazit záznam");
    }
    delete = (Button) findViewById(R.id.deleteBtn);
    edit.setOnClickListener(this);
    delete.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.editBtn:

            break;
        case R.id.deleteBtn:
dismiss();
            break;
        default:
            break;
        }
        dismiss();
    }
}

这就是我称之为对话框的地方:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    positionOfTAriff = position;
AskDialog askDialog = new AskDialog(MyCommonListActivity.this,position,false);
askDialog.show();

或者我应该使用哪个更好的对话框?

3 个答案:

答案 0 :(得分:0)

您可以使用共享偏好设置:

SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

阅读偏好:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

编辑和保存偏好

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

您可以在AsdkDialog中提交首选项并读入您的活动或片段。

答案 1 :(得分:0)

可以有两种方法

  1. 创建界面,使用创建的界面实现您的活动/片段,将其引用传递给对话框并在对话框中初始化,而不是onDestroy()使用对话框中保存的参考调用您的界面方法
  2. 在拨打dialog.show();来电dialog.setTargetFragment(TargetFragment, RequestCode);之前,将对话框更新为DialogFragment,并在onActivityResult以及Fragment中实施Activity,并在销毁之前对话呼叫getTargetFragment().onActivityResult(RequestCode, Activity.RESULT_OK, intent);

答案 2 :(得分:0)

将Callback参数添加到对话框的构造函数中。

Callback myCallback;
public AskDialog(Activity a, int position, Callback myCallback, boolean tariff) {
super(a);
this.c = a;
this.position = position;  
this.tariff = tariff;
this.myCallback = myCallback;
}

更改onClick:

public void onClick(View v) {
switch (v.getId()) {
    case R.id.editBtn:
        {
            Bundle bundle = new Bundle();
            bundle.putString("choice", "edit");
            bundle.putInt("position", position);

            Message msg = new Message();
            msg.setData(bundle);
            myCallback.handleMessage(msg);

            dismiss();
       }
    break;
    case R.id.deleteBtn:
       {
            Bundle bundle = new Bundle();
            bundle.putString("choice", "delete");
            bundle.putInt("position", position);

            Message msg = new Message();
            msg.setData(bundle);
            myCallback.handleMessage(msg);

            dismiss();
       }
        break;
    default:
        break;
    }  
}

在您调用对话框的地方,您将创建回调处理程序:

 Callback theCallback = new Callback() {
    // @Override
    public boolean handleMessage(Message msg) {
        // super.handleMessage(msg);
        Bundle bundle = msg.getData();
        String choice = bundle.getString("choice");
        int position = bundle.getInt("position");
        Toast.makeText(MainActivity.this, choice + "\nposition: " + position, Toast.LENGTH_SHORT) .show();
        return true;
    }
};

你用以下方式调用对话框:

 new AskDialog( MainActivity.this, position, theCallback, false).show();