使用android

时间:2015-10-28 03:41:10

标签: android listview android-listview adapter searchview

我是Android开发的新手,我正在尝试创建一个搜索界面,使用搜索小部件“SearchView”在同一活动中的列表视图中显示结果。我已经查看了android文档http://developer.android.com/intl/es/guide/topics/search/search-dialog.html#SearchableConfiguration,但我已经按照所有步骤和配置来实现在操作栏上添加搜索小部件的基础知识等等,但是文档没有提供有关如何查询要在列表视图上显示的数据。任何人都可以指导我完成如何执行此操作的完整教程。谢谢。

这是我想要实现的目标的打印屏幕:enter image description here

2 个答案:

答案 0 :(得分:1)

您可以将TextWatcher用于此

例如

ur_edittext.addTextChangedListener(new TextWatcher(){

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//Do your search algorithm here
}

@Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

答案 1 :(得分:0)

试试这种方式

public class MainActivity extends Activity {

    SearchView search;

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

        search=(SearchView) findViewById(R.id.searchView1);
        search.setQueryHint("SearchView");

        //*** setOnQueryTextFocusChangeListener ***
        search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
                Toast.LENGTH_SHORT).show();
        }
    });

        //*** setOnQueryTextListener ***
        search.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), query, 
                Toast.LENGTH_SHORT).show();

            return false;
        }

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

        //  Toast.makeText(getBaseContext(), newText,
                Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}