0

我有一个设置菜单,用于应用程序的所有屏幕。现在,当我单击菜单并转到特定屏幕编辑时,设置并按回,然后我无法知道从哪个屏幕单击了菜单项。我试图将活动名称放在意图中,但这听起来不是个好主意..请帮忙。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.propsmain);
    setTitle("Settings");
    prev_screen = getIntent().getStringExtra("prev_screen");
    try {
        final ArrayList<Properties> propsList = getProperties();
        if (propsList == null || propsList.size() == 0) {
                       // show alert
        } else {
            setListAdapter(new PropertyAdapter(this, R.layout.props,
                    propsList));
        }
        final ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {

            // @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Properties prop = propsList.get(arg2);
                Bundle b = new Bundle();
                b.putString("prop_name", prop.getName());
                b.putString("prop_value", prop.getValue());

                Intent intent = new Intent(SetProperties.this,
                            EditPropertyActivity.class);
                    intent.putExtras(b);
                    startActivity(intent);
                }
        });

    } catch (PropertyFileMissing e) {
        AlertDialog dialog = new AlertDialog.Builder(SetProperties.this)
                .create();
        dialog.setTitle("Failure");
        dialog.setMessage(e.toString());
        dialog.setIcon(R.drawable.error);
        dialog.setButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        dialog.show();
    } 
}

// EditProperty 类在我的清单中有一个对话框主题

    ublic class EditPropertyActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog);
    setTitle("");
    TextView propName = (TextView) findViewById(R.id.tv_propName);
    final String tagName = getIntent().getExtras().getString("prop_name");
    final String value = getIntent().getExtras().getString("prop_value");

    propName.setText(getIntent().getExtras().getString("prop_label"));

    final EditText ed_propValue = (EditText) findViewById(R.id.ed_propValue);
    ed_propValue.setText(value);
    if (tagName.equals("timeout")) {
        ed_propValue.setInputType(InputType.TYPE_CLASS_NUMBER);
    }
    Button btn_save = (Button) findViewById(R.id.btn_save);
    btn_save.setOnClickListener(new View.OnClickListener() {

        // @Override
        public void onClick(View v) {
            String newValue = ed_propValue.getText().toString().trim();
            if ((tagName.equals("timeout") || tagName.equals("serverUrl") || tagName
                    .equals("channelName")) && (newValue.equals(""))) {
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.NOT_NULL),
                        Toast.LENGTH_LONG).show();
                return;
            }

            editFile(tagName, newValue);
            finish();
        }
    });

    Button btn_cancel = (Button) findViewById(R.id.btn_cancel);
    btn_cancel.setOnClickListener(new View.OnClickListener() {

        // @Override
        public void onClick(View v) {
            finish();
        }
    });
}

}

——阿毗拉沙

4

1 回答 1

0

很抱歉延迟发布答案。我在使用以下方法解决问题时遇到了问题。

public void onBackPressed() {
    super.onBackPressed();
    if(count == 0){
        super.onBackPressed();
    }else{
    for (int i = 0; i < count; i++) {
        super.onBackPressed();
    }

而且在我单击对话框活动上的保存按钮后,我添加了下面的代码来更新所选项目的值。而不是用新的意图再次启动活动,我只是按照 Waqas 的建议调用“finish();”。

    SetProperties.propsList = getProperties();
    PropertyAdapter adapter = (PropertyAdapter) SetProperties.listview.getAdapter();
    adapter.notifyDataSetChanged();
    finish();

感谢大家的帮助和指导:)

于 2012-06-12T08:17:33.290 回答