将TextView从onCreate传递给OptionsItemSelected

时间:2016-01-22 11:51:06

标签: android-actionbar save

我正在尝试使用以下代码保存到数据库。 Logcat将值显示为空(客户端,电子邮件,地址,余额)。

似乎没有将TextView传递给OnOptionsItemSelected方法。知道我如何将TextViews从OnCreate方法传递给OnOptionsItemSelected

public class NewClient extends Activity {

    private ActionBar actionBar;
    //private DatabaseHandler dbHelper;
    static SimpleCursorAdapter dataAdapter;
    private TextView client_name;
    TextView client_code;
    TextView email;
    TextView addressline1;

    TextView addressline2;
    TextView balance;
    ClientsFragment client = new ClientsFragment();
    //String carried_code;
    //String carried_name;
    DatabaseHandler db;
    static Cursor cursor;
    private TextView ObtainAdd;
    private String ObtainAdd1;
    private String ObtainAdd2;
    public static TextView textViewC;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_client);

        db = new DatabaseHandler(this.getApplicationContext());
        actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        // dbHelper.open();

        // Get the layout inflater


        client_code = (TextView)findViewById(R.id.code_field);

        //set
        //get the Text-view for
        client_name = (TextView)findViewById(R.id.client_name_field);

        email = (TextView)findViewById(R.id.invoice_email_add_field);

        addressline1 = (TextView)findViewById(R.id.contact_info_field);

        balance = (TextView)findViewById(R.id.balance_field);

        //Generate ListView from SQLite Database
        // displayListView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.client_activity_actions, menu);
        final View menuItemView = findViewById(R.id.action_save);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_save:
                openSave();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void openSave() {

        String Updated_Name = client_name.getText().toString();
        // TextView prod_price = (TextView) newProd.findViewById(R.id.price_text);
        String Updated_code = client_code.getText().toString();
        String Updated_email = email.getText().toString();
        String Updated_add1 = addressline1.getText().toString();
        String Updated_add2 = "add2";// addressline2.getText().toString();
        String Updated_balance = balance.getText().toString();
        //get double value of code

        //updating products

        //  product.setID(position);
        client.setClientName(Updated_Name);
        client.setCode(Updated_code);
        client.setEmail(Updated_email);
        client.setAddressl1(Updated_add1);
        client.setAddressl2(Updated_add2);
        client.setBalance(Updated_balance);

        //update database
        // db.updateProduct(product);
        db.addClient(client);

        //  db.close();

        //  cursor.close();
        //cursor = db.getAllProducts();

        displayListView();

        //dataAdapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, columns, to, 0);
        //dataAdapter.swapCursor(NewClient.cursor);
        //startActivity(new_client_intent);
    }

    private void displayListView() {

         cursor = db.getAllClients(); 

    }
}

1 个答案:

答案 0 :(得分:0)

我通过从onCreate方法中移除textView的Inflater来修复它。现在它完美无缺: - )