简体   繁体   English

将参数从MainActivity.java传递到我的SQLiteOpenHelper类中

[英]passing a parameter from MainActivity.java into my SQLiteOpenHelper class

I have set up my SQLiteOpenHelper class and all working ok to retrieve all data in the sqLite database. 我已经设置了SQLiteOpenHelper类,并且一切正常,可以检索sqLite数据库中的所有数据。 I now want tp pass a parameter from mainActivity into my SQLiteOpenHelper class to find certain records. 我现在希望tp从mainActivity传递一个参数到我的SQLiteOpenHelper类中以查找某些记录。 User will enter search criteria into a field and i want to pass that user entry to be searched. 用户将在字段中输入搜索条件,而我想通过该用户条目进行搜索。 Currently i have hard coded to search for category 161 but this is where i want to be able to use the parameter passed by the user. 目前,我已经硬编码搜索类别161,但这是我希望能够使用用户传递的参数的地方。 I have put my SQLiteOpenerHelper code and my mainActivty code below. 我在下面放置了SQLiteOpenerHelper代码和mainActivty代码。 I have tried passing a parameter into the getAllUsers() in mainActivity file but Android will not accept parameters. 我尝试过将参数传递到mainActivity文件的getAllUsers()中,但Android将不接受参数。 Can someone please help in showing me what i need to do to pass the parameter i need into the sqLite query 有人可以帮我看看我需要怎么做才能将我需要的参数传递给sqLite查询

SQLiteOpenHelper class SQLiteOpenHelper类

public ArrayList<HashMap<String, String>> getAllUsers() {
        ArrayList<HashMap<String, String>> usersList;
        usersList = new ArrayList<HashMap<String, String>>();
        String[] columns = {"id", "company", "firstname", "lastname", "category"};

        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.query("members", columns, "category = 161", null, null, null, "company");
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", cursor.getString(0));
                map.put("company", cursor.getString(1));
                map.put("firstname", cursor.getString(2));
                map.put("lastname", cursor.getString(3));
                map.put("category", cursor.getString(4));

                usersList.add(map);
            } while (cursor.moveToNext());
        }
        database.close();
        return usersList;
    }

mainActivity.java mainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get User records from SQLite DB
        ArrayList<HashMap<String, String>> userList = controller.getAllUsers();

I don't see a problem here. 我在这里没有问题。 Just define the method as: 只需将方法定义为:

public ArrayList < HashMap< String, String >> getAllUsers(int category){
    //
}

Then instead of "category = 161" , use "category = " + category as the third parameter in query. 然后,使用"category = " + category作为查询中的第三个参数,而不是"category = 161"

And call it from main activity as: 并从主要活动中将其称为:

ArrayList < HashMap< String , String>> userList = controller.getAllUsers(161);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM