简体   繁体   English

第二个活动无法从意图中读取 getExtras

[英]Second activity can't read getExtras from intent

I'm trying to get the values from a recyclerView item (Firestore as database) in a new activity by using intents if I click on it.如果我单击它,我正在尝试通过使用意图从新活动中的 recyclerView 项目(Firestore 作为数据库)中获取值。 Don't know if that is the best way, but I got so far.不知道这是否是最好的方法,但我到目前为止。

In my second activity I only get Null in the textViews I want to populate.在我的第二个活动中,我只在要填充的 textViews 中得到 Null。

I've tried to test it with Toasts.我试过用 Toasts 来测试它。 You will see there is two Toast messages.您将看到有两条 Toast 消息。 The first one in the mainActivity (UltraLiquors) show the data I want to pull correctly (the ID, product and price), but the second one in the details activity only shows "null", so I assume there is a problem with my getExtras in my detail activity. mainActivity (UltraLiquors) 中的第一个显示了我想要正确提取的数据(ID、产品和价格),但 details 活动中的第二个仅显示“null”,所以我认为我的 getExtras 有问题在我的详细活动中。 I just don't know where to look anymore.我只是不知道该往哪里看。

Please be so kind and help, I really have been struggling with this a few days.请如此友善和帮助,我真的已经为此苦苦挣扎了几天。

Here is my mainActivity (UltraLiquors.class)这是我的主要活动(UltraLiquors.class)

public class UltraLiquors extends AppCompatActivity {

private FirebaseFirestore ultra_liquor_db = FirebaseFirestore.getInstance();
private CollectionReference promo_oneRef = ultra_liquor_db.collection("ultra_liquors");
private static final int ACTIVITY_NUM = 2;

private UltraLiquorsRecyclerAdapter ultra_liquors_adapter;

private static final String TAG = "Ultra Liquors";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ultra_liquors);
    setUpPromoOneRecyclerView();
    //setupBottomNavigationView();
    setTitle("Ultra Liquors");
    Context context;
}

private void setUpPromoOneRecyclerView() {
    Query query = promo_oneRef.whereEqualTo("promo_number", "1").orderBy("department");

    FirestoreRecyclerOptions<Ultra_liquors> options = new FirestoreRecyclerOptions.Builder<Ultra_liquors>()
            .setQuery(query, Ultra_liquors.class)
            .build();

    ultra_liquors_adapter = new UltraLiquorsRecyclerAdapter(options);

    RecyclerView promo_one_recyclerView = findViewById(R.id.recycler_view_ultra_liquors);
    //recyclerView.setHasFixedSize(true);
    promo_one_recyclerView.setLayoutManager(new LinearLayoutManager(this));
    promo_one_recyclerView.setAdapter(ultra_liquors_adapter);


    ultra_liquors_adapter.setOnItemClickListener(new UltraLiquorsRecyclerAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
            Ultra_liquors note = documentSnapshot.toObject(Ultra_liquors.class);
            String id = documentSnapshot.getId();
            String product = (String) documentSnapshot.get("product");
            String price = (String) documentSnapshot.get("price");
            String path = documentSnapshot.getReference().getPath();
            Toast.makeText(UltraLiquors.this,
                   "Position: " + position + " ID: " + product + price, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(UltraLiquors.this, ViewProduct.class);
            intent.putExtra("Product", product);
            intent.putExtra("Price", price);
            startActivity(intent);
        }
    });

}

@Override
protected void onStart() {
    super.onStart();
    ultra_liquors_adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    ultra_liquors_adapter.stopListening();
}

} }

And here is the detailActivity (ViewProduct) where I want to pull the data from the intent:这是我想从意图中提取数据的detailActivity(ViewProduct):

public class ViewProduct extends AppCompatActivity {

private String edit_product_ID;
//private String edit_product_Product;
private String edit_product_Price;

private TextView productView, priceView;
private TextView mSave, mDelete;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_product);
Intent intent = getIntent();
String product;
String price;

product = String.valueOf(getIntent().getExtras().get("product"));
price = String.valueOf(getIntent().getExtras().get("price"));

    Toast.makeText(this, "The Product ID: " + edit_product_ID +
            " Product Name " + product + " Product Price " + price, Toast.LENGTH_LONG).show();

    TextView productView = findViewById(R.id.product);
    productView.setText(product);
    TextView priceView = findViewById(R.id.price);
    priceView.setText(price);
    mSave = (TextView) findViewById(R.id.save);
    mDelete = (TextView) findViewById(R.id.delete);

  }}

I am not the best yet with Java or Android, so please be patient.我在 Java 或 Android 方面还不是最好的,所以请耐心等待。 Thank you in advance.先感谢您。

You're using:您正在使用:

 intent.putExtra("Product", product);
 intent.putExtra("Price", price);

But then you're trying to get them with:但是你试图让他们:

product = String.valueOf(getIntent().getExtras().get("product"));
price = String.valueOf(getIntent().getExtras().get("price"));

See how these values are different?看看这些值有何不同?

You need to use the same casing, as it's case sensitive, so use something like:您需要使用相同的大小写,因为它区分大小写,因此请使用以下内容:

product = String.valueOf(getIntent().getExtras().get("Product"));
price = String.valueOf(getIntent().getExtras().get("Price"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM