SearchView显示从SQLITE数据库到ListView的结果

时间:2017-10-07 12:43:07

标签: android sqlite searchview

我想对自定义列表向我显示医疗记录的活动进行搜索。我没有参与SearchView

我的医学数据库代码:

package com.example.sarhanaashir.wecare.Database_MedInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;


public class DatabaseHelperMed extends SQLiteOpenHelper

{
    private static final String Database_Name = "WeCareDatabaseMed";
    private static final int Database_Ver = 1;

    public DatabaseHelperMed(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
    {
        super(context,Database_Name,null,Database_Ver);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        String query = "CREATE TABLE tbl_med(id INTEGER PRIMARY KEY AUTOINCREMENT,MedName TEXT,MedSymptom TEXT)";
        sqLiteDatabase.execSQL(query);
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
    {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS tbl_med");
        this.onCreate(sqLiteDatabase);
    }

    public boolean addNewMedicine(MedicineRecord med_rec)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put("MedName", med_rec.getMed_name() );
        values.put("MedSymptom", med_rec.getMed_sym() );

        db.insert("tbl_med", null, values);
        db.close();

        return true;
    }

    public ArrayList<MedicineRecord> getAllMed()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<MedicineRecord> medi = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from tbl_med", null);
        if (cursor.moveToFirst())
        {

            while (!cursor.isAfterLast())
            {
                String id = cursor.getString(0);
                String name = cursor.getString(1);
                String sympt = cursor.getString(2);

                medi.add(new MedicineRecord(name,sympt));
                cursor.moveToNext();

            }
        }
        return medi;
    }
    public Cursor searchTasks(SQLiteDatabase db, String searchTxt)

    {
        Cursor cursor;
        String q = "select * tbl_med from  where MedSymptom Like '"+searchTxt+"%'";
        cursor = db.rawQuery(q,null);
        return cursor;
    }
}

我的活动SearchView是:

 package com.example.sarhanaashir.wecare.Database_MedInfo;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.app.SearchManager;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

import com.example.sarhanaashir.wecare.R;

import java.util.ArrayList;

public class Med_info extends AppCompatActivity {

    private ArrayList<MedicineRecord> med_record;
    private MedicineListAdapter adapter;
    private ListView listView;
    DatabaseHelperMed db;
    SearchView searchView1;


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

        inti();

        searchView1 = (SearchView) findViewById(R.id.SVscrhMed);

        searchView1.setOnQueryTextListener(new OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String qry)
            {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText)

            {
                return true;
            }
        });
    }

    private void inti()
    {
        db = new DatabaseHelperMed(Med_info.this,"WeCareDatabaseMed.db",null,1);
        med_record = new ArrayList<>();
        listView = (ListView) findViewById(R.id.Med_list);
        adapter = new MedicineListAdapter(this, R.layout.med_rec_style, med_record);
        listView.setAdapter(adapter);

        fillmed();

    }

    private void fillmed()
    {
        med_record.clear();
        med_record.addAll(db.getAllMed()); // Getting Medicine from database.
        adapter.notifyDataSetChanged();
    }
}

My Medicine Records完美地显示在我的自定义列表中,我唯一想要的是放置一个searchView,以便用户可以从列表中搜索药物。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

 searchView1.setOnQueryTextListener(new OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String qry)
            {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText)
            {
                adapter.getFilter().filter(newText);
                return true;
            }
        });

答案 1 :(得分:0)

Try this code:
// DatabaseHelperMed //
final ArrayList<MedicineRecord> medicalRecords = new ArrayList<MedicineRecord>();
public ArrayList<MedicineRecord> getMedicine(String name) {
    try {
        medicalRecords.clear();
        String selectQuery = "SELECT  * FROM tbl_med "+" WHERE MedSymptom LIKE '%" + name + "%'";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                MedicineRecord medRec = new MedicineRecord();
                medRec.setMed_name(cursor.getString(1));
                medRec.setMed_sym(cursor.getString(2));
                medicalRecords.add(medRec);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return medicalRecords;
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("get_all_contacts", "" + e);
    }
    return medicalRecords;
}
// SearchView
 searchView1.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        DatabaseHelperMed db ;
        db = new DatabaseHelperMed(getApplicationContext());
        ArrayList<MedicineRecord> medRecords = db.getMedicine(newText);
        for (int i = 0; i < medRecords.size(); i++) {
            String name = medRecords.get(i).getMed_name();
            String symp = medRecords.get(i).getMed_sym();
        }
        return false;
    }
});