1

JSONを使用してリストビューにデータをフェッチしています。アイテムリストビューの行のいずれかをクリックする機能をユーザーに提供しており、選択したアイテムを別のアクティビティに表示できますが、ユーザーがカートに追加した単一のアイテムのみを表示していますviewcart アクティビティ、ビューカート アクティビティで選択したすべてのアイテムを表示したい: ショッピングカートのビューカート アクティビティ フォームのように、ここではタイトルとコストを表示しています。

単品コード:-

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

最終注文コード:-

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

5 に答える 5

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

そして、必要なアクティビティからゲッターを使用して取得できます

    SomeClass.getTitle();
    SomeClass.getCost();
于 2012-10-29T05:21:56.313 に答える
0

Intent in = getIntent().getextras();代わりに使用Intent in = getIntent();

于 2012-10-29T05:23:17.997 に答える
0
itemsList.get(position);

ここに問題があります。のサイズitemsListはなしで、アイテム インデックス 0 を取得しようとしています。そのため、Android は indexoutofbound 例外を返します。

まず、itemsList のサイズが 0 より大きいかどうかを確認してから、itemsList.

例えば:-

if(itemsList != null && itemsList.size > 0)
{
  HashMap<String, String> map = itemsList.get(position);
}
于 2012-10-29T05:23:23.423 に答える
0

最初に参照extras用に次のように確認します。null

String extras = getIntent().getExtras();
String newString;
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString("MY_STRING");
}
于 2012-10-29T05:29:23.560 に答える
0

2 つのアクティビティ内でデータを使用している場合は、Bundleデータをグローバルに使用する場合は、getter セッター ファイルを使用してApplicationデータを拡張し、マニフェスト ファイルに登録することもできます。

于 2012-10-29T05:25:39.567 に答える