このコードがボタンの背景画像を希望どおりに変更しない理由を誰かが知っていますか?
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(getApplicationContext(), "Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
price.setText(String.valueOf(Order.getInstance().getAmount()));
if(Order.getInstance().getOrderItems().size() > 0 && sent == false){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.send);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Review Order Screen", Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
sent = true;
startActivity(i);
}
});
}else if(sent == true){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.pay);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
//Intent i = new Intent(v.getContext(), PayActivity.class);
//startActivity(i);
}
});
}
}
ユーザーが初めてボタンを押した場合は、aBoolean
を true に設定し、新しいアクティビティを開始する必要があります。ユーザーがボタンを押して前のアクティビティに戻ると、新しいアクティビティ内から、Boolean
が true に設定されているため、画像を異なるものに設定する必要があります。
何らかの理由で機能していません...どんな助けも大歓迎です!
これは、ボタンが押されたときにユーザーを前のアクティビティに戻すアクティビティ 2 内のコードです。
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.confirm);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent k = new Intent(v.getContext(), SectionsActivity.class);
int tableNum = Order.getInstance().getTableNumber();
int coverNum = Order.getInstance().getCoverNumber();
itemNames = new ArrayList<String>();
//Get dishes from order
for(int i = 0; i < orderItems.size(); i++){
MenuItem item = orderItems.get(i);
String itemName = item.getName();
itemNames.add(i, itemName);
}
RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
ReviewOrderActivity.this.finish();
//startActivity(k);
}
});
コードへの編集:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(getApplicationContext(), "value: " + sent, Toast.LENGTH_SHORT).show();
price.setText(String.valueOf(Order.getInstance().getAmount()));
if(Order.getInstance().getOrderItems().size() > 0 && sent == false){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.send);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Review Order Screen " + sent, Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
startActivityForResult(i, 0);
}
});
}else if(sent == true){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.pay);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
//Intent i = new Intent(v.getContext(), PayActivity.class);
//startActivity(i);
}
});
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
sent = true;
}
if (resultCode == RESULT_CANCELED) {
sent = false;
}
}
}//onActivityResult
2 番目のアクティビティで
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_review);
//Get order items from application and assign to list
orderItems = Order.getInstance().getOrderItems();
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.confirm);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent k = new Intent(v.getContext(), SectionsActivity.class);
int tableNum = Order.getInstance().getTableNumber();
int coverNum = Order.getInstance().getCoverNumber();
itemNames = new ArrayList<String>();
//Get dishes from order
for(int i = 0; i < orderItems.size(); i++){
MenuItem item = orderItems.get(i);
String itemName = item.getName();
itemNames.add(i, itemName);
}
RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
setResult(RESULT_OK); //EDIT
ReviewOrderActivity.this.finish();
//startActivity(k);
}
});
//Create adapter and set to the list
adapter = new ReviewOrderArrayAdapter(this, orderItems);
this.setListAdapter(adapter);
}