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

时间:2012-11-02 04:33:24

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

我正在制作一个应用程序,其中我使用JSON将数据提取到ListView中,并允许用户以另一种形式查看所选项目,在singleMenuItem表单中我已经放置了一个按钮 - 添加到购物车,这里我希望每当用户将点击该按钮,然后选择项目需要在查看购物车表格中显示,然后返回列表视图选择项目>在另一项活动中显示>再次单击“添加到购物车”按钮,将用户选择的所有项目显示在“查看购物车”表单中,我只想显示项目的标题和成本,我使用Intent从ListView表单获取到SingleMenuItem表单,现在我想在View中显示购物车形式如下: - 按动作顺序添加多个项目,问题是每当用户点击添加到购物车按钮时,我不知道如何更新查看购物车表单。

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

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

}
}

1 个答案:

答案 0 :(得分:0)

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