![](/img/trans.png)
[英]how to show selected item(s) details (Title & Cost) into another activity , like:- Shopping Cart Functinality (Add to Cart & View Cart)
[英]how to show selected item(s) details into another activity using on button click
我正在使用json解析器將數據提取到listview中,一旦用戶單擊任何listview項目行,然后在整個頁面中顯示所選項目以及“添加到購物車”按鈕,在這里我希望每當用戶單擊該按鈕時顯示項目詳細信息(僅名稱和費用)以查看訂單表單,並允許用戶選擇更多商品,例如:-購物車功能,最后允許用戶在ViewCart活動中查看其選擇的訂單商品,這里我放置了SingleItem和FinalOrder代碼:-
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;
ListView list;
LazyAdapter adapter;
@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));
}
});
final ArrayList<HashMap<String, String>> itemsList =
new ArrayList<HashMap<String, String>>();
list=(ListView)findViewById(R.id.listView1);
adapter=new LazyAdapter(this, itemsList);
list.setAdapter(adapter);
ImageButton addToCartButton = (ImageButton) findViewById(R.id.img_add);
addToCartButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int position = 0;
HashMap<String, String> map = itemsList.get(position);
Intent in = new Intent
(SingleMenuItem.this, com.erachnida.restaurant.versionoct.FinalOrder.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_COST, map.get(KEY_COST));
startActivity(in);
}
});
// Close the activity
// finish();
}}
查看購物車:-
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);
}
在下面的代碼中編寫以從意圖中獲取數據,它將解決您的問題。
Bundle bdl=getIntent().getExtras();
String title1 = bdl.getString(KEY_TITLE);
String cost1 = bdl.getString(KEY_COST);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.