[英]Cannot get a value from EditText
我有一個小問題,感謝 Alex Mamo 之前我已經解決了我的問題,現在我遇到了另一個問題。
這里附上我的 firebase 實時數據庫截圖
好吧,在我附加的 ima 上,te Cart 應該有一個子表號,我從 edittext sing getText function 獲得一個表號,然后我將它發送到另一個活動,但是,當我將它發送到另一個活動並獲取它時,似乎沒有任何價值。 我嘗試記錄,它沒有顯示任何內容,也嘗試打印,結果是一樣的。 我的問題是,如何以其他方式從 edittext 獲取值?
在這里我附上我的代碼 fo getText function
DineIn.java
EditText txtTable;
ArrayList<Menu> menuItems = new ArrayList<Menu>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dine_in);
txtTable = findViewById(R.id.txtTable);
String tables = txtTable.getText().toString();
Log.e("Nomormeja", tables);
這個鱈魚是為了把它送到另一個活動
DineIn.java(此代碼來自 onCreate() 方法)
class CustomAdaptor extends BaseAdapter {
private Activity mContext;
private ArrayList<Menu> mItems;
public CustomAdaptor(Activity context, ArrayList<Menu> list) {
mContext = context;
mItems = list;
}
@Override
public int getCount() {
return menuItems.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.order_items, null);
TextView menuName = (TextView) convertView.findViewById(R.id.menuName);
TextView menuType = (TextView) convertView.findViewById(R.id.menuType);
TextView menuPrice = (TextView) convertView.findViewById(R.id.menuPrice);
ImageButton addcart = (ImageButton) convertView.findViewById(R.id.addBtn);
final String menuNames = menuItems.get(position).getMenuName();
final String tables = txtTable.getText().toString();
addcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Like a Session variable ..it passes the dishName from the row that was clciked
SharedPreferences sharedPreferences=getSharedPreferences("shared preferences",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("menuNames",menuNames);
editor.putString("tables", tables);
editor.commit();
startActivity(new Intent(DineIn.this,ProductDetails.class));
}
});
這段代碼是為了獲取目標活動的價值
產品.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
txtNama = findViewById(R.id.tvNama);
txtQuantity = findViewById(R.id.tvQuantity);
btnMin = findViewById(R.id.btnMin);
btnPls = findViewById(R.id.btnPls);
btnAdd = findViewById(R.id.btnAdd);
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences sharedPreferences=getSharedPreferences("shared preferences",MODE_PRIVATE);
menuName = sharedPreferences.getString("menuNames", ""); // taking dishname from menu when clicked a
tables = sharedPreferences.getString("tables", "");
Log.d("meja", tables);
Log.d("menu", menuName);
我在我的購物車條目中為孩子設置了 vale
if(invalid==false){
writeNewEntry(tables, menuName, date, quantity);
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
}
private void writeNewEntry(String table, String name, String dates, int q){
Cart entry = new Cart(table, name, dates, q);
FirebaseDatabase.getInstance().getReference("Cart").child(tables).push().setValue(entry);
}
非常感謝您的每一次幫助
首先,您不應該通過 SharedPreferences 將內容傳遞給另一個活動。 嘗試通過 Intent 發送... 發件人:
Intent intent = new Intent(DineIn.this,ProductDetails.class);
intent.putExtra("tables", tables);
startActivity(intent)
接收者(活動案例):
Intent intent = getIntent();
tables = intent.getStringExtra("tables", "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.