如何更新EditText值在另一个活动,而使用常量类存储数据



我需要一些指导

问题:

我想在ProductInformationActivity中使用CartActivity中的EditText更新一个项目的数量

程序概述:

我在ProductInformationActivity中使用Constant类来存储Item的信息,例如:Title, Qty, Cost, Total,然后在CartActivity中显示这些详细信息。

查看这些屏幕截图:

  1. ProductInformationActivity:用户第一次接受数量

  2. CartActivity:我想让用户更新一个项目的数量,他已经在ProductInformationActivity[这里我使用EditText来接受新的数量]

ProductInformationActivity.java:

每当用户点击添加订单按钮,我存储项目标题,数量,成本和总在常量类,然后显示这些值在CartActivity

首先在CartAdapter.java中将qty EditText修改为static,如下所示

修改这一行

final EditText qty = (EditText) vi.findViewById(R.id.qty);

,

static EditText qty = (EditText) vi.findViewById(R.id.qty);

现在在CartAdapter.java中创建一个公共方法,如下所示,

public static String getQuantity()
{
     if ( qty != null )
     {
          return qty.getText().toString();
     }
     return "";
}

现在在ProductInformationActivity.java

在onResume()方法中,写如下行(如果不存在则创建一个方法),

@Override
public void onResume()
{
     super.onResume();
     edit_qty_code.setText ( CartAdapter.getQuantity() ); // This line will get the modified EditText's value from CartAdapter.java class file.
}

您有一个包含项目的列表

Constants.sItem_Detail

它是静态和公共的,所以它可以从任何地方的所有类访问。

当你在CartActivity, ProductInformationActivity将被暂停,停止或销毁。它是不显示的,所以你不能更新它的视图元素,或者至少你不应该这样做。

在CartActivity中你有TextWatcher,在onTextChanged方法中常量更新值。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
                        }      
                }
在onResume

@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));
}
    创建一个新的Intent。
  1. 将新数据设置为intent上的extra。
  2. 用新的Intent启动CartActivity
  3. 在CartActivity的onCreate中获取Intent的数据并更新UI。

最新更新