1

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

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

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

目录(列表查看活动)代码:-

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 活动代码

   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 活动代码:-

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

}
}
4

1 回答 1

0

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

于 2012-11-02T10:01:36.650 回答