如何将 EditText 值从方法传递到同一 Activity 中 SQLiteOpenHelper

我有一个微调器,它选择分支值并将其存储在 onItemSelected 方法的 EditText 中。但我需要将编辑文本(分支)中显示的值预先分配给同一活动中的 SqliteDatabase 插入操作方法。抱歉,我无法适当地提出问题。

register.java

public class register extends AppCompatActivity implements AdapterView.OnItemSelectedListener {



    SQLiteOpenHelper openHelper;

    SQLiteDatabase db;

    Button _btnsignup;

    EditText _txtFname, _txtLname, _txtpass, _txtemail, _txtrollno;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.register);


        openHelper=new DatabaseHelper(this);


        _btnsignup= findViewById(R.id.btn_signup);

        _txtFname=findViewById(R.id.fname);

        _txtLname=findViewById(R.id.lname);

        _txtpass=findViewById(R.id.password);

        _txtemail=findViewById(R.id.email);

        _txtrollno=findViewById(R.id.rollno);

        _btnsignup.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                db=openHelper.getWritableDatabase();

                String fname=_txtFname.getText().toString();

                String Lname= _txtLname.getText().toString();

                String pwd= _txtpass.getText().toString();

                String email= _txtemail.getText().toString();

                String rollno= _txtrollno.getText().toString();


                insertdata(Fname, Lname, pwd,email,rollno);

                Toast.makeText(getApplicationContext(), "Registered Succesfully", Toast.LENGTH_LONG).show();

            }

        });



我需要将 Editext 值从branch.setText(text2);上面String branch= _txtbranch.getText().toString();定义的传递到public void onclick. 但它说不能


解析符号branch.getText().toString; 因为它是在另一个函数中声明的。


如果我在全局范围内声明它,则该getItemSlelected方法会显示错误并且应用程序崩溃。请帮忙


30秒到达战场
浏览 109回答 1
1回答

慕侠2389804

我建议进行以下更改:-将 spinner、adapter 和 _txtbranch 声明为类变量设置视图(spinner 和 _txtbranch)以及其他视图可以选择不覆盖 onItemSelected 或 onNothingSelected,而是在按钮的 onclick 中检索所选项目(因此不需要分支 EditText)。以下代码包含上述内容,但请参阅注释:-public class register extends AppCompatActivity implements AdapterView.OnItemSelectedListener {&nbsp; &nbsp; SQLiteOpenHelper openHelper;&nbsp; &nbsp; SQLiteDatabase db;&nbsp; &nbsp; Button _btnsignup;&nbsp; &nbsp; Spinner spinner; //<<<<<<<<<< DECLARED HERE&nbsp; &nbsp; ArrayAdapter<CharSequence> adapter; //<<<<<<<<<< DECLARED HERE&nbsp; &nbsp; EditText _txtFname, _txtLname, _txtpass, _txtemail, _txtrollno, _txtbranch /* <<<<<<<<<< CHANGED&nbsp; to add _txtbranch>>>>>>>> */;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.register);&nbsp; &nbsp; &nbsp; &nbsp; openHelper=new DatabaseHelper(this);&nbsp; &nbsp; &nbsp; &nbsp; _btnsignup= findViewById(R.id.btn_signup);&nbsp; &nbsp; &nbsp; &nbsp; _txtFname=findViewById(R.id.fname);&nbsp; &nbsp; &nbsp; &nbsp; _txtLname=findViewById(R.id.lname);&nbsp; &nbsp; &nbsp; &nbsp; _txtpass=findViewById(R.id.password);&nbsp; &nbsp; &nbsp; &nbsp; _txtemail=findViewById(R.id.email);&nbsp; &nbsp; &nbsp; &nbsp; _txtrollno=findViewById(R.id.rollno);&nbsp; &nbsp; &nbsp; &nbsp; _txtbranch=findViewById(R.id.branch); /* <<<<<<<<<< ADDED >>>>>>>>>> Alternately not needed if getting item directly from spinner*/&nbsp; &nbsp; &nbsp; &nbsp; spinner=findViewById(R.id.semester); /*<<<<<<<<<<< MOVED TO HERE spinner defined as a class variable */&nbsp; &nbsp; &nbsp; &nbsp; _btnsignup.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db=openHelper.getWritableDatabase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fname=_txtFname.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String Lname= _txtLname.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String pwd= _txtpass.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String email= _txtemail.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String rollno= _txtrollno.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String branch = _txtbranch.getText().toString(); // code wanted BUT not needed if value obtained directly from the spinner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //String branch = spinner.getSelectedItem().toString(); // ALTERNATIVE no need&nbsp; _txtbranch duplicating selected value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertdata(Fname, Lname, pwd,email,rollno);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "Registered Succesfully", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Toast.makeText(view.getContext(),"Registered Succesfully", Toast.LENGTH_LONG).show(); // Alternative to previous line gets the context from the view&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; adapter = ArrayAdapter.createFromResource(this, R.array.semester,android.R.layout.simple_spinner_item); //<<<<<<<<<< CHANGED&nbsp; &nbsp; &nbsp; &nbsp; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);&nbsp; &nbsp; &nbsp; &nbsp; spinner.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; spinner.setOnItemSelectedListener(this); //<<<<<<<<<< not really needed as selected item can be retrieved directly from spinner&nbsp; &nbsp; }&nbsp; &nbsp; public void insertdata( String Fname, String Lname, String pwd, String email, String rollno) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues contentValues = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(DatabaseHelper.COL_2, Fname);&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(DatabaseHelper.COL_3, Lname);&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(DatabaseHelper.COL_4, pwd);&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(DatabaseHelper.COL_5, email);&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(DatabaseHelper.COL_6, rollno);&nbsp; &nbsp; &nbsp; &nbsp; long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);&nbsp; &nbsp; }&nbsp; &nbsp; // Not needed if retrieving value directly from spinner&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {&nbsp; &nbsp; &nbsp; &nbsp; String text=adapterView.getItemAtPosition(i).toString();&nbsp; &nbsp; &nbsp; &nbsp; //branch= (EditText)findViewById(R.id.branch);&nbsp; &nbsp; &nbsp; &nbsp; String text2=adapterView.getSelectedItem().toString();&nbsp; &nbsp; &nbsp; &nbsp; _txtbranch.setText(text2);&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(adapterView.getContext(),text,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }&nbsp; &nbsp; // Not needed if retrieving value directly from spinner&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onNothingSelected(AdapterView<?> adapterView) {&nbsp; &nbsp; }&nbsp; &nbsp; public void open_login(View view){&nbsp; &nbsp; &nbsp; &nbsp; startActivity(new Intent(getApplicationContext(),login.class));&nbsp; &nbsp; }}请注意,以上是原则上的代码,尚未运行或测试,因此包含一些错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java