简体   繁体   中英

how to show all selected item(s) into another activity like in shopping cart's viewcart activity

I am fetching data into listview using JSON, i am providing facility to user click on any of the item listview row, and will be able to view that selected item into another activity, still i am showing only single item added by user to cart in viewcart activity, now i want to show all the selected item(s) in viewcart activity, like: as in shoppingcart's viewcart activity form, here i am just showing Title and Cost, which i am fetching into singleitemactivity by using listviewitem row click

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

    public void onClick(View v) {    

        Intent in = new Intent
          (SingleMenuItem.this, com.erachnida.restaurant.versionoct.FinalOrder.class);
        in.putExtra(KEY_TITLE, getIntent().getStringExtra(KEY_TITLE)); 
        in.putExtra(KEY_COST, getIntent().getStringExtra(KEY_COST));
        startActivity(in);

            // Close the activity
            //  finish();
        }
    });    
}}

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

You can use shared preferences

SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString(KEY_TITLE , "Title");
    prefsEditor.putString(KEY_COST , "cost");
    prefsEditor.commit(); 

OR use a class with static variables so that you can pass it on

You can use setters to set the data

SomeClass.setTitle(String title){
KEY_TITLE = title;
}

SomeClass.setCost(String cost){
KEY_COST = cost;
}

And from the activity where you need it you can get it using getters

    SomeClass.getTitle();
    SomeClass.getCost();

Use Intent in = getIntent().getextras(); instead Intent in = getIntent();

itemsList.get(position);

here is the problem. the size of itemsList is none and you are trying to get the item index of 0. So the android gives you the indexoutofbound exception.

first check whether the size of itemsList is greater than 0 then try to get the item from the itemsList .

eg:-

if(itemsList != null && itemsList.size > 0)
{
  HashMap<String, String> map = itemsList.get(position);
}

如果要在两个活动中使用数据,则可以通过Bundle来实现,如果可以全局使用数据,则还可以使用getter setter文件扩展Application数据并在清单文件中注册。

First check the extras for null reference like:

String extras = getIntent().getExtras();
String newString;
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString("MY_STRING");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM