SearchView返回空列表视图

时间:2016-11-01 09:27:24

标签: android listview searchview custom-adapter

我有ActivitySearchView和自定义ListView,当我点击SearchView时,它不会过滤ListView上的元素,但仅显示完成写作后的一个元素,以及清除SearchView时,它不会返回主ListView

请帮忙, 感谢

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    List<WordTranslation> quotes;
    Context context;
    Adapter adapter;
    Button searchBtn;
    ArrayList <WordTranslation> arrayList = new ArrayList<>();
    WordTranslation wordTranslation;
    DatabaseAccess databaseAccess;
    SearchView sv;
    EditText editText;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public  ArrayList<WordTranslation> getwordtranslation(String SearchItem){
       arrayList.clear();

        Log.e("get word" , "open" );
        wordTranslation = null;
        Cursor cursor = retrieve(SearchItem);
        System.out.println(SearchItem);

        while (cursor.moveToNext()){
            String word = cursor.getString(0);
            String translation = cursor.getString(1);
            WordTranslation wordTranslation1 = new        WordTranslation(word,translation);
            arrayList.add(wordTranslation1);
        }

        ArrayAdapter<WordTranslation> adapter = new Adapter(this, R.layout.activity_list_view_item,arrayList );
        listView.setAdapter(adapter);
        Log.e("get word" , arrayList.toString() );

        return arrayList;
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.ListView);
        listView.setTextFilterEnabled(true);
        sv = (SearchView) findViewById(R.id.search_view);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        databaseAccess.getQuotes();
        quotes = databaseAccess.ShowDictionaryDetails();
        databaseAccess.close();
        ArrayAdapter<String> adapter = new Adapter(this,    R.layout.activity_list_view_item, quotes);
        listView.setAdapter(adapter);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(MainActivity.this, "search loading .....", Toast.LENGTH_LONG).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
               getwordtranslation(newText);

                listView.clearTextFilter();


                return false;
            }
        });
    }    

    public Cursor retrieve(String name){
        DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this);
        SQLiteDatabase data =databaseOpenHelper.getReadableDatabase();
        String table = "quotes";
        String where = "word = ? ";
        String [] whereargs = {name};
        Log.e("retrieve", whereargs.toString());
        return data.query(table , null ,where , whereargs ,null, null, null );
    }
}

记录活动

E/get word: open I/SQLiteAssetHelper: successfully opened database quotes.db      E/retrieve: [Ljava.lang.String;@408fb608 E/get word: [] E/get word: open I/SQLiteAssetHelper: successfully opened database quotes.db E/retrieve: [Ljava.lang.String;@40a6f900 E/get word: [] E/get word: open I/SQLiteAssetHelper: successfully opened database quotes.db E/retrieve: [Ljava.lang.String;@40a748b0 E/get word: [] 

1 个答案:

答案 0 :(得分:0)

这是因为你甚至过滤了空字符串。如果SearchView变为空,则retrieve()将接收空字符串作为参数,并且您的数据库查询如下所示:

SELECT * FROM quotes WHERE word = ''

看起来没有单词等于空字符串的记录。

要解决此问题,请在retrieve()中检查name是否为空,然后选择所有数据,而不是WHERE