如何将所有选定的项目显示到另一个活动中,例如购物车的viewcart活动

时间:2012-10-29 05:18:42

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

我使用JSON将数据提取到listview中,我正在为用户点击任何项目列表视图行提供工具,并且能够将所选项目查看到另一个活动中,我仍然只显示用户添加的单个项目要在viewcart活动中购物,现在我想在viewcart活动中显示所有选定的项目,例如:如在shoppingcart的viewcart活动表单中,这里我只是显示标题和成本,我通过使用listviewitem行获取到singleitemactivity点击

SingleItem代码: -

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

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

5 个答案:

答案 0 :(得分:0)

您可以使用共享偏好

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

或者使用带有静态变量的类,以便可以在

上传递它

您可以使用设置器设置数据

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

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

从您需要它的活动中,您可以使用getter获取它

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

答案 1 :(得分:0)

使用Intent in = getIntent().getextras();代替Intent in = getIntent();

答案 2 :(得分:0)

itemsList.get(position);

这是问题所在。 itemsList的大小为none,你试图获得0的项索引。所以android给你indexoutofbound异常。

第一 检查itemsList的大小是否大于0然后尝试从itemsList获取项目。

例如: -

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

首先检查extras null引用,如:

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