SearchView 和listView结合使用实现时时刻刻展现搜索结果,实现litePal实现模糊搜索

5 篇文章 0 订阅
4 篇文章 0 订阅

1号代码必须有,如果没有这个方法,搜索结果不会实时更新,最终按了键盘上的search按钮(相当于确认按钮,回车)才会更新搜索结果。
2号代码就是以LitePal的方式实现select * from table-name where field-name like %String% ; 同样的效果。

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

            @Override
            public boolean onQueryTextChange(String newText) {
                if(newText.isEmpty()){
  	                  taskAdapter.initList();
1.                    taskAdapter.notifyDataSetChanged();
                }else {
2.                    taskAdapter.list = DataSupport.where("name like ?","%"+newText+"%").find(Task.class);
1.                    taskAdapter.notifyDataSetChanged();
                }
                return false;
            }
        });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,这里是一个简单的示例代码,用于演示如何使用SearchViewListView实现搜索功能,并在ListView中点击其中一项跳转到其页面。 首先,我们需要在布局文件中定义一个SearchView和一个ListView,如下所示: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 接下来,我们需要在Activity中实现SearchView的监听器,并在onQueryTextSubmit方法中执行搜索逻辑。在这个示例代码中,我们将搜索结果保存在一个ArrayList中,并使用一个自定义的Adapter来显示ListView中的数据。 ``` public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener { private ListView listView; private SearchView searchView; private MyAdapter adapter; private ArrayList<Item> itemList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); searchView = findViewById(R.id.searchView); itemList = new ArrayList<>(); itemList.add(new Item("Apple", "https://www.apple.com")); itemList.add(new Item("Google", "https://www.google.com")); itemList.add(new Item("Microsoft", "https://www.microsoft.com")); itemList.add(new Item("Facebook", "https://www.facebook.com")); adapter = new MyAdapter(this, itemList); listView.setAdapter(adapter); searchView.setOnQueryTextListener(this); } @Override public boolean onQueryTextSubmit(String query) { ArrayList<Item> tempList = new ArrayList<>(); for (Item item : itemList) { if (item.getName().toLowerCase().contains(query.toLowerCase())) { tempList.add(item); } } adapter.setData(tempList); return false; } @Override public boolean onQueryTextChange(String newText) { return false; } } ``` 接下来,我们需要实现一个自定义的Adapter来显示ListView中的数据,并在其中实现ListView的点击事件,以便在用户点击其中一项时跳转到其页面。 ``` public class MyAdapter extends BaseAdapter { private Context context; private ArrayList<Item> itemList; public MyAdapter(Context context, ArrayList<Item> itemList) { this.context = context; this.itemList = itemList; } public void setData(ArrayList<Item> itemList) { this.itemList = itemList; notifyDataSetChanged(); } @Override public int getCount() { return itemList.size(); } @Override public Object getItem(int position) { return itemList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.nameTextView = convertView.findViewById(R.id.nameTextView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final Item item = itemList.get(position); holder.nameTextView.setText(item.getName()); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(context, DetailActivity.class); intent.putExtra("url", item.getUrl()); context.startActivity(intent); } }); return convertView; } private static class ViewHolder { TextView nameTextView; } } ``` 在这个示例代码中,我们实现了一个自定义的Adapter,其中getView方法中设置了ListView的点击事件。当用户点击其中一项时,我们将其URL作为Extra传递给DetailActivity,并启动该Activity。 最后,我们需要创建一个DetailActivity来显示用户点击的项目详细信息。在这个示例代码中,我们使用一个WebView来加载该项目的URL。 ``` public class DetailActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); webView = findViewById(R.id.webView); String url = getIntent().getStringExtra("url"); webView.loadUrl(url); } } ``` 希望这个示例代码可以帮助你实现你的功能
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Sadam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值