1

我正在使用JSON將數據提取到listview中,我正在爲用戶單擊任何項​​目listview行提供工具,並且能夠查看選定的項目到另一個活動,仍然我只顯示用戶添加到cartcart活動中的單個項目,現在我想要顯示所有選擇的項目(s)在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); 
    } 
} 
+1

,你在'填數據itemsList'它是空的。這就是它通過例外的原因。 –

+0

你可以告訴我,在單項活動中,我使用itemlistview點擊獲取數據,現在我只想顯示這些數據,如: - 只有標題和費用的viewcart活動,所以我需要使用itemslist或不? – Stanley

+1

您想要將'SingleMenuItem'中收到的數據發送到'FinalOrder'活動?那麼就這樣做'in.putExtra(KEY_TITLE,getIntent()。getStringExtra(KEY_TITLE)); in.putExtra(KEY_COST,getIntent()。getStringExtra(KEY_COST));' –

回答

0

您可以使用共享偏好

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

,或者使用與靜態變量的一類,這樣就可以通過它

您可以使用setter方法來設置數據

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

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

而且從您需要的活動中您可以得到它干將

SomeClass.getTitle(); 
    SomeClass.getCost(); 
+0

都是不好的方法.... –

+0

@Moshin Bundles不適合他,你提出更好的解決方案 –

0

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

+0

getIntent()。getExtras();'returns an Intent'? –

+0

是的,這將返回與捆綁發送的一些值的意圖。 – URAndroid

+0

嗨,所有的觀衆和讀者,我已編輯我的問題和代碼請幫助 – Stanley

0
itemsList.get(position); 

這裏的問題。 itemsList的大小是沒有的,你試圖獲得項目索引0.所以android給你的indexoutofbound異常。

第一個 檢查itemsList的大小是否大於0,然後嘗試從itemsList中獲取該項目。

如: -

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

如果你要在兩個活動中使用數據,您可以通過Bundle實現它,如果數據的全球使用,你也可以用你的getter setter方法的文件擴展Application數據並登記在清單文件。

0

首先檢查像extrasnull參考:

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