I want to let user add multiple items by action se

2020-02-02 03:06发布

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, here i want whenever user will click on that button, then selected item need to show in View Cart form, then go back to Listview Select an item > show in another activity > again click on Add to Cart button and show all the items selected by user into View Cart Form, i just want to show Item's Title and Cost, those i am fetching into 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 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 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);    

}
}

1条回答
够拽才男人
2楼-- · 2020-02-02 03:12

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.

查看更多
登录 后发表回答