Android 如何在使用常量类存储数据时更新另一个活动中的EditText值
我需要一些指导 问题: 我想使用CartActivity中的EditText更新ProductInformationActivity中项目的数量 我的课程概述: 我在ProductInformationActivity中使用常量类来存储项目的信息,如:标题、数量、成本、总计,然后在活动中显示这些详细信息 查看这些屏幕截图:Android 如何在使用常量类存储数据时更新另一个活动中的EditText值,android,Android,我需要一些指导 问题: 我想使用CartActivity中的EditText更新ProductInformationActivity中项目的数量 我的课程概述: 我在ProductInformationActivity中使用常量类来存储项目的信息,如:标题、数量、成本、总计,然后在活动中显示这些详细信息 查看这些屏幕截图: ProductInformationActivity:我第一次接受用户的数量 CartActivity:我希望允许用户更新他在ProductInformationActivi
好的,首先在CartAdapter.java中将qty EditText修改为static,如下所示: 所以,修改这一行 最终编辑文本数量=(编辑文本)vi.findViewById(R.id.qty) 如下: 静态EditText数量=(EditText)vi.findViewById(R.id.qty) 现在在CartAdapter.java中创建一个公共方法,如下所示
public static String getQuantity()
{
if ( qty != null )
{
return qty.getText().toString();
}
return "";
}
现在进入ProductInformationActivity.java
在onResume()方法中,写入以下行(如果不存在,则创建一个方法)
您有一个包含项目的列表
Constants.sItem_Detail
它是静态的和公共的,因此可以从任何地方的所有类访问它
当您处于CartActivity时,ProductInformationActivity将暂停、停止或销毁。它不会显示,因此您无法更新它的视图元素,或者至少您不应该这样做
在CartActivity中,您有TextWatcher,在ContextChanged方法中,将Constants.sItem_Detail中的值更新为新值,在ProductInformationActivity中,您可以在onResume方法中的EditText中获取并设置它
HashMap<String, String> item = new HashMap<String, String>();
item = Constants.sItem_Detail.get(position);
// Setting all values in listview
title.setText(item.get(com.era.restaurant.versionoct.menu.ProductInformationActivity.KEY_TITLE));
qty.setText(item.get(com.era.restaurant.versionoct.menu.ProductInformationActivity.KEY_QTY));
cost.setText(item.get(com.era.restaurant.versionoct.menu.ProductInformationActivity.KEY_COST));
total.setText(item.get(com.era.restaurant.versionoct.menu.ProductInformationActivity.KEY_TOTAL));
qty.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!qty.getText().toString().equals("")
|| !qty.getText().toString().equals("")) {
// accept quantity by user
itemquantity = Integer.parseInt(qty.getText()
.toString());
//here You update value, You have item variable with current item
item.put(KEY_QTY, itemquantity);
//all object are always passed by reference so that is enough to update it
}
}
HashMap item=newhashmap();
item=常数。sItem_Detail.get(位置);
//在listview中设置所有值
title.setText(item.get(com.era.restaurant.versionect.menu.ProductInformationActivity.KEY_title));
qty.setText(item.get(com.era.restaurant.versionAct.menu.ProductInformationActivity.KEY_qty));
cost.setText(item.get(com.era.restaurant.versionect.menu.ProductInformationActivity.KEY_-cost));
setText(item.get(com.era.restaurant.versionect.menu.ProductInformationActivity.KEY_total));
数量addTextChangedListener(新TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
如果(!qty.getText().toString()等于(“”)
||!qty.getText().toString()等于(“”){
//按用户接受数量
itemquantity=Integer.parseInt(qty.getText()
.toString());
//在这里您更新值,您有一个带有当前项的项变量
物品放置(钥匙数量、物品数量);
//所有对象总是通过引用传递,这样就足够更新它了
}
}
在简历中
@Override
public void onResume()
{
super.onResume();
//get item to display
HashMap<String, String> item = new HashMap<String, String>();
item = Constants.sItem_Detail.get(id); //i don't know how You identify items, but here You need to get correct item from list.
edit_qty_code.setText(item.get(KEY_QTY));
}
@覆盖
恢复时公开作废()
{
super.onResume();
//获取要显示的项目
HashMap项=新建HashMap();
item=Constants.sItem_Detail.get(id);//我不知道如何标识项,但这里需要从列表中获取正确的项。
编辑_数量_代码.setText(item.get(KEY_数量));
}
1st-为什么不使用数据库?2nd-您想用旧值替换新值吗?听起来就是这样。有点让人困惑。是的,我想用旧值替换新值,但我接受CartActivity中的新值和ProductInformationActivity中的旧值,并且不需要使用数据库,这就是为什么我没有使用@gustkSo你想用9替换3,对吗,Pandey Ji?你有两个活动和一个类,其中一个是静态变量,你存储数据,对吗?你想同步两个活动中显示的数据?所以如果你在其中一个活动中进行更改,它将在另一个活动中更新?@Lucifer是的Lucifer你是正确的是的,给我看一些代码我怎么做从编程的角度看,我希望看到的更新是正确的。我不知道您如何决定在ProductInformationActivity中显示哪个项目,但我希望您了解它的工作原理。
@Override
public void onResume()
{
super.onResume();
//get item to display
HashMap<String, String> item = new HashMap<String, String>();
item = Constants.sItem_Detail.get(id); //i don't know how You identify items, but here You need to get correct item from list.
edit_qty_code.setText(item.get(KEY_QTY));
}