如何实现搜索(编辑文本)功能?

时间:2015-09-08 02:53:57

标签: android android-sqlite

我有一个listview,可以从sqlite和edittext获取所有单词(我想从数据库中过滤/搜索单词)。

我想实现一个TextWatcher,它好多了,但我不知道如何使用我的代码。我是新手。

IdFTP_Main.Get('test.ini',MemoryStream);
MemoryStream.Position := 0;
MemoryStream.SaveToFile('./test.ini');

DictionaryDatabaseHelper

public class DictionaryListActivity extends Activity {

TextView userTextView;
EditText searchEditText;

ListView dictionaryListView;

String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();


DictionaryDatabaseHelper myDictionaryDatabaseHelper;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary_list);

    Log.d("DICTIONARY", "second activity started");


    userTextView=(TextView) findViewById(R.id.personTextView);
    userTextView.setText(getIntent().getStringExtra(MainActivity.USER_NAME_STRING)+"'s Dictionary");

    searchEditText=(EditText) findViewById(R.id.searchEditText);

    dictionaryListView=(ListView) findViewById(R.id.dictionaryListView);

    myDictionaryDatabaseHelper=new DictionaryDatabaseHelper(this, "Dictionary", null, 1);
    sharedPreferences=getSharedPreferences(MainActivity.SHARED_NAME_STRING, MODE_PRIVATE);


    boolean initialized=sharedPreferences.getBoolean("initialized", false);

    if (initialized==false) {
        //Log.d(logTagString, "initializing for the first time");
        initializeDatabase();
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("initialized", true);
        editor.commit();

    }else {
        Log.d(logTagString, "db already initialized");
    }

    allWordDefinitions=myDictionaryDatabaseHelper.getAllWords();

    dictionaryListView.setAdapter(new BaseAdapter() {

        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            if (view==null) {
                view=getLayoutInflater().inflate(R.layout.list_item, null);
            }
            TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
            textView.setText(allWordDefinitions.get(position).word);

            return view;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return allWordDefinitions.size();
        }
    });

    dictionaryListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Intent intent =new Intent(DictionaryListActivity.this, WordDefinitionDetailActivity.class);
            intent.putExtra("word", allWordDefinitions.get(position).word);
            intent.putExtra("definition", allWordDefinitions.get(position).definition);

            startActivity(intent);
        }
    });



private void initializeDatabase() {
    InputStream inputStream=getResources().openRawResource(R.raw.dictionary);
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
    DictionaryLoader.loadData(bufferedReader, myDictionaryDatabaseHelper);

}

 }

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

在全球范围内,您需要定义searchProducts。

List<WordDefinition> searchProducts;  

searchEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
       }

       @Override
        public void afterTextChanged(Editable s) {
         String text = s.toString();
         getSearchResult(text);
        }
});

在getSearchResult的方法中,您可以看到搜索逻辑。您可以根据自己的要求进行更改。

public void getSearchResult(String s) {
                    myDictionaryDatabaseHelper=new DictionaryDatabaseHelper(this, "Dictionary", null, 1);
                    allWordDefinitions=myDictionaryDatabaseHelper.getAllWords();
                    searchProducts = new ArrayList<WordDefinition>();

                    for (int i = 0; i < allWordDefinitions.size(); i++) {
                        if (s.equals("")) {
                        } else {
                        if (allWordDefinitions.get(i).YOUR_MODEL_METHODNAME
                                    .toLowerCase(Locale.getDefault()).contains(s)) {
                            searchProducts.add(allWordDefinitions.get(i));
                        }
                    }
                listUpdate(searchProducts);
            }

        }

        private void listUpdate(List<WordDefinition> searchProducts) {
          // Here you call your adapter.
        }

如果代码适合您,请将其标记为可接受。