This is a search for just one word to find many words. Use the sign, divided into individual words.
package com.example.vish.offlin
edictionarydemo;import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
/**
-
Created by VisH on 18-10-2016.
*/
public class Dictionary extends AppCompatActivity {private Button button;
ImageView imageView,imageView1;
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
public static ArrayList data;
DatabaseHelper db ;
ArrayList wordcombimelist;
ArrayList meancombimelist;
LinkedHashMap<String,String> namelist;
SearchView searchView;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); recyclerView.setHasFixedSize(true); db= new DatabaseHelper(this); searchView = (SearchView) findViewById(R.id.searchView); searchView.setQueryHint("ชื่อสินค้า"); searchView.setQueryRefinementEnabled(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); data = new ArrayList<DictObjectModel>(); fetchData(); button = (Button) findViewById (R.id.button); imageView1 = (ImageView) findViewById(R.id.img_tool_mic); imageView = (ImageView) findViewById(R.id.ihome); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(getApplicationContext(), MenuUser.class); startActivity(i); finish(); } }); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) {return false; } @Override public boolean onQueryTextChange(String newText) { newText = newText.toLowerCase(); final ArrayList<DictObjectModel> filteredList = new ArrayList<DictObjectModel>(); for (int i = 0; i < wordcombimelist.size(); i++) { final String text = wordcombimelist.get(i).toLowerCase(); if (text.contains(newText)) { filteredList.add(new DictObjectModel(wordcombimelist.get(i),meancombimelist.get(i))); } } adapter = new CustomAdapter(filteredList); recyclerView.setAdapter(adapter); return true; } }); imageView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); try{ startActivityForResult(intent,200); }catch (ActivityNotFoundException a){ Toast.makeText(getApplicationContext(),"Intent problem", Toast.LENGTH_SHORT).show(); } } });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 200){
if(resultCode == RESULT_OK && data != null){
ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);} }
}
public void fetchData()
{
db =new DatabaseHelper(this);
try {db.createDataBase(); db.openDataBase(); } catch (Exception e) { e.printStackTrace(); } namelist=new LinkedHashMap<>(); int ii; SQLiteDatabase sd = db.getReadableDatabase(); Cursor cursor = sd.query("Dictionary1" ,null, null, null, null, null, null); ii=cursor.getColumnIndex("word"); wordcombimelist=new ArrayList<String>(); meancombimelist= new ArrayList<String>(); while (cursor.moveToNext()){ namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("definition"))); } Iterator entries = namelist.entrySet().iterator(); while (entries.hasNext()) { Map.Entry thisEntry = (Map.Entry) entries.next(); wordcombimelist.add(String.valueOf(thisEntry.getKey())); meancombimelist.add("- "+String.valueOf(thisEntry.getValue())); } for (int i = 0; i < wordcombimelist.size(); i++) { data.add(new DictObjectModel(wordcombimelist.get(i), meancombimelist.get(i))); } adapter = new CustomAdapter(data); recyclerView.setAdapter(adapter);
}
}