如何将所选项目的详细信息(标题和成本)显示到另一个活动中,例如: - 购物车功能(添加到购物车和查看购物车)

时间:2012-10-29 08:16:11

标签: android android-intent android-emulator android-listview

我正在制作一个应用程序,其中我使用json解析器将数据提取到listview中,然后将所选列表视图项目行显示到具有项目详细信息的另一个活动(注意: - 我从ListView活动获取到singleItem活动的详细信息)现在我需要通过使用singleItem活动上的“添加到购物车”按钮将所有选定项目的项目标题和成本显示到另一个活动ie-Vi​​ewCart(从singleItem Activity到ViewCart Activity),以将所有选定记录发送到ViewCart活动,如: - 购物车功能添加到购物车然后将整个选定的记录查看到另一个活动ViewCart,我只是想显示所选项目的标题和成本到另一个活动,仍然我只显示一个选定的项目而不是所有选定的项目由用户在会话,我也没有保存完整会话的记录,但现在我想根据完整的会话,按用户将所有选定的项目显示到viewcart活动。 下面我将放置singleItem和ViewCart代码: -

SingleItem代码: -

    public class SingleMenuItem extends Activity{
static final String KEY_TITLE = "title";
static final String KEY_COST = "cost";
static final String KEY_THUMB_URL = "imageUri";
private EditText edit_qty_code;
private TextView txt_total;
private TextView text_cost_code;
private double itemamount = 0;
private double itemquantity = 0;

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single);

    Intent in = getIntent();
    String title = in.getStringExtra(KEY_TITLE);
    String thumb_url = in.getStringExtra(KEY_THUMB_URL);
    String cost = in.getStringExtra(KEY_COST);

    ImageLoader imageLoader = new ImageLoader(getApplicationContext());

    ImageView imgv = (ImageView) findViewById(R.id.single_thumb);
    TextView txttitle = (TextView) findViewById(R.id.single_title);
    TextView txtcost = (TextView) findViewById(R.id.single_cost);
    TextView txtheader = (TextView) findViewById(R.id.actionbar);

    txttitle.setText(title);
    txtheader.setText(title);
    txtcost.setText(cost);
    imageLoader.DisplayImage(thumb_url, imgv);

    text_cost_code = (TextView)findViewById(R.id.single_cost);
    edit_qty_code = (EditText)findViewById(R.id.single_qty);
    txt_total=(TextView)findViewById(R.id.single_total);

    itemamount=Double.parseDouble(text_cost_code.getText().toString());
    txt_total.setText(Double.toString(itemamount));

    edit_qty_code.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {


    // TODO Auto-generated method stub
    }
    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {

    // TODO Auto-generated method stub
    }
    public void afterTextChanged(Editable s) {
         itemquantity=Double.parseDouble(edit_qty_code.getText().toString());
         itemamount=Double.parseDouble(text_cost_code.getText().toString());
         txt_total.setText(Double.toString(itemquantity*itemamount));
    }
    });             

    ImageButton addToCartButton = (ImageButton) findViewById(R.id.img_add);
    addToCartButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {    

        Intent in = new Intent
           (SingleMenuItem.this, com.erachnida.restaurant.versionoct.FinalOrder.class);
        in.putExtra(KEY_TITLE, getIntent().getStringExtra(KEY_TITLE)); 
        in.putExtra(KEY_COST, getIntent().getStringExtra(KEY_COST));
        startActivity(in);

            // Close the activity
            //  finish();
        }
    });    
}}

ViewCart代码: -

public class FinalOrder extends Activity
{
static final String KEY_TITLE = "title";
static final String KEY_COST = "cost";
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);     


    Intent in = getIntent();

    String title1 = in.getStringExtra(KEY_TITLE);
    String cost1 = in.getStringExtra(KEY_COST);

    TextView txttitle1 = (TextView) findViewById(R.id.item_name);
    TextView txtcost1 = (TextView) findViewById(R.id.item_cost);

    txttitle1.setText(title1);
    txtcost1.setText(cost1);    

}
}

1 个答案:

答案 0 :(得分:0)

使用以下代码删除您的onclick代码。

 public void onClick(View v) {    

    Intent in = new Intent
       (SingleMenuItem.this, com.erachnida.restaurant.versionoct.FinalOrder.class);
    in.putExtra(KEY_TITLE, title); 
    in.putExtra(KEY_COST, cost);
    startActivity(in);

        // Close the activity
        //  finish();
    }