首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >第二个活动无法从意图读取getExtras

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

Stack Overflow用户
提问于 2021-02-07 23:30:43
回答 1查看 41关注 0票数 1

我正在尝试从新活动中的recyclerView项(Firestore作为数据库)中获取值,如果我单击它,则使用intents。不知道这是不是最好的方法,但我到目前为止。

在我的第二个活动中,我只在要填充的textViews中获取Null。

我试着用Toast来测试它。您将看到有两条Toast消息。mainActivity中的第一个参数(UltraLiquors)显示了我想要正确拉取的数据( ID、产品和价格),但details活动中的第二个参数仅显示"null",因此我假设我的detail活动中的getExtras有问题。我只是不知道该去哪里找了。

请多关照和帮助,我已经为这件事挣扎了好几天了。

这是我的mainActivity (UltraLiquors.class)

代码语言:javascript
复制
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();
}

}

下面是我想要从intent中提取数据的detailActivity (ViewProduct):

代码语言:javascript
复制
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);

  }}

我对Java或Android还不是最好的,所以请耐心等待。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-07 23:44:36

您正在使用:

代码语言:javascript
复制
 intent.putExtra("Product", product);
 intent.putExtra("Price", price);

但之后你会尝试用以下命令来获取它们:

代码语言:javascript
复制
product = String.valueOf(getIntent().getExtras().get("product"));
price = String.valueOf(getIntent().getExtras().get("price"));

看到这些值有什么不同了吗?

您需要使用相同的大小写,因为它区分大小写,因此使用类似于:

代码语言:javascript
复制
product = String.valueOf(getIntent().getExtras().get("Product"));
price = String.valueOf(getIntent().getExtras().get("Price"));
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66089643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档