我想让用户按动作序列添加多个项目

I want to let user add multiple items by action sequence(我想让用户按动作序列添加多个项目)

本文介绍了我想让用户按动作序列添加多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,在该应用程序中,我使用 JSON 将数据提取到 ListView 中,并允许用户以另一种形式(即 singleMenuItem 形式)查看所选项目.

I am making an app in which I am fetching data using JSON into ListView, and allowing user to view selected item in another form, in singleMenuItem form.

我已经放置了一个按钮 - 添加到购物车.每当用户单击添加到购物车"按钮时,所选项目需要显示在查看购物车"表单中.对于添加到购物车的每件商品,都会重复相同的过程.

I have placed a button namely - Add to Cart. Whenever users will click on the Add to Cart button, the selected item needs to show in the View Cart form. The same process will be repeated for each item which is added to the cart.

我只想显示每个项目的标题和成本,对于那些我使用 ListView 表单中的 Intent 获取到 SingleMenuItem 表单的项目,现在我想在视图中显示购物车表单如:- 按操作顺序添加多个项目,问题是我不知道每次用户单击添加到购物车"按钮时如何更新查看购物车表单.

I just want to show each Item's Title and Cost, for those I am fetching into the SingleMenuItem form using Intent from ListView Form and now I want to show in View Cart Form like:- add multiple items by action sequence, the problem is I don't know how to update View Cart form every time whenever user click on Add to Cart Button.

目录(列表视图活动)代码:-

Catalogue(List View Activity) Code:-

public class Catalogue  extends Activity{

    static String URL = "http:/--------/and/menu.json";
    static String KEY_CATEGORY = "item";    
    static final String KEY_TITLE = "title";
    static final String KEY_DESCRIPTION = "description";
    static final String KEY_COST = "cost";
    static final String KEY_THUMB_URL = "imageUri";

ListView list;
LazyAdapter adapter;

/** Called when the activity is first created. */
@Override
 protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 
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);


Intent in = getIntent();

KEY_CATEGORY = in.getStringExtra("category");
    
                           try{

                 JSONArray jsonArray = json.getJSONArray(KEY_CATEGORY);
                 
                    for(int i=0;i < jsonArray.length();i++){                        

                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);                     
                        
                        map.put("id",  String.valueOf(i));
                        map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));
                        map.put(KEY_DESCRIPTION, jsonObject.getString(KEY_DESCRIPTION));
                        map.put(KEY_COST, jsonObject.getString(KEY_COST));
                        map.put(KEY_THUMB_URL, jsonObject.getString(KEY_THUMB_URL));
                    
                    
                    itemsList.add(map);
                    

                        
                  } 
                    return itemsList;
            }catch(JSONException e)        {
                 Log.e("log_tag", "Error parsing data "+e.toString());
            }
                           
                return null;
              
                }
                 
              
          @Override
          
          protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
                
                list=(ListView)findViewById(R.id.listView1);            
                adapter=new LazyAdapter(Catalogue.this, itemsList);        
                list.setAdapter(adapter);
                
                this.progressDialog.dismiss();
                list.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                          HashMap<String, String> map = itemsList.get(position);
                        
                        Intent in = new Intent(Catalogue.this, SingleMenuItem.class);
                        in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
                        in.putExtra(KEY_DESCRIPTION, map.get(KEY_DESCRIPTION));
                        in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL));
                        in.putExtra(KEY_COST, map.get(KEY_COST));
                        startActivity(in);
                    }

        
                });     
                
               ImageButton viewShoppingCart = (ImageButton) findViewById(R.id.ButtonViewCart);
               viewShoppingCart.setOnClickListener(new OnClickListener() {
                    
                    public void onClick(View v) {
                        Intent in = new Intent
                               (Catalogue.this, FinalOrder.class);
                        startActivity(in);
                        
                    }
                });

                

          } }}


SingleMenuItem 活动代码


SingleMenuItem Activity Code

   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);
    final 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);
    
  
 
    ImageButton addToCartButton = (ImageButton) findViewById(R.id.img_add);
    addToCartButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {                               
        Intent in = new Intent
               (SingleMenuItem.this, 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 活动代码:-


ViewCart Activity Code:-

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);    

}
}

推荐答案

要让 View Cart 更新,您需要集中存储数据.最好使用 SQLite 数据库.然后您可以在用户添加到购物车时更新此数据库,然后在查看购物车时,您可以从数据库中读取.

To get the View Cart to update, you need to store the data centrally. Best to use an SQLite database. Then you can update this database when a user adds to the cart, then when viewing the cart, you can read from the database.

这篇关于我想让用户按动作序列添加多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我想让用户按动作序列添加多个项目

基础教程推荐