如何在android中搜索外部SQLite数据库?

时间:2016-06-17 19:35:35

标签: android android-sqlite android-search

我正在开发一个带有外部数据库的字典。我可以用RecyclerView,Adapter和DBHelpr显示所有单词,但我的搜索和查询有问题。我不知道如何创建它的搜索。我想搜索2列。

MainActivity

public class MainActivity extends AppCompatActivity {


    EditText Et;
    SQLiteDatabase sqLiteDatabase;
    private databse db;
    Adapter mAdapter;
    private List<Words> list = new ArrayList<>();


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

        Toolbar toolbar= (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        db=new databse(this);

        db.database();

        db.open();

        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.listviews);
        mAdapter = new Adapter(GetWords(),this,R.layout.itemsforeach);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        Et = (EditText)findViewById(R.id.Edidttext);
        Et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
            {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
            {

            }

            @Override
            public void afterTextChanged(Editable editable)
            {

            }
        });
       }




    @Override
    protected void onResume() {
        super.onResume();
    }

    public List<Words> GetWords()
    {
         List<Words> list = new ArrayList<>();

        databse databse = new databse(this);
        sqLiteDatabase = databse.getWritableDatabase();
        String query ="SELECT * FROM dictionary";
        Cursor c = sqLiteDatabase.rawQuery(query,null);

        if(c != null)
        {
            c.moveToFirst();
            do {
                //make a new object of Words's class
                Words words = new Words();
                words.setEwords(c.getString(c.getColumnIndex("src")));
                words.setPwords(c.getString(c.getColumnIndex("dist")));
                list.add(words);
            }while (c.moveToNext());
            return list;
        }
        else
        {
            return null;
        }
    }


}

数据库

public class databse extends SQLiteOpenHelper {


    public static final String path="data/data/com.dic.hossein.mydictionary/databases/";
    public static final String Name="mydice.db3";
    public SQLiteDatabase mydb;

    private final Context mycontext;

    public databse(Context context) {
        super(context, "mydice.db3", null, 1);
        mycontext=context;

    }



    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    public void database(){

        boolean checkdb=checkdb();

        if(checkdb){


        }else{

            this.getReadableDatabase();

            try{
            copydatabase();
            }catch(IOException e){


            }

        }



    }

    public void open(){

        mydb=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READWRITE);

    }



    public void close(){
        mydb.close();
    }


    public boolean checkdb(){

        SQLiteDatabase db=null;
        try{    
        db=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch(SQLException e)
        {

        }
        return db !=null ? true:false ;

    }

    public void copydatabase() throws IOException{
        OutputStream myOutput = new FileOutputStream(path+Name);
        byte[] buffer = new byte[1024];
        int length;

        InputStream myInput = mycontext.getAssets().open("mydice.db3");
        while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }



    public String Display(int row,int fild){
        Cursor cu= mydb.query("dictionary", null, null, null, null, null, null);
        cu.moveToPosition(row);
        String name=cu.getString(fild);
        return name;
    }

}

适配器

public class Adapter extends RecyclerView.Adapter<ItemsViewHolder> {

    Context context;
    List<Words> wordses;
    int UI;

    public Adapter(List<Words> wordses, Context context, int UI) {
        this.wordses = wordses;
        this.context = context;
        this.UI = UI;
    }

    @Override
    public ItemsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemsViewHolder itemsViewHolder = new ItemsViewHolder(LayoutInflater.from(context).inflate(UI,null));

        return itemsViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemsViewHolder holder, int position) {

        final Words words = wordses.get(position);
        holder.pWords.setText(words.getPwords());
        holder.eWords.setText(words.getEwords());

    }

    @Override
    public int getItemCount() {
        return wordses.size();
    }
}

0 个答案:

没有答案