为什么我的on活动参数为我提供了必需;

时间:2018-03-15 15:33:12

标签: java android android-intent

我现在正试图将数据从一个活动发送到另一个活动,我现在想知道为什么int requestCode,int resultCode,给我的变量从未使用过加上为什么它们会变红?它也给了我一个需要的; ?我正在接受本教程https://codelabs.developers.google.com/codelabs/android-room-with-a-view/index.html?index=..%2F..%2Findex#13的帮助,我无法看到他添加请求代码的位置?也许你们可以找到它......手头的帮助

package com.example.jenso.paperseller;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    CustomerDatabase database;
    FloatingActionButton fab;
    private CustomerViewModel mCustomerViewModel;
    final int NEW_CUSTOMER_ACTIVITY_REQUEST_CODE = 1;



    private static final  String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView recyclerView = findViewById(R.id.recycler);
        final PapperRecyclerAdapter adapter = new PapperRecyclerAdapter(this);


        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        mCustomerViewModel = ViewModelProviders.of(this).get(CustomerViewModel.class);

        mCustomerViewModel.getmAllCustomers().observe(this, new Observer<List<Customer>>() {
            @Override
            public void onChanged(@Nullable List<Customer> customers) {
                adapter.setCustomer(customers);
            }
        });
        public void onActivityResult(int requestCode; int resultCode; Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == NEW_CUSTOMER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
                Customer customer = new Customer(data.getStringExtra(CreateCustomer.EXTRA_REPLY), data.getStringExtra(CreateCustomer.SECOND_REPLY),data.getStringExtra(CreateCustomer.THIRD_REPLY),data.getStringExtra(CreateCustomer.FOURTH_REPLY));
                mCustomerViewModel.insert(customer);
            } else {
                Toast.makeText(
                        getApplicationContext(),
                        R.string.empty_not_saved,
                        Toast.LENGTH_LONG).show();
            }
        }

        recyclerView.setAdapter(adapter);

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: Do this when you click");
                Intent intent = new Intent(MainActivity.this, CreateCustomer.class);
                startActivityForResult(intent, NEW_CUSTOMER_ACTIVITY_REQUEST_CODE);


            }

        });
















    }







}

2 个答案:

答案 0 :(得分:1)

似乎你在onCreate方法中编写了onActivityResult方法,请在onCreate方法之外写。 希望这有效..!

答案 1 :(得分:0)

如果您想从下一个需要使用的活动中获得一些结果,这很简单startActivityForResult(intent,requestCode );

请求代码基本上是发件人的ID,例如,如果您使用相同的requestCode,您可能会有两个调用两个不同的活动来获取结果的按钮,您将无法理解活动正在向您发送数据。要区分两者,请使用不同的requestCodes。

现在在onActivityResult中,首先检查requestCode,然后检查ResultCode。

ResultCode 表示活动是否为您提供了正确的结果。