【问题标题】:setSuggestionAdapter cursor - need to update itsetSuggestionAdapter 游标 - 需要更新它
【发布时间】:2014-05-01 02:27:40
【问题描述】:

所以,我有这两行:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    super.onCreateOptionsMenu(menu);
    getSupportMenuInflater().inflate(R.menu.onime_main, menu);

    // Create the search view
    SearchView searchView = new SearchView(getSupportActionBar()
            .getThemedContext());
    searchView.setQueryHint("Search for anime");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);

    if (mSuggestionsAdapter == null) {
        cursor = new MatrixCursor(COLUMNS);

        cursor.addRow(new String[] { "1", "'Murica" });

        mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
                .getThemedContext(), cursor);
    }

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);

    menu.add("Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_IF_ROOM
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}

然后我还有其他功能:

@Override
public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub

    Log.e("test: ", newText);

    List<Serial> temp = db.getSuggestion(newText);

    MatrixCursor tempCur = new MatrixCursor(COLUMNS);

    for (Serial cn : temp) {
        tempCur.addRow(new String[] { cn.getID() + "", cn.getName() });
        Log.e("Info:", cn.getID() + " " + cn.getName());
    }

    cursor = tempCur;

    for (Serial cn : temp) {
        String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
                + " ,Description: " + cn.getDescription();
        // Writing Contacts to log
        Log.d("Name: ", log);
    }

    mSuggestionsAdapter.notifyDataSetChanged();

    return false;
}

想法很简单。我有“搜索框”,无论我输入什么都会有 newText。将发送 sql 查询,我将在其中获得所需的数据(用于自动完成)。问题是无论我尝试什么方法,我都无法更新“列表”。我可以将无限内容添加到光标,但旧的内容仍然存在,我不想要那个。我不知道如何解决它了:(。

如果您有任何问题,请随时提出,我会尽力解释或类似的。

【问题讨论】:

    标签: android search autocomplete actionbarsherlock


    【解决方案1】:

    修复它。基本上我重置了适配器,将新光标设置为主要内容。我之前尝试过,但它没有用,因为我没有将 searchView 设为全局(在课堂上)。使其成为全局后,我可以使用 searchView 并设置新的适配器,使其工作。换句话说:

    mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
                .getThemedContext(), tempCur);
    
        searchView.setSuggestionsAdapter(mSuggestionsAdapter);
    

    【讨论】:

      猜你喜欢
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 2011-09-07
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多