我想使用SearchView小部件来过滤数据。 由于我切换到Listview的自定义布局,因此过滤无效。 ListView充满了我拥有的所有歌曲,但在将某些内容输入SearchView时不会更新。我只想让SearchView适用于Custom ListView。如果你有任何有用的链接请与我分享。
搜索类:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SearchView;
import java.util.ArrayList;
import univie.g02.t06.tmsd.dummydata.DummyAPIData;
import univie.g02.t06.tmsd.dummydata.DummySong;
import univie.g02.t06.tmsd.MyAdapter;
public class Search extends AppCompatActivity {
ListView listV;
ArrayList<String> listItems = new ArrayList<String>();
ArrayList<String> titles = new ArrayList<String>();
ArrayList<DummySong> listSongs = new ArrayList<DummySong>();
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listV = (ListView) findViewById(R.id.list_view);
SearchView searchView = (SearchView) findViewById(R.id.search_view);
listSongs = DummyAPIData.getAllDummySongs();
for (int i = 0; i < listSongs.size(); i++) {
listItems.add(listSongs.get(i).getDummyArtistTitle());
titles.add(listSongs.get(i).getDummyTitle());
}
adapter = new MyAdapter(this,
android.R.layout.simple_list_item_1,
listItems);
listV.setAdapter(adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
listV.setAdapter(adapter);
return true;
}
@Override
public boolean onQueryTextSubmit(String query){
return true;
}
});
}
}
MyAdapter类:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends ArrayAdapter implements Filterable {
private List<String> list;
ArrayList<String> origData = new ArrayList<String>();
private Context context;
public MyAdapter(Context context, int resources, ArrayList<String> list) {
super(context, resources, list);
this.list = list;
this.context = context;
this.origData = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.customlayout, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
}
customlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp" />
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="add" />
</RelativeLayout>
activitysearch.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="univie.g02.t06.tmsd.Search">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/search_view" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
SearchView searchView =(SearchView) findViewById(R.id.searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
adapterQuote.getFilter().filter(s);
listView.setAdapter(adapterQuote);
return true;
}
@Override
public boolean onQueryTextChange(String s) {
adapterQuote.getFilter().filter(s);
return false;
}
});