gpt4 book ai didi

android - 如何在 Android 中使用 Tab 时跨 Activity 存储信息

转载 作者:行者123 更新时间:2023-11-29 01:57:06 27 4
gpt4 key购买 nike

我制作了一个使用两个选项卡的应用程序,

第一个标签,显示产品列表[菜单标签]

第二个标签,显示您添加的产品[购物车标签]

在这里,我允许用户将产品添加到购物车更新产品数量并显示所有项目的总金额。 p>

在我的代码中,我遇到了非常小的问题,但过去两天我没有找到任何方法来解决这个问题。

问题:

每当我点击购物车选项卡查看我选择购买的产品,然后再次返回菜单选项卡选择更多要购买的产品,然后再次点击购物车选项卡查看所有产品我已经选择,在这里我可以查看所选产品但无法查看更新数量并且总金额没有任何变化[仅获取所有先前添加的项目的总计,而不是我第二次添加的项目...]

请告诉我我遗漏的地方、获取更新的数量和总价的变化、我需要添加什么代码以及我需要在我的 ViewCartActivity 类中添加的位置

ViewCartActivity.Java:

    public class ViewCartActivity extends Activity {

TextView mTxtViewGrandTotal;
String mGrandTotal ;


@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcartactivity);

ListView mLstView1 = (ListView) findViewById(R.id.listView1);
mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);

ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(ViewCartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);

if (Constants.mItem_Detail.size() > 0)
{
Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++) {
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}

mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);



ImageButton mImgViewCart = (ImageButton) findViewById(R.id.back_btn);
mImgViewCart.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Intent mViewCartIntent = new Intent(ViewCartActivity.this, com.version.bajrang.january.menu.ArrowsActivity.class);
startActivity(mViewCartIntent);


}
});
}
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("onResume list current size "+Constants.mItem_Detail.size());
super.onResume();
}
}

在 Logcat 中,我得到:

onResume list current size 3 [Number of Records]

TabActivity.Java:

    private void setTabs()
{
addTab("Order", R.drawable.tab_order, CategoryActivity.class);
addTab("Cart", R.drawable.tab_cart, ViewCartActivity.class);
}

@Override
public void onBackPressed() {
// Code to update product Quantity
super.onBackPressed();
Constants.mItem_Detail.clear();
}

ViewCartAdapter.Java:

 public class ViewCartAdapter extends BaseAdapter {

Activity activity;
LayoutInflater inflater;

public ViewCartAdapter(Activity a) {
// TODO Auto-generated constructor stub
activity = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return Constants.mItem_Detail.size();
}
public Object getItem(int position) {
//return position;
return Constants.mItem_Detail.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;

}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.viewcartlist, null);
TextView title = (TextView) vi.findViewById(R.id.title);
TextView qty = (TextView) vi.findViewById(R.id.qty);
TextView cost = (TextView) vi.findViewById(R.id.cost);

HashMap<String, String> item = new HashMap<String, String>();
item = Constants.mItem_Detail.get(position);
// Setting all values in listview
title.setText(item.get(SingleProductActivity.TAG_NAME));
qty.setText(item.get(SingleProductActivity.KEY_QTY));
cost.setText(item.get(SingleProductActivity.TAG_DURATION));
return vi;
}
}

最佳答案

Rakesh 你已经在你的程序中编写了所有需要的东西,只需要在 ViewCartActivity 中从 onCreate() 方法移动到 onResume() 方法

像这样:

 public class ViewCartActivity extends Activity 
{

TextView mTxtViewGrandTotal;
String mGrandTotal ;


@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcartactivity);

ListView mLstView1 = (ListView) findViewById(R.id.listView1);
mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);

ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(ViewCartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);

ImageButton mImgViewCart = (ImageButton) findViewById(R.id.back_btn);
mImgViewCart.setOnClickListener(new OnClickListener()
{

public void onClick(View v) {
// TODO Auto-generated method stub

Intent mViewCartIntent = new Intent(ViewCartActivity.this, com.version.bajrang.january.menu.ArrowsActivity.class);
startActivity(mViewCartIntent);

}
});
}


@Override
protected void onResume()
{
super.onResume();
System.out.println("onResume list current size " + Constants.mItem_Detail.size());

if (Constants.mItem_Detail.size() == 0)
{
return;
}

Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++)
{
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}
mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);
}
}

关于android - 如何在 Android 中使用 Tab 时跨 Activity 存储信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com