如何从一个活动传输数据到另一个?数据

2023-09-07 22:02:55 作者:我没有城府

我试图将数据从一个页面转移到另一个,但有些问题是下面即将为code。我想从一个活动转移使用捆绑变量的值,但第二个事情是错了,请告诉我怎么回事错了。

下面是第一项活动: -

 包route.planning;进口android.app.Activity;进口android.content.Context;进口android.content.Intent;进口android.os.Bundle;进口android.view.View;进口android.view.View.OnClickListener;进口android.widget.Button;进口android.widget.Toast;公共类扩展登录活动{    / **当第一次创建活动调用。 * /    上下文mCtx;    最终静态INT START = 0;    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        mCtx =这一点;        按钮BTN =(按钮)findViewById(R.id.btn);        btn.setOnClickListener(新OnClickListener(){        // @覆盖        公共无效的onClick(视图v)        {            字符串fromLat =新的String();            字符串fromLong =新的String();            字符串toLat =新的String();            字符串toLong =新的String();            fromLat =将String.valueOf(R.id.FromLatitude);            fromLong =将String.valueOf(R.id.FromLongitude);            toLat =将String.valueOf(R.id.ToLatitude);            toLong =将String.valueOf(R.id.ToLongitude);            意向意图=新意图(mCtx,MapRouteActivity.class);            / *发送一些参数* /            束束=新包();              bundle.putString(fromLat,fromLat);              bundle.putString(加上FromLong,fromLong);              bundle.putString(toLat,toLat);              bundle.putString(toLong,toLong);              intent.putExtras(包);            / *启动活动* /            mCtx.startActivity(意向);            / *启动ActivityForResult * /            ((活动)mCtx).startActivityForResult(意向,2);        }    });    }    @覆盖    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){        // TODO自动生成方法存根        super.onActivityResult(要求code,结果code,数据);        如果(要求code == START)        {            Toast.makeText(mCtx,Integer.toString(结果code),Toast.LENGTH_SHORT).show();        }    }    @覆盖    公共无效的onDestroy(){        super.onDestroy();        完();    }} 

下面是我想从上面

获得价值第二活动的一部分

 进口java.io.IOException异常;进口的java.io.InputStream;进口java.net.MalformedURLException;进口的java.net.URL;进口java.net.URLConnection中;进口的java.util.ArrayList;进口的java.util.List;进口org.ci.geo.route.Road;进口org.ci.geo.route.RoadProvider;进口route.planning.R;进口android.graphics.Canvas;进口android.graphics.Color;进口android.graphics.Paint;进口android.graphics.Point;进口android.os.Bundle;进口android.os.Handler;进口android.widget.LinearLayout;进口android.widget.TextView;进口com.google.android.maps.GeoPoint;进口com.google.android.maps.MapActivity;进口com.google.android.maps.MapController;进口com.google.android.maps.MapView;进口com.google.android.maps.Overlay;公共类MapRouteActivity扩展MapActivity {        LinearLayout中的LinearLayout;        MapView类MapView类;        私家路mRoad;        @覆盖        公共无效的onCreate(捆绑savedInstanceState){                super.onCreate(savedInstanceState);                的setContentView(R.layout.routeplanning);                图形页面=(图形页面)findViewById(R.id.mapview);                mapView.setBuiltInZoomControls(真);                捆绑额外= this.getIntent()getExtras()。                最后弦乐平= extras.getString(fromLat);                最后弦乐fLong = extras.getString(加上FromLong);                最后弦乐= TLAT extras.getString(toLat);                最后弦乐腾龙= extras.getString(toLong);                新的Thread(){                        @覆盖                        公共无效的run(){                            双fromLat = Double.parseDouble(持平);                            双fromLon = Double.parseDouble(fLong);                            双toLat = Double.parseDouble(TLAT);                            双托隆= Double.parseDouble(腾龙);                              //双fromLat = 28.6353,fromLon = 77.2250,toLat = 30.7313,托隆= 76.7754;                                / *** URL包含路径从互联网上获取KML文件* /                                字符串URL = RoadProvider                                                .getUrl(fromLat,fromLon,toLat,托隆);                                InputStream为=的getConnection(URL);                                mRoad = RoadProvider.getRoute(是);                                mHandler.sendEmptyMessage(0);                        }                }。开始();        } 

解决方案

  fromLat =将String.valueOf(R.id.FromLatitude);        fromLong =将String.valueOf(R.id.FromLongitude);        toLat =将String.valueOf(R.id.ToLatitude);        toLong =将String.valueOf(R.id.ToLongitude); 
Android开发 一个活动向另一个活动传递数据的相关知识 五

下面是问题所在。你不读从活动的任何实际数据,但得到的字符串重新控制标识符presantation。

您需要获取用户输入,如果R.id.FromLatitude等等都是的EditText的标识符:

  fromLat =((的EditText)findViewById(R.id.FromLatitude))的getText()的toString(); 

I am trying to transfer data from one page to another but some problem is coming below is the code. I am trying to transfer value of variable using bundle from first activity to second but something is wrong please tell me whats going wrong.

below is first activity:-

package route.planning;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class login extends Activity {
    /** Called when the activity is first created. */

    Context mCtx;
    final static int START =0;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mCtx = this;


        Button btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new OnClickListener(){

        //  @Override
        public void onClick(View v)
        {
            String fromLat = new String();
            String fromLong = new String();
            String toLat = new String();
            String toLong = new String();

            fromLat=String.valueOf(R.id.FromLatitude);
            fromLong=String.valueOf(R.id.FromLongitude);
            toLat=String.valueOf(R.id.ToLatitude);
            toLong=String.valueOf(R.id.ToLongitude);


            Intent intent = new Intent(mCtx, MapRouteActivity.class);

            /*Sending some arguments*/ 
            Bundle bundle = new Bundle();

              bundle.putString("fromLat",fromLat );
              bundle.putString("fromLong",fromLong );
              bundle.putString("toLat",toLat );
              bundle.putString("toLong",toLong );


              intent.putExtras(bundle);

            /*Start Activity*/
            mCtx.startActivity(intent);

            /*Start ActivityForResult*/
            ((Activity)mCtx).startActivityForResult(intent, 2);
        }
    });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == START)
        {

            Toast.makeText(mCtx, Integer.toString(resultCode), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onDestroy(){
        super.onDestroy();
        finish();
    }
}

Below is part of second activity where i am trying to get value from above

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.ci.geo.route.Road;
import org.ci.geo.route.RoadProvider;

import route.planning.R;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapRouteActivity extends MapActivity {

        LinearLayout linearLayout;
        MapView mapView;
        private Road mRoad;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.routeplanning);
                mapView = (MapView) findViewById(R.id.mapview);
                mapView.setBuiltInZoomControls(true);

                Bundle extras = this.getIntent().getExtras();
                final String fLat=extras.getString("fromLat");
                final String fLong=extras.getString("fromLong");
                final String tLat=extras.getString("toLat");
                final String tLong=extras.getString("toLong");



                new Thread() {
                        @Override
                        public void run() {

                            double fromLat=Double.parseDouble(fLat);
                            double fromLon=Double.parseDouble(fLong);
                            double toLat=Double.parseDouble(tLat);
                            double toLon=Double.parseDouble(tLong);
                              //double fromLat = 28.6353, fromLon = 77.2250, toLat = 30.7313, toLon = 76.7754;
                                /***url contains the path to fetch the kml file from the internet*/
                                String url = RoadProvider
                                                .getUrl(fromLat, fromLon, toLat, toLon);
                                InputStream is = getConnection(url);
                                mRoad = RoadProvider.getRoute(is);
                                mHandler.sendEmptyMessage(0);
                        }
                }.start();
        }

解决方案

        fromLat=String.valueOf(R.id.FromLatitude);
        fromLong=String.valueOf(R.id.FromLongitude);
        toLat=String.valueOf(R.id.ToLatitude);
        toLong=String.valueOf(R.id.ToLongitude);

Here is the problem. You don't read any actual data from the activity, but get the string represantation of identifiers of controls.

You need to obtain the user inputs if R.id.FromLatitude and so on are identifiers of EditText:

fromLat = ((EditText)findViewById(R.id.FromLatitude)).getText().toString();