代码之家  ›  专栏  ›  技术社区  ›  ttattini

从sqlite数据库自动填充android studio中的edittext

  •  -1
  • ttattini  · 技术社区  · 6 年前

    我正试图在应用程序中自动完成edittext,但不是通过键入,而是从一个条目到另一个条目。我的意思是,当用户输入“student id”时,“first name”和“last name”字段将自动填充sqlite数据库中的相应数据(如果存在)。

    我当前的XML文件如下:

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#d5ebfd"
        android:orientation="vertical">
    
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="Student Grade Management System"
            android:textSize="20dp"
            android:textColor="#ff0000"
            android:gravity="center"
            android:fontFamily="serif"
            android:textStyle="bold"/>
    
        <EditText
            android:id="@+id/studentID"
            android:layout_width="match_parent"
            android:paddingTop="20dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:hint="Enter Student ID (Used for Updating/Deleting): " />
        <requestFocus />
    
        <EditText
            android:id="@+id/firstName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter First Name: " />
    
        <EditText
            android:id="@+id/lastName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Last Name: " />
    
        <EditText
            android:id="@+id/classID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:hint="Enter Class ID: " />
    
        <EditText
            android:id="@+id/className"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Class Name: " />
    
        <EditText
            android:id="@+id/gradePoint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:hint="Enter Grade Point (0-100): " />
    
        <EditText
            android:id="@+id/letterGrade"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Letter Grade: " />
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >
    
            <Button
                android:id="@+id/addbtn"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="Add" />
    
            <Button
                android:id="@+id/deletebtn"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="Delete" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >
    
            <Button
                android:id="@+id/viewall"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="View" />
    
            <Button
                android:id="@+id/modifystudent"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="Update Student Info" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" >
    
            <Button
                android:id="@+id/modifyclass"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="Update Class Info" />
    
            <Button
                android:id="@+id/modifygrade"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="Update Grade Info" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.74" >
    
        </LinearLayout>
    
    </LinearLayout>
    

    主要活动如下:

    public class MainActivity extends Activity {
        EditText fname,lname,classid,classname,pointgrade,lettergrade,studentid;
        Button add,view,viewall,delete,modifystudent, modifyclass, modifygrade;
        SQLiteDatabase db;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            studentid=(EditText)findViewById(R.id.studentID);
            fname=(EditText)findViewById(R.id.firstName);
            lname=(EditText)findViewById(R.id.lastName);
            classid=(EditText)findViewById(R.id.classID);
            classname=(EditText)findViewById(R.id.className);
            pointgrade=(EditText)findViewById(R.id.gradePoint);
            lettergrade=(EditText)findViewById(R.id.letterGrade);
            add=(Button)findViewById(R.id.addbtn);
            viewall=(Button)findViewById(R.id.viewall);
            delete=(Button)findViewById(R.id.deletebtn);
            modifystudent=(Button)findViewById(R.id.modifystudent);
            modifyclass=(Button)findViewById(R.id.modifyclass);
            modifygrade=(Button)findViewById(R.id.modifygrade);
    
            db=openOrCreateDatabase("STUDENTGRADES", Context.MODE_PRIVATE, null);
            db.execSQL("CREATE TABLE IF NOT EXISTS CUSTOMER_TABLE(studentid VARCHAR, fname VARCHAR, lname VARCHAR, classid VARCHAR, classname VARCHAR, pointgrade INTEGER, lettergrade VARCHAR);");
    
    
            add.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(fname.getText().toString().trim().length()==0||
                            lname.getText().toString().trim().length()==0 || studentid.getText().toString().trim().length()==0)
                    {
                        showMessage("Error", "Please enter First & Last Name and Student ID");
                        return;
                    }
                    db.execSQL("INSERT INTO CUSTOMER_TABLE VALUES('"+studentid.getText()+"','"+fname.getText()+"','"+lname.getText()+"','"+classid.getText()+"','"+classname.getText()+
                            "','"+pointgrade.getText()+"','"+lettergrade.getText()+"');");
                    showMessage("Success", "Record added successfully");
                    clearText();
                }
            });
            delete.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0)
                    {
                        showMessage("Error", "Please enter Student and Class ID ");
                        return;
                    }
                    Cursor c=db.rawQuery("SELECT * FROM CUSTOMER_TABLE WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'", null);
                    if(c.moveToFirst())
                    {
                        db.execSQL("DELETE FROM CUSTOMER_TABLE WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'");
                        showMessage("Success", "Record Deleted");
                    }
                    else
                    {
                        showMessage("Error", "Invalid First and Last Name or Student ID");
                    }
                    clearText();
                }
            });
            modifystudent.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(fname.getText().toString().trim().length()==0 || lname.getText().toString().trim().length()==0 || studentid.getText().toString().trim().length()==0)
                    {
                        showMessage("Error", "Please enter Student First & Last Name and ID to update student info. \n\nAll other Field Entries will be ignored");
                        return;
                    }
                    Cursor c=db.rawQuery("SELECT * FROM CUSTOMER_TABLE WHERE studentid='"+studentid.getText()+"'", null);
                    if(c.moveToFirst())
                    {
                        db.execSQL("UPDATE CUSTOMER_TABLE SET fname='"+fname.getText()+"',lname='"+lname.getText()+"' WHERE studentid='"+studentid.getText()+"'");
                        showMessage("Success", "Name Record Modified");
                    }
                    else
                    {
                        showMessage("Error", "Invalid First and Last name or Student ID");
                    }
                    clearText();
                }
            });
    
            modifyclass.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0 || classname.getText().toString().trim().length()==0)
                    {
                        showMessage("Error", "Please enter Student & Class ID to update class. \n\nAll other Field Entries will be ignored");
                        return;
                    }
                    Cursor c=db.rawQuery("SELECT * FROM CUSTOMER_TABLE WHERE classid='"+classid.getText()+"'", null);
                    if(c.moveToFirst())
                    {
                        db.execSQL("UPDATE CUSTOMER_TABLE SET classid='"+classid.getText()+"',classname='"+classname.getText()+"' WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'");
                        showMessage("Success", "Class Record Modified");
                    }
                    else
                    {
                        showMessage("Error", "Invalid First and Last name or Class ID");
                    }
                    clearText();
                }
            });
    
            modifygrade.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0 || classname.getText().toString().trim().length()==0)
                    {
                        showMessage("Error", "Please enter Student & Class ID to update grades. \n\nAll other Field Entries will be ignored");
                        return;
                    }
                    Cursor c=db.rawQuery("SELECT * FROM CUSTOMER_TABLE WHERE classid='"+classid.getText()+"' AND studentid='"+studentid.getText()+"'", null);
                    if(c.moveToFirst())
                    {
                        db.execSQL("UPDATE CUSTOMER_TABLE SET pointgrade='"+pointgrade.getText()+"',lettergrade='"+lettergrade.getText()+"' WHERE classid='"+classid.getText()+"' AND studentid='"+studentid.getText()+"'");
                        showMessage("Success", "Grade Record Modified");
                    }
                    else
                    {
                        showMessage("Error", "Invalid First and Last name or Class ID");
                    }
                    clearText();
                }
            });
    
            viewall.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Cursor c=db.rawQuery("SELECT * FROM CUSTOMER_TABLE", null);
                    if(c.getCount()==0)
                    {
                        showMessage("Error", "No records found");
                        return;
                    }
                    StringBuffer buffer=new StringBuffer();
                    while(c.moveToNext())
                    {
                        buffer.append("ID: "+c.getString(0)+"\n");
                        buffer.append("Student Name: "+c.getString(1)+" " +c.getString(2)+"\n");
                        buffer.append("Class ID: "+c.getString(3)+"\n");
                        buffer.append("Class Name: "+c.getString(4)+"\n");
                        buffer.append("Points: "+c.getString(5)+"\n");
                        buffer.append("Grade: "+c.getString(6)+"\n\n");
                    }
                    showMessage("Student Report", buffer.toString());
                }
            });
    
    
        }
        public void showMessage(String title,String message)
        {
            Builder builder=new Builder(this);
            builder.setCancelable(true);
            builder.setTitle(title);
            builder.setMessage(message);
            builder.show();
        }
        public void clearText()
        {
            studentid.setText("");
            fname.setText("");
            lname.setText("");
            classid.setText("");
            classname.setText("");
            pointgrade.setText("");
            lettergrade.setText("");
            studentid.requestFocus();
    
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

    非常感谢您的直接帮助,谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   ttattini    6 年前

    这可以通过文本更改侦听器/文本观察程序完成。对于上述请求,可以按如下方式实现,并将使用来自sqlite数据库的数据自动填充

    studentid.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    Cursor c = db.rawQuery("SELECT * FROM " + STUDENT_TABLE + " WHERE studentid='" + s.toString() + "'", null);
                    if (c.moveToFirst()) {
                        fname.setText(c.getString(1));
                        lname.setText(c.getString(2));
                    } else {
                        fname.setText("");
                        lname.setText("");
                    }
                    c.close();
                }
            });