首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Studio中的AutoPopulate EditText来自SQLite数据库

Android Studio中的AutoPopulate EditText来自SQLite数据库
EN

Stack Overflow用户
提问于 2018-05-30 00:32:32
回答 1查看 216关注 0票数 -1

我正在尝试在我的应用程序中自动完成EditText,但是不是通过键入的方式,而是从一个条目到另一个条目。我的意思是,当用户输入"Student ID“时,"First Name”和"Last Name“字段将自动填充来自SQLite数据库的相应数据(如果存在)。

我当前的XML文件如下:

代码语言:javascript
复制
<?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>

主要活动如下所示:

代码语言:javascript
复制
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;
    }

}

非常感谢任何直接的帮助,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 07:26:35

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

代码语言:javascript
复制
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();
            }
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50589046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档