Android 如何从自己的数据库创建自己的searchView加载?

Android 如何从自己的数据库创建自己的searchView加载?,android,sqlite,listview,searchview,Android,Sqlite,Listview,Searchview,我很难创建自己的searchView,从数据库加载它,并将数据显示到列表中 现在我的应用程序正在运行,当我按下搜索按钮时,会出现一个搜索列表,但我无法进行任何搜索。当我开始将我的搜索写入搜索视图时,应用程序不会查找和筛选我的列表,当我再次提交搜索时,我的应用程序将返回显示我的搜索列表的主活动 主要活动 public class MainActivity extends AppCompatActivity { /** Database helper that will provide u

我很难创建自己的searchView,从数据库加载它,并将数据显示到列表中

现在我的应用程序正在运行,当我按下搜索按钮时,会出现一个搜索列表,但我无法进行任何搜索。当我开始将我的搜索写入搜索视图时,应用程序不会查找和筛选我的列表,当我再次提交搜索时,我的应用程序将返回显示我的搜索列表的主活动

主要活动

public class MainActivity extends AppCompatActivity {

    /** Database helper that will provide us access to the database */
    private MarluvasDbHelper myDbHelper;


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

        myDbHelper = new MarluvasDbHelper(this);
        try {
            myDbHelper.createDataBase();

        } catch (IOException ice) {
            throw new Error("Unable to connect");
        }
        myDbHelper.openDataBase();

        // Create and/or open a database to read from it
        //SQLiteDatabase db = myDbHelper.getReadableDatabase();
        //Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_SHORT).show();
        //Toast.makeText(MainActivity.this, MarluvasContract.MarluvasEntry.COLUMN_COD, Toast.LENGTH_SHORT).show();
        //displayDatabaseInfo();
    }

    private void displayDatabaseInfo(){
        // Create and/or open a database to read from it
        SQLiteDatabase db = myDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                MarluvasContract.MarluvasEntry._ID,
                MarluvasContract.MarluvasEntry.COLUMN_MODELO,
                MarluvasContract.MarluvasEntry.COLUMN_COR};

        // Perform a query on the pets table
        Cursor cursor = db.query(
                MarluvasContract.MarluvasEntry.TABLE_NAME,   // The table to query
                projection,            // The columns to return
                null,                  // The columns for the WHERE clause
                null,                  // The values for the WHERE clause
                null,                  // Don't group the rows
                null,                  // Don't filter by row groups
                null);                   // The sort order

        ListView listView = (ListView) findViewById(R.id.list);

        CustomAdapter adapter = new CustomAdapter(this, cursor, 0);

        listView.setAdapter(adapter);
    }

    public Cursor getStudentListByKeyword(String search) {
        //Open connection to read only
        SQLiteDatabase db = myDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                MarluvasContract.MarluvasEntry._ID,
                MarluvasContract.MarluvasEntry.COLUMN_MODELO,
                MarluvasContract.MarluvasEntry.COLUMN_COR};

        //String whereClause = MarluvasContract.MarluvasEntry.COLUMN_MODELO+ "=?";
        //String [] whereArgs = {"  LIKE  '%" +search + "%' "};

        // Perform a query on the pets table
        Cursor cursor = db.query(
                MarluvasContract.MarluvasEntry.TABLE_NAME,   // The table to query
                projection,            // The columns to return
                null,                  // The columns for the WHERE clause
                null,                  // The values for the WHERE clause
                null,                  // Don't group the rows
                null,                  // Don't filter by row groups
                null);                   // The sort order



        ListView petListView = (ListView) findViewById(R.id.list);

        CustomAdapter adapter = new CustomAdapter(this, cursor, 0);

        petListView.setAdapter(adapter);

        return cursor;
    }



    @Override
    public void onResume(){
        super.onResume();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(search).getActionView();

        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
               getStudentListByKeyword(s);

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                getStudentListByKeyword(s);

                return false;
            }

        });

        return true;

    }
}
自定义适配器

public class CustomAdapter extends CursorAdapter {

    private LayoutInflater mInflater;

    public CustomAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return LayoutInflater.from(context).inflate(R.layout.item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        // Find fields to populate in inflated template
        TextView modelo = (TextView) view.findViewById(R.id.modelo);
        TextView cor = (TextView) view.findViewById(R.id.cor);

        // Extract properties from cursor
        String stringModelo = cursor.getString(cursor.getColumnIndexOrThrow(MarluvasContract.MarluvasEntry.COLUMN_MODELO));
        String stringDescr = cursor.getString(cursor.getColumnIndex(MarluvasContract.MarluvasEntry.COLUMN_COR));

        // Populate fields with extracted properties
        modelo.setText(stringModelo);
        cor.setText(stringDescr);


    }
}
我怎样才能修好它?如何实现setOnQueryTextListener方法


非常感谢你

结账

我该怎么办?
。。。你应该给我们展示这个问题的一个最简单的表述。您包含的代码太多了。问题发生在这一行codecursor=queryPlace.getStudentList();我添加了更多关于我的工作的细节error@TimBiegeleisen我还切断了一些代码。我尝试了,但在这行代码中出现了错误:Cursor=db.rawQuery(selectQuery,null);在GetStudentList中,我将添加表创建代码。。。我使用合同和代码从资产文件夹中读取我的数据库。如果我读得正确,您正在尝试将现有数据库从外部源复制到您的设备。对吗?我有一个叫做teste.db的数据库。我把这个数据库放在资产文件夹中,我想创建一个搜索视图来浏览我看到的数据。据我所知,您缺少了实际创建表的语句。