如何将数据发送到另一个类?

时间:2016-08-22 08:43:26

标签: android

我想将textview上的数据发送到EditText其他活动以更新帖子,我不知道如何传递数据

 btnEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(getApplicationContext(), UpdateClick.class);
            startActivity(in);
        }
    });


    try {
        if (Cars != null) {
            tvCarModel.setText("Car Model: "+Cars.Car_Model);
            tvCarType.setText("Car Type: "+ Cars.Car_Type);
            tvCapacity.setText("Capacity: "+Cars.Capacity);
            tvFuelType.setText("Fuel Type: "+Cars.FuelType);
            tvPlateNumber.setText("Plate Number: "+Cars.PlateNumber);
            tvPoster.setText("Posted by: " + Cars.owner);

            ImageLoader.getInstance().displayImage(Cars.Image, ivImage);

        }
    }catch (Exception e){
        System.out.println("Error in details " + e.toString());
    }

如何使用该Intent发送?谢谢:))

UpdateClick

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update_click);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MESSAGE_KEY);
    TextView tvCarModel = new TextView(this);
    tvCarModel.setTextSize(45);
    etCarModel.setText(message);
    setContentView(etCarModel);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    galleryPhoto = new GalleryPhoto(getApplicationContext());
    spFuelType = (Spinner) findViewById(R.id.spFuelType);
    adapter = ArrayAdapter.createFromResource(this, R.array.fuel_types,android.R.layout.simple_dropdown_item_1line);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spFuelType.setAdapter(adapter);
    spFuelType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    ImageLoader.getInstance().init(UILConfig.config(UpdateClick.this));


    etCarType = (EditText) findViewById(R.id.etCarType);
    etCapacity = (EditText) findViewById(R.id.etCapacity);
    etPlateNumber = (EditText) findViewById(R.id.etPlateNumber);
    ivImg = (ImageView) findViewById(R.id.ivImg);
    btnSelectPhoto = (Button) findViewById(R.id.btnSelectPhoto);



    btnSelectPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityForResult(galleryPhoto.openGalleryIntent(), GALLERY_REQUEST);
        }
    });



}

4 个答案:

答案 0 :(得分:0)

So according to your code:

 btnEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(getApplicationContext(), UpdateActivty.class);
            in.putExtra("id", "1");
            in.putExtra("name","abc" );
            startActivity(in);
        }
    });     

在UpdateActivty上:

Intent in1;
 in1= getIntent();

String id = in1.getStringExtra("id");
String name = in1.getStringExtra("name");

因此,在您的UpdateActivty上,您可以获得id - 1和name - abc。

请参阅 - intent doc

答案 1 :(得分:0)

在使用TextView的活动中,这将是意图,

Intent yourIntent = new Intent(this, TargetActivityClass.this);
yourIntent.putExtra("CONSTANT_STRING", yourActualString);
startActivity(yourIntent);

在使用EditText的活动中,您可以在活动中获取传递的数据onCreate,如下所示,

String myText = getIntent().getStringExtra("CONSTANT_STRING");

您现在可以将myText字符串放在EditText中。

答案 2 :(得分:0)

使用Java serialization/deserialization库将java对象转换为Json并通过intent传递Json String。您可以再次将Json字符串转换为Java对象。查看thisthis教程。

答案 3 :(得分:0)

您可以创建一个UI模型类,它将存储汽车信息并将整个模型类传递到下一个屏幕,而不是逐个传递数据