我有一个 3 列的 Listview,其中包含来自 SQLite DB 的数据和一个 seachview window。我创建了一个自定义数据。搜索未正确执行
I have a 3 column Listview with data from SQLite DB and a seachview window. i created a custom data. The search is not properly executed
我创建了一个多列 ListView
,上面有一个 SearchView
。它从 SQLite 数据库中提取数据并显示在列表视图中。我创建了自定义 ArrayAdapter
和 java class。但是,我的 SearchView
在我的 3 列自定义数组列表视图上的输出没有返回正确的数据,即它没有正确过滤。我已尽一切努力弄清楚可能是什么问题,并且在过去 3 天里一直在处理这个问题。我正在使用 QueryTextChangeListerner
。感谢您的帮助,因为我的应用程序开发遇到了瓶颈。
MainActivity.java:
mSearchView = (SearchView) findViewById(R.id.search_view);
listView = (ListView) findViewById(R.id.list_view);
controller = new ResturantsDbHelper(this);
userList = new ArrayList<>();
Cursor resturantdata = controller.getResturantList();
int numRows =resturantdata.getCount();
if (numRows == 0) {
Toast.makeText(SearchResturantListingX.this,"No Resturants matching your selection criteria",Toast.LENGTH_LONG).show();
}
else {
while (resturantdata.moveToNext()) {
user = new User(resturantdata.getString(1),resturantdata.getString(2),resturantdata.getString(3),resturantdata.getString(4),resturantdata.getString(5));
userList.add(user);
}
//FINISH Populating the array variable with the string data from SQLIte database
adapter = new FiveColumn_ListAdapter(this,R.layout.activity_search_main3_resturant_list,userList);
listView.setAdapter(adapter);
}
listView.setTextFilterEnabled(true);
setupSearchView();
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here By Name");
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
}
这是我的 Java class,它定义了我的 ListItems:
public class User {
private String resturantName;
private String resturantType;
private String resturantLocation;
public User (String resturantName, String resturantType, String resturantLocation, String rOpening, String rClosing){
super();
this.resturantName = resturantName;
this.resturantType = resturantType;
this.resturantLocation = resturantLocation;
}
public String getResturantName() {
return resturantName;
}
public void setResturantName (String resturantName) {
this.resturantName = resturantName;
}
public String getResturantType() {
return resturantType;
}
public void setType (String resturantType) {
this.resturantType = resturantType;
}
public String getResturantLocation() {
return resturantLocation;
}
public void setLocation (String resturantLocation) {
this.resturantLocation = resturantLocation;
}
public User(String resturantName, String type, String location) {
super();
this.resturantName = resturantName;
}
@Override
public String toString() {
return resturantName + " ";
}
}
这是我的自定义适配器:
public class FiveColumn_ListAdapter extends ArrayAdapter<User> {
private LayoutInflater mInflater;
private ArrayList<User> users;
private int mViewResourceId;
public FiveColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<User> users) {
super(context,textViewResourceId,users);
this.users = users;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents) {
convertView = mInflater.inflate(mViewResourceId,null);
User user =users.get(position);
if (user != null) {
TextView name=(TextView)convertView.findViewById(R.id.resturantName);
TextView type=(TextView)convertView.findViewById(R.id.resturantType);
TextView location=(TextView)convertView.findViewById(R.id.resturantLocation);
if(name !=null) {
name.setText((user.getResturantName()));
}
if(type !=null) {
type.setText((user.getResturantType()));
}
if(location !=null) {
location.setText((user.getResturantLocation()));
}
}
return convertView;
}
}
首先您需要更改项目集合,以便始终排序。这个例子之一。
private Set<User> users;
.....
users = new SortedSet<>();
将 User
对象的实现更改为包含 Comparable
接口。对于前。下面。
class Movie implements Comparable<Movie>
{
private double rating;
private String name;
private int year;
// Used to sort movies by year
public int compareTo(Movie m)
{
return this.year - m.year;
}
// Constructor
public Movie(String nm, double rt, int yr)
{
this.name = nm;
this.rating = rt;
this.year = yr;
}
// Getter methods for accessing private data
public double getRating() { return rating; }
public String getName() { return name; }
public int getYear() { return year; }
}
然后您可以配置所有排序,正如您在上述方法中所期望的那样compareTo
。查看有关 Comparable 界面的更多信息。
FiveColumn_ListAdapter
必须实施 android.widget.Filterable
然后将这些代码添加到 FiveColumn_ListAdapter
:
public class FiveColumn_ListAdapter extends ArrayAdapter<User>
implements android.widget.Filterable {
private LayoutInflater mInflater;
private ArrayList<User> users;// this is filtered users a subset of AllUsers
private int mViewResourceId;
private ArrayList<User> AllUsers;// AllUsers is users without any filter
public FiveColumn_ListAdapter(Context context, int textViewResourceId,
ArrayList<User> users, String initialQuery) {
super(context,textViewResourceId,users);
this.AllUsers = users;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
getFilter().filter(initialQuery);// perform fiter to populate users filter may be empty String
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence query) {
FilterResults filterResults = new FilterResults();
if(AllUsers == null || AllUsers.size() == 0){
filterResults.count = 0;
filterResults.values = null;
return filterResults;
}
ArrayList<User> results = new ArrayList<>();
for (User user : AllUsers) {
if(query.toString().equals(user.getResturantName())// do any filtering here
results.add(user);
}
filterResults.count = results.size();
filterResults.values = results;
return filterResults;
}
///////////////////////////////
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
users = results.values//
}
};
}
我创建了一个多列 ListView
,上面有一个 SearchView
。它从 SQLite 数据库中提取数据并显示在列表视图中。我创建了自定义 ArrayAdapter
和 java class。但是,我的 SearchView
在我的 3 列自定义数组列表视图上的输出没有返回正确的数据,即它没有正确过滤。我已尽一切努力弄清楚可能是什么问题,并且在过去 3 天里一直在处理这个问题。我正在使用 QueryTextChangeListerner
。感谢您的帮助,因为我的应用程序开发遇到了瓶颈。
MainActivity.java:
mSearchView = (SearchView) findViewById(R.id.search_view);
listView = (ListView) findViewById(R.id.list_view);
controller = new ResturantsDbHelper(this);
userList = new ArrayList<>();
Cursor resturantdata = controller.getResturantList();
int numRows =resturantdata.getCount();
if (numRows == 0) {
Toast.makeText(SearchResturantListingX.this,"No Resturants matching your selection criteria",Toast.LENGTH_LONG).show();
}
else {
while (resturantdata.moveToNext()) {
user = new User(resturantdata.getString(1),resturantdata.getString(2),resturantdata.getString(3),resturantdata.getString(4),resturantdata.getString(5));
userList.add(user);
}
//FINISH Populating the array variable with the string data from SQLIte database
adapter = new FiveColumn_ListAdapter(this,R.layout.activity_search_main3_resturant_list,userList);
listView.setAdapter(adapter);
}
listView.setTextFilterEnabled(true);
setupSearchView();
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here By Name");
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
}
这是我的 Java class,它定义了我的 ListItems:
public class User {
private String resturantName;
private String resturantType;
private String resturantLocation;
public User (String resturantName, String resturantType, String resturantLocation, String rOpening, String rClosing){
super();
this.resturantName = resturantName;
this.resturantType = resturantType;
this.resturantLocation = resturantLocation;
}
public String getResturantName() {
return resturantName;
}
public void setResturantName (String resturantName) {
this.resturantName = resturantName;
}
public String getResturantType() {
return resturantType;
}
public void setType (String resturantType) {
this.resturantType = resturantType;
}
public String getResturantLocation() {
return resturantLocation;
}
public void setLocation (String resturantLocation) {
this.resturantLocation = resturantLocation;
}
public User(String resturantName, String type, String location) {
super();
this.resturantName = resturantName;
}
@Override
public String toString() {
return resturantName + " ";
}
}
这是我的自定义适配器:
public class FiveColumn_ListAdapter extends ArrayAdapter<User> {
private LayoutInflater mInflater;
private ArrayList<User> users;
private int mViewResourceId;
public FiveColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<User> users) {
super(context,textViewResourceId,users);
this.users = users;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents) {
convertView = mInflater.inflate(mViewResourceId,null);
User user =users.get(position);
if (user != null) {
TextView name=(TextView)convertView.findViewById(R.id.resturantName);
TextView type=(TextView)convertView.findViewById(R.id.resturantType);
TextView location=(TextView)convertView.findViewById(R.id.resturantLocation);
if(name !=null) {
name.setText((user.getResturantName()));
}
if(type !=null) {
type.setText((user.getResturantType()));
}
if(location !=null) {
location.setText((user.getResturantLocation()));
}
}
return convertView;
}
}
首先您需要更改项目集合,以便始终排序。这个例子之一。
private Set<User> users; ..... users = new SortedSet<>();
将
User
对象的实现更改为包含Comparable
接口。对于前。下面。class Movie implements Comparable<Movie> { private double rating; private String name; private int year; // Used to sort movies by year public int compareTo(Movie m) { return this.year - m.year; } // Constructor public Movie(String nm, double rt, int yr) { this.name = nm; this.rating = rt; this.year = yr; } // Getter methods for accessing private data public double getRating() { return rating; } public String getName() { return name; } public int getYear() { return year; }
}
然后您可以配置所有排序,正如您在上述方法中所期望的那样compareTo
。查看有关 Comparable 界面的更多信息。
FiveColumn_ListAdapter
必须实施 android.widget.Filterable
然后将这些代码添加到 FiveColumn_ListAdapter
:
public class FiveColumn_ListAdapter extends ArrayAdapter<User>
implements android.widget.Filterable {
private LayoutInflater mInflater;
private ArrayList<User> users;// this is filtered users a subset of AllUsers
private int mViewResourceId;
private ArrayList<User> AllUsers;// AllUsers is users without any filter
public FiveColumn_ListAdapter(Context context, int textViewResourceId,
ArrayList<User> users, String initialQuery) {
super(context,textViewResourceId,users);
this.AllUsers = users;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
getFilter().filter(initialQuery);// perform fiter to populate users filter may be empty String
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence query) {
FilterResults filterResults = new FilterResults();
if(AllUsers == null || AllUsers.size() == 0){
filterResults.count = 0;
filterResults.values = null;
return filterResults;
}
ArrayList<User> results = new ArrayList<>();
for (User user : AllUsers) {
if(query.toString().equals(user.getResturantName())// do any filtering here
results.add(user);
}
filterResults.count = results.size();
filterResults.values = results;
return filterResults;
}
///////////////////////////////
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
users = results.values//
}
};
}