onActivityResult在片段中无法正常工作

时间:2014-05-18 15:05:39

标签: android autocompletetextview onactivityresult

我正在开发导航应用程序。我使用两个autocompletetextviews。当我写地址给他们时,它的作品和绘制路线。但是,当我从数据库中选择一个地址时,程序从数据库片段中获取地址并将其设置为第二个autocompletetextview,当单击加载按钮时它不起作用。地图上没有路线。程序什么都不做。 我对两种方式都使用相同的活动类。你为什么不工作? 你有什么想法吗? 谢谢。

此处为AutoCompleteDirectionsActivity.java

public class AutoCompleteDirectionsActivity extends Activity {

public static final int RESULT_CODE = 123;
private AutoCompleteTextView from = null;
public static AutoCompleteTextView to = null;


@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.directions);

    Button btnLoadDirections = (Button) findViewById(R.id.load_directions);
    from = (AutoCompleteTextView) findViewById(R.id.from);
    to = (AutoCompleteTextView) findViewById(R.id.to);
    from.setText("My Location");

    if (BookmarksArrayAdapter.checkNumber == 1) {

        String address = getIntent().getStringExtra("address");
        to.setText(address);
        BookmarksArrayAdapter.checkNumber = 0;
    }

    from.setAdapter(new AutoCompleteDirectionsActivityAdapter(this,
            android.R.layout.simple_dropdown_item_1line));
    to.setAdapter(new AutoCompleteDirectionsActivityAdapter(this,
            android.R.layout.simple_dropdown_item_1line));

    btnLoadDirections.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (!to.getText().toString().equals("")
                    && !from.getText().toString().equals("")) {

                Intent data = new Intent();
                data.putExtra("from", from.getText().toString());
                data.putExtra("to", to.getText().toString());

                setResult(RESULT_CODE, data);
                finish();
            } else {
                Toast.makeText(TabActivity.mainContext,
                        "Please, Enter The Destiantion Location",
                        Toast.LENGTH_LONG).show();
            }

        }
    });

}

}

片段类

中的

onActivityResult 方法

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == AutoCompleteDirectionsActivity.RESULT_CODE) {

        progressDialog = new ProgressDialog(
                getSherlockActivity());
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();

        String from = data.getExtras().getString("from");
        String to = data.getExtras().getString("to");

        System.out.println(from);
        System.out.println(to);

        new DirectionsFetcher(from, to).execute();

    }
}

0 个答案:

没有答案