Issue
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. 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.
I've tried to test it with Toasts. You will see there is two Toast messages. 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. 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)
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:
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.
Solution
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"));
Answered By - a_local_nobody
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.