Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – SearchView显示从数据库到ListView的结果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经设法有一个应用程序将数据插入sqlite数据库并在具有自定义布局和适配器的ListView中查看它.

但是,当我试图使SearchView根据用户的输入过滤结果时,我无处可去.

以下是我的代码,请指出我的解决方

DbHelper

public class DbHelper extends sqliteOpenHelper {

    public static final String db_name = "homeworks.db";
    public static final int DB_VERSION = 1;

    public static final String createTaksTableQuery =
            "create table "+ DbInfo.TasksInfo.TABLE_NAME+"("+DbInfo.TasksInfo.taskId+
             " INTEGER PRIMARY KEY AUTOINCREMENT,"+DbInfo.TasksInfo.taskName+" NVARCHAR(255),"
                    +DbInfo.TasksInfo.taskDate+" DATE,"+DbInfo.TasksInfo.taskTime+" TIME,"+ DbInfo.TasksInfo.taskNotes+" NVARCHAR(255));";

    public static final String createNotesTableQuery =
            "create table "+ DbInfo.NotessInfo.TABLE_NAME+"("+DbInfo.NotessInfo.notEID+
                    " INTEGER PRIMARY KEY AUTOINCREMENT,"+DbInfo.NotessInfo.notename+" NVARCHAR(255),"
                    +DbInfo.NotessInfo.noteDate+" DATE,"+ DbInfo.NotessInfo.noteContents+" NVARCHAR(255));";


    public DbHelper(Context context) {
        super(context,db_name,null,DB_VERSION);
        Log.e("Database Operation","Database Created / Opened...");
    }

    @Override
    public void onCreate(sqliteDatabase db) {
        db.execsql(createTaksTableQuery);
        Log.e("Database Operation","Tasks Table Created ...");
        db.execsql(createNotesTableQuery);
        Log.e("Database Operation","Notes Table Created ...");

    }

    public void insert@R_541_1@R_618_11226@6@sks(String tName,String tDate,String tTime,String taskNote,sqliteDatabase db) {

        ContentValues contentValues = new ContentValues();

        contentValues.put(DbInfo.TasksInfo.taskName,tName);
        contentValues.put(DbInfo.TasksInfo.taskDate,tDatE);
        contentValues.put(DbInfo.TasksInfo.taskTime,tTimE);
        contentValues.put(DbInfo.TasksInfo.taskNotes,taskNotE);

        db.insert(DbInfo.TasksInfo.TABLE_NAME,contentValues);

        Log.e("Insertion OP","Row inserted into Tasks Databases");
    }

    public void insertToNotes(String nName,String nDate,sqliteDatabase db) {

        ContentValues contentValues = new ContentValues();

        contentValues.put(DbInfo.NotessInfo.notename,nName);
        contentValues.put(DbInfo.NotessInfo.noteDate,nDatE);

        db.insert(DbInfo.NotessInfo.TABLE_NAME,"Row inserted into Notes Databases");
    }


    public cursor getTasks(sqliteDatabase db) {
        cursor cursor;
        String[] projections = {
            DbInfo.TasksInfo.taskId,DbInfo.TasksInfo.taskName,DbInfo.TasksInfo.taskDate,DbInfo.TasksInfo.taskTime,DbInfo.TasksInfo.taskNotes};


            cursor = db.query(DbInfo.TasksInfo.TABLE_NAME,projections,DbInfo.TasksInfo.taskId + " desc");

        return cursor;
    }

    public cursor searchTasks(sqliteDatabase db,String searchTxt) {
        cursor cursor;
        String q = "SELEct * FROM tasksTable where taskName Like '"+searchTxt+"%'";
        cursor = db.rawQuery(q,null);
        Log.e("Database Op",q);
        return cursor;

    }


    @Override
    public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {

    }
}

主类

解决方法

我有管理来解决问题……
我不得不在SearchView上创建onQueryTextChangedListner,创建一个新的适配器和一个新的游标,从数据库获取数据然后放入适配器并设置一个新的适配器到ListView

下面是新的最终代码,包括,插入,删除,搜索“选择”和删除,以及ListView,searchView方法

DbHelper类

package ly.edu.CET.www.myhomworks;

import android.content.ContentValues;
import android.content.Context;
import android.database.cursor;
import android.database.sqlException;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;

/**
 * Created by newton on 11/19/15.
 */
public class DbHelper extends sqliteOpenHelper {

    public static final String db_name = "homeworks.db";
    public static final int DB_VERSION = 1;

    public static final String createTaksTableQuery =
            "create table "+ DbInfo.TasksInfo.TABLE_NAME+"("+DbInfo.TasksInfo.taskId+
             " INTEGER PRIMARY KEY AUTOINCREMENT,String searchTxt) {
        cursor cursor;

        String[] projections = {
                DbInfo.TasksInfo.taskId,DbInfo.TasksInfo.taskNotes};

        cursor = db.query(DbInfo.TasksInfo.TABLE_NAME,DbInfo.TasksInfo.taskName+" Like '"+searchTxt+"%'",null);

        return cursor;

    }


    public void updateTasks(sqliteDatabase db,String id,String tname,String tdate,String ttime,String tnotE) {

        ContentValues values = new ContentValues();

        values.put(DbInfo.TasksInfo.taskName,tName);
        values.put(DbInfo.TasksInfo.taskDate,tdatE);
        values.put(DbInfo.TasksInfo.taskTime,ttimE);
        values.put(DbInfo.TasksInfo.taskNotes,tnotE);

        int cursor = db.update(DbInfo.TasksInfo.TABLE_NAME,values,DbInfo.TasksInfo.taskId+" = "+id,null);
    }

    public Boolean deleteTasks(sqliteDatabase db,String id) {

        return  db.delete(DbInfo.TasksInfo.TABLE_NAME,null) >0;

    }
    @Override
    public void onUpgrade(sqliteDatabase db,int newVersion) {

    }
}

这是我的TasksFragment在swipeview选项卡内的MainActivity上调用

package ly.edu.CET.www.myhomworks;

import android.content.Intent;
import android.database.cursor;
import android.database.sqlite.sqliteDatabase;
import android.support.design.widget.FloaTingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.os.bundle;
import android.support.Annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.support.v4.app.Fragment;
import android.widget.AdapterView;
import android.widget.button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

/**
 * Created by newton on 11/19/15.
 */
public class TasksFragment extends Fragment {

    ListView tasksListView;
    sqliteDatabase sqliteDatabase;
    cursor cursor;
    cursor scursor;
    DbHelper dbHelper;

    Taskslistadapter adapter;
    SearchView searchView;
    String searchQuery;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,final ViewGroup container,Bundle savedInstanceStatE) {

        View view = inflater.inflate(R.layout.tasks_layout,container,falsE);
        searchView = (SearchView) view.findViewById(R.id.searchView);
        tasksListView = (ListView) view.findViewById(R.id.tasksListView);
        adapter = new Taskslistadapter(getContext(),R.layout.tasks_row);

        dbHelper = new DbHelper(view.getContext());
        sqliteDatabase = dbHelper.getReadableDatabase();


        tasksListView.setAdapter(adapter);
        cursor = dbHelper.getTasks(sqliteDatabasE);

        if (cursor.moveToFirst()) {
            do {
                String name,date,time,note;
                name = cursor.getString(1);
                date = cursor.getString(2);
                time = cursor.getString(3);
                note = cursor.getString(4);

                DataProvider dataProvider = new DataProvider(name,notE);

                adapter.add(dataProvider);

            } while (cursor.moveToNext());
        }
        adapter.notifyDataSetChanged();

        tasksListView.setOnItemClickListener(new AdapterView.onItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

                if (cursor != null) {
                    if (cursor.moveToFirst()) {
                        cursor.moveToPosition(position);
                        String listId = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskId));
                        String listName = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskName));
                        String listDate = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskDatE));
                        String listTime = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskTimE));
                        String listNote = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskNotes));

                        Intent updateTasksIntent = new Intent(getContext(),AddTask.class);

                        updateTasksIntent.putExtra("editTask",truE);
                        updateTasksIntent.putExtra("taskId",listId);
                        updateTasksIntent.putExtra("taskName",listName);
                        updateTasksIntent.putExtra("taskDate",listDatE);
                        updateTasksIntent.putExtra("taskTime",listTimE);
                        updateTasksIntent.putExtra("taskNote",listNotE);

//                        Toast.makeText(getContext(),"List row " + position + " ID = " + listId,Toast.LENGTH_SHORT).show();

                        startActivity(updateTasksIntent);
                    }
                }

            }
        });


        tasksListView.setOnItemLongClickListener(new AdapterView.onItemLongClickListener() {
            @Override
            public Boolean onItemLongClick(AdapterView<?> parent,long id) {

                if (cursor != null) {
                    if (cursor.moveToFirst()) {
                        cursor.moveToPosition(position);
                        String listId = cursor.getString(cursor.getcolumnIndex(DbInfo.TasksInfo.taskId));

                        Log.e("Database Op","List row " + position + " ID = " + listId);

                        dbHelper.deleteTasks(sqliteDatabase,listId);

                        Taskslistadapter newAdapter = new Taskslistadapter(getContext(),R.layout.tasks_row);

                        cursor = dbHelper.getTasks(sqliteDatabasE);

                        if (cursor.moveToFirst()) {
                            do {
                                String name,note;
                                name = cursor.getString(1);
                                date = cursor.getString(2);
                                time = cursor.getString(3);
                                note = cursor.getString(4);

                                DataProvider dataProvider = new DataProvider(name,notE);

                                newAdapter.add(dataProvider);

                            } while (cursor.moveToNext());
                        }
                        newAdapter.notifyDataSetChanged();
                        tasksListView.setAdapter(newAdapter);
//                        Toast.makeText(getContext(),Toast.LENGTH_SHORT).show();

                    }
                }
                return true;
            }
        });

        searchView.setOnQueryTextListener(new SearchView.onQueryTextListener() {
            @Override
            public Boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public Boolean onQueryTextChange(String newText) {

                Taskslistadapter searchAdapter = new Taskslistadapter(getContext(),R.layout.tasks_row);

                scursor = dbHelper.searchTasks(sqliteDatabase,newText);

                if (scursor.moveToFirst()) {
                    do {
                        String name,note;
                        name = scursor.getString(1);
                        date = scursor.getString(2);
                        time = scursor.getString(3);
                        note = scursor.getString(4);

                        DataProvider dataProvider = new DataProvider(name,notE);

                        searchAdapter.add(dataProvider);

                    } while (scursor.moveToNext());
                }
                tasksListView.setAdapter(searchAdapter);
                searchAdapter.notifyDataSetChanged();

                return true;
            }
        });


        FloaTingActionButton fab = (FloaTingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.onClickListener() {
            @Override
            public void onClick(View view) {
                Intent addTaskIntent = new Intent(getContext(),AddTask.class);
                addTaskIntent.putExtra("addNewTask",truE);
                startActivity(addTaskIntent);
            }
        });

        return view;
    }

}

祝你们好运:)穆罕德格里特利

大佬总结

以上是大佬教程为你收集整理的android – SearchView显示从数据库到ListView的结果全部内容,希望文章能够帮你解决android – SearchView显示从数据库到ListView的结果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。