【问题标题】:how to show selected item(s) details into another activity using on button click如何使用单击按钮将所选项目的详细信息显示到另一个活动中
【发布时间】:2012-10-29 09:40:00
【问题描述】:

我正在使用 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);    

}

【问题讨论】:

    标签: android android-intent android-emulator


    【解决方案1】:

    在下面编写代码以从意图中获取数据,它将解决您的问题。

    Bundle bdl=getIntent().getExtras();
    String title1 = bdl.getString(KEY_TITLE);
    String cost1 = bdl.getString(KEY_COST);
    

    【讨论】:

    • Dipak 谢谢,我正在获取数据,但我想将所有选定项目的数据获取到另一个活动中,例如在购物车应用程序中,用户选择一个项目,然后查看更多,然后选择另一个并存储到查看订单页面,我需要在我的应用程序中创建相同的功能,以在用户单击查看订单按钮时​​显示所有选定的项目,我希望你能帮助我,通过编写一些代码我该怎么做?跨度>
    • @Stanley 然后先保存到一个数组列表中,然后在下一个活动中使用。
    • 我正在使用代码在该类中使用 json 将详细信息提取到列表视图中该活动的 ArrayList 代码,或者您可以为我编写所需的 ArrayList 代码,如您所愿,我很好
    • Dipak 我已经使用 ArrayList 发布了我的新 singleActivity 代码,请看那个
    • @Stanley 首先创建一个常量类并在这个类中声明2个静态数组列表,然后在下一个活动中使用这个数组列表。
    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多