samedi 21 novembre 2015

searchView show result from database to listview android

Hello dear Android Programmers,

i've Managed to have an app that inserts data to a SqLite Database and view it in a ListView that has a custom Layout and Adapter.

However when i tried to make a searchView that filters the results based on the user's input , well i got nowhere .

below is my code , please point me to the solution

****my DbHelper class :****

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 insertToTasks(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, null, 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, null, contentValues);

        Log.e("Insertion OP", "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, null, null, null, null, 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) {

    }
}


my Main Class

public class TasksFragment extends Fragment {

ListView tasksListView;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
DbHelper dbHelper;

TasksListAdapter adapter;
SearchView searchView;
String searchQuery;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, 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();


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {


            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            tasksListView.setVisibility(View.GONE);
            return true;
        }
    });



    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, date, time, note);

                adapter.add(dataProvider);

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



    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent = new Intent(getContext(), AddTask.class);
                startActivity(intent);
        }
    });

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


        }
    });
    adapter.notifyDataSetChanged();


    return view;
}

}

Aucun commentaire:

Enregistrer un commentaire