简体   繁体   English

如何通过单击“搜索”按钮从android数据库中检索记录?

[英]How to retrieve a record from android database by click on Search button?

I have started coding for Finder app which contains employee records and user will able to find employee record by using 'id' when click on Search button in useractivity.java and data should display in Empdetails.java activity in textview contains 3 textfields to display. 我已经开始为包含员工记录的Finder应用进行编码,并且在useractivity.java中单击“搜索”按钮时,用户将能够通过使用“ id”来查找员工记录,并且数据应显示在Empdetails中。textview中的java活动包含要显示的3个文本字段。 Below is my code , Please help me in this 下面是我的代码,请帮助我

My requirement : I want to retrieve a single record by giving an id in UserActivity.java and when i click on Search button it should get a record details in Empdetails.java in text view 我的要求:我想通过在UserActivity.java中提供一个ID来检索一条记录,当我单击“搜索”按钮时,它应该在文本视图的Empdetails.java中获得一条记录详细信息

Output: 输出:

id: 12345 id:12345

name : vijay kumar 名字:vijay kumar

Email: vijay@gmail.com 电子邮件:vijay@gmail.com

like above shown 如上图所示

Thanks inadvance Vijay Kumar. 感谢Vijay Kumar的前瞻性。

1) AdminActivity.java 1)AdminActivity.java

public class AdminActivity extends Activity {

String id,name,email;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    db=openOrCreateDatabase("EMP",MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS EMP(id varchar,name varchar,email varchar);");
}

public void Submit(View view)
{       
EditText edittext1=(EditText) findViewById(R.id.id);
    EditText edittext2=(EditText) findViewById(R.id.name);
    EditText edittext3=(EditText) findViewById(R.id.email);
    id=edittext1.getText().toString();
    name=edittext2.getText().toString();
    email=edittext3.getText().toString();
    db.execSQL("INSERT INTO EMP(id,name,email) values ('"+id+"','"+name+"','"+email+"');");

    if(id.equals("") && name.equals("") && email.equals(""))
    {
        Toast.makeText(getApplicationContext(), "Please enter details and submit", Toast.LENGTH_SHORT).show();
    }
    else 
        if(id.equals(""))
       {
            Toast toast=new Toast (getApplicationContext());
            toast.setDuration(Toast.LENGTH_LONG);
            Toast.makeText(getApplicationContext(), "Enter id....", Toast.LENGTH_SHORT).show();
       }
    else
        if(name.equals(""))
           {
            Toast toast=new Toast (getApplicationContext());
            toast.setDuration(Toast.LENGTH_LONG);
            Toast.makeText(getApplicationContext(), "Enter Name....", Toast.LENGTH_SHORT).show();
           }
        else
             if(email.equals(""))
            {
            Toast toast=new Toast (getApplicationContext());
            toast.setDuration(Toast.LENGTH_LONG);
             Toast.makeText(getApplicationContext(), "Enter email.....", Toast.LENGTH_SHORT).show();
            }
             else 
                { 
                    Toast toast=new Toast (getApplicationContext());
                    toast.setDuration(Toast.LENGTH_LONG);
                    edittext1.setText("");
                    edittext2.setText("");
                    edittext3.setText("");
                    Toast.makeText(getApplicationContext(), "Data submitted successfully", Toast.LENGTH_SHORT).show();
                    db.close();
                 }
            }

public void Exit(View view){
    System.exit(0);
}

}

2) UserAvtivity.java 2)UserAvtivity.java

public class UserActivity extends Activity {


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

    public void Search(View view)
    {
    /////???????????????
    }

    public void Exit(View view)
    {
    System.exit(0);
    }
}

Place an editText on UserActivity class for id search. 在UserActivity类上放置一个editText以便进行ID搜索。 and on onClickListener of a Button using sql query retrieve data from database (SELECT * From tableName where id = (Here put id no where user is going to search for))and display it in a listview. 并使用sql查询在Button的onClickListener上从数据库中检索数据(SELECT * From tableName,其中id =(此处将id放在用户要搜索的位置)),并将其显示在列表视图中。 For simple example use this reference. 对于简单示例,请使用此参考。 http://javapapers.com/android/android-sqlite-database/ http://javapapers.com/android/android-sqlite-database/

I will explain the logical method to do this: 我将解释执行此操作的逻辑方法:

You will need to check the ID you get from the user, and compare this ID with each ID stored in the database. 您将需要检查从用户那里获得的ID,并将该ID与数据库中存储的每个 ID进行比较。 Once you find a match, you should collect all the details under that ID. 找到匹配项后,您应该收集该ID下的所有详细信息。 So for this you will require a readable database, and then try to collect the information using a Cursor . 因此,为此,您将需要一个可读的数据库,然后尝试使用Cursor收集信息。

Check this for further info: http://www.vogella.com/tutorials/AndroidSQLite/article.html 检查此以获得更多信息: http : //www.vogella.com/tutorials/AndroidSQLite/article.html

Write all this code within the onClick() method of your search button. 搜索按钮的onClick()方法中编写所有这些代码。

EDIT: Code to include the above functionality in onClick() event: 编辑:代码以在onClick()事件中包含以上功能:

search.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //Include your code here
        }   
     });

Also create a search button in your layout and instantiate it using 还可以在布局中创建一个搜索按钮,并使用实例化它

 private Button search = null; // This line inside your MainActivity class
 search = (Button)findViewById(R.id.search); //This line inside onCreate function.

As for the code to actually implement that logic, check the link i gave you and try coding it on you own. 至于实际实现该逻辑的代码,请检查我给您的链接,然后尝试自行编码。

Create a new Class for creating the Database and doing queries/selects, inserts and updates. 创建一个新类,以创建数据库并进行查询/选择,插入和更新。

It will bi nice to separate your SQL script - use String variables 分离您的SQL脚本会很不错-使用字符串变量

private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMP(id varchar, name varchar, email varchar);";

to insert data use db.insert , select db.select or rawQuery . 要使用db.insert插入数据,请选择db.selectrawQuery You need to use Cursor to retrieve the data from the DB query. 您需要使用Cursor从数据库查询中检索数据。 After getting the data from the Cursor you can arrange it as you wish. 游标获取数据后,您可以根据需要进行排列。

private static final String SELECT_ID = "SELECT name, mail FROM EMP WHERE id = ? ;";

Your DB id must be INTEGER and not a varchar - it is a Primary Key or ? 您的数据库ID必须是INTEGER而不是varchar-它是主键或? Primary Key: http://en.wikipedia.org/wiki/Unique_key SQLite Tuts: http://www.tutorialspoint.com/sqlite/index.htm 主键: http : //en.wikipedia.org/wiki/Unique_key SQLite Tuts: http ://www.tutorialspoint.com/sqlite/index.htm

http://developer.android.com/training/basics/data-storage/databases.html http://developer.android.com/training/basics/data-storage/databases.html

Code for the Search 搜索代码

Button search = (Button) findViewById(R.id.but_search);
search.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // DO queries, Update TextView or call your methods
         }
     });

This is bad: 这不好:

id=edittext1.getText().toString();
name=edittext2.getText().toString();
email=edittext3.getText().toString();
db.execSQL("INSERT INTO EMP(id,name,email) values ('"+id+"','"+name+"','"+email+"');");

It will be nice first to check the data and then to insert it into the Database. 最好先检查数据,然后将其插入数据库。

 if(email.lenght() > 0 ){
 // check other data, do things
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中单击“搜索”按钮时从数据库中检索数据 - Retrieve data from the database when Search Button is click in Android 搜索记录并从Android中的数据库中检索它 - Search record and retrieve it from the database in Android 单击按钮时如何从sqlite数据库中检索数据 - How to retrieve the data from sqlite database on button click 按钮点击从firebase数据库中检索数据 - Retrieve data from firebase database on button click 如何从 firebase 数据库 android 检索单选按钮的值 - how to retrieve value of a radio button back from firebase database android 如何在Android中从arraylist(Sqlite数据库)单击记录时检索特定数据? - How to retrieve particular data on clicking record from arraylist (Sqlite database)in Android?? 如何从按钮的单击事件中获取数据库到另一个活动以显示插入的记录? - How to Fetch Database from button's click event to another activity for show inserted record? Android:单击按钮即可从数据库中删除行 - Android: deleting the rows from database on click of a button 如何使用Java Android Studio中的多个按钮从Firebase实时数据库检索数据库? - How to use more than one button in Java android studio to retrieve a database from firebase realtime database? (Android Kotlin) 如何在 recyclerView 中单击按钮时从 firebase 存储中检索图像“名称”? - (Android Kotlin) How to retrieve the image “Name” from firebase storage on a Button click in a recyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM