如何查看用户在列表视图中选择的列表中的项目?

时间:2017-01-19 09:48:50

标签: android listview

在此图像中,将向用户显示一个列表。用户可以搜索列表中的项目并单击它。如何记录用户点击的内容?

enter image description here

Flight_Book.java:

public class Flight_Book extends AppCompatActivity {
ListView lv;
SearchView sv;
String[] teams = {"Dog","Cat","Horse","Rabbit","Squirrel"};
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flight__book);

    lv=(ListView) findViewById(R.id.listview1);
    sv = (SearchView) findViewById(R.id.searchview1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teams);
    lv.setAdapter(adapter);

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);
            return false;
        }
    });


}

}

1 个答案:

答案 0 :(得分:0)

您的列表视图和searchView完全不同。如果你在谈论建议,

https://developer.android.com/guide/topics/search/adding-custom-suggestions.html

尝试上面链接中描述的方法,你会做得很好。