在第二个活动中调用方法

时间:2014-03-08 00:13:10

标签: android

我正在编写一个用户选择支票的提示应用,然后在第二个活动中显示小计。但是,我完全迷失了如何显示我的小计。我有一个getSubtotal()方法,但我不知道如何调用它。

第一项活动

public class TableListActivity extends Activity {


private ListView mListView;

private TableListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_table_list);

    // Find the ListView, create an adapter that reads our list of checks,
    // and connect the two
    mListView = (ListView)findViewById(R.id.listView);
    mAdapter = new TableListAdapter(this, DataStore.CHECKS);
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Intent intent = new Intent (TableListActivity.this, PayCheckActivity.class);
            intent.putExtra(PayCheckActivity.Extra_check, arg2);
            startActivity(intent);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.table_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_refresh) {
            // TODO do stuff here
            Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
}}

第二项活动

public class PayCheckActivity extends Activity{ 
String Thank;
Button Sign;
Button fifteen;
Button eighteen;
Button twenty;
String sample;

public static final String Extra_check= "abc";
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.paycheck);
  Sign = (Button)findViewById(R.id.Sign);
  fifteen= (Button)findViewById(R.id.fifteen);
  eighteen= (Button)findViewById(R.id.eighteen);
  twenty= (Button)findViewById(R.id.twenty);
  Sign.setOnClickListener(new OnClickListener()
  {
  public void onClick(View v)
  {
  Toast msg = Toast.makeText(getBaseContext(),"Thank You", Toast.LENGTH_LONG);
             msg.show();
  }});
  fifteen.setOnClickListener(new OnClickListener()
  {
  public void onClick(View v)
  {
  Toast msg = Toast.makeText(getBaseContext(),"Thank Yolllllu", Toast.LENGTH_LONG);
             msg.show();
  }});}}

Check.java

public class Check {
private long id;
private String tableName;
private ArrayList<MenuItem> mItems = new ArrayList<MenuItem>();
private boolean hasBeenSigned = false;

public static class MenuItem {

    public String name;
    public Amount cost;

    public MenuItem(String itemDescription, double cost) {
        this.name = itemDescription;
        this.cost = new Amount(cost);
    }
}

public Check(long id, String tableName) {
    this.id = id;
    this.tableName = tableName;
}

public long getId() {
    return id;
}

@Override
public String toString() {
    // The ArrayAdapter uses toString to get the text to display in the list item
    // We override toString here to display the table name
    return tableName;
}

public void addItem(String itemDescription, double cost) {
    mItems.add(new MenuItem(itemDescription, cost));

}

public String getTableName() {
    return tableName;
}

public Amount getSubtotal() {
    double total = 0;
    for (MenuItem item : mItems) {
        total += item.cost.getRawValue();
    }

    return new Amount(total);
}

public void markAsSigned() {
    hasBeenSigned = true;
}

public int getItemCount() {
    return mItems.size();
}

public MenuItem getMenuItemAt(int index) {
    return mItems.get(index);
}}

1 个答案:

答案 0 :(得分:0)

快速提示:您正通过Intent(onItemClick)向第二个活动发送一些数据。在onCreate中的第二个活动中,您可以选择此数据并调用getSubtotal方法。由于它不是很清楚,“检查”是什么,它取决于你如何实例化它:

public class PayCheckActivity extends Activity{ 
    // ...
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if(intent != null) {
            String value = intent.getStringExtra(PayCheckActivity.Extra_check);
            Check check = .....
            check.getSubtotal()
        }

    }
    // ...
}