I am able to make a search filter for a listview with single input but am not able to do it for my custom layout which has 3 values. When using the filter I have created all the text disappears from my list view. I would like to be able to filter any of the input be it FirstName, LastName or FavouriteFood.
(ViewListContents.this).adapter.getFilter().filter(charSequence);
Using this code didn't work.
ViewListContents class:
DatabaseHelper myDB;
ArrayList<User> userList;
ListView listView;
User user;
ThreeColumn_ListAdapter adapter;
private List<User> userL = null;
private ArrayList<User> arraylist;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontents_layout);
myDB = new DatabaseHelper(this);
final EditText filter = (EditText) findViewById(R.id.searchFilter);
userList = new ArrayList<>();
Cursor data = myDB.getListContents();
int numRows = data.getCount();
if(numRows == 0){
Toast.makeText(ViewListContents.this,"The Database is empty :(.",Toast.LENGTH_LONG).show();
}else{
int i=0;
while(data.moveToNext()){
user = new User(data.getString(1),data.getString(2),data.getString(3));
userList.add(i,user);
System.out.println(data.getString(1)+" "+data.getString(2)+" "+data.getString(3));
System.out.println(userList.get(i).getFirstName());
i++;
}
adapter = new ThreeColumn_ListAdapter(this,R.layout.list_adapter_view, userList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
//listView.setTextFilterEnabled(true);
}
filter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
(ViewListContents.this).adapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
User class:
private String FirstName;
private String LastName;
private String FavFood;
public User(String fName,String lName, String fFood){
FirstName = fName;
LastName = lName;
FavFood = fFood;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getFavFood() {
return FavFood;
}
public void setFavFood(String favFood) {
FavFood = favFood;
}
Custom Adapter Class:
private LayoutInflater mInflater;
private ArrayList<User> users;
private int mViewResourceId;
public ThreeColumn_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 parent) {
convertView = mInflater.inflate(mViewResourceId, null);
User user = users.get(position);
if (user != null) {
TextView firstName = (TextView) convertView.findViewById(R.id.textFirstName);
TextView lastName = (TextView) convertView.findViewById(R.id.textLastName);
TextView favFood = (TextView) convertView.findViewById(R.id.textFavFood);
if (firstName != null) {
firstName.setText(user.getFirstName());
}
if (lastName != null) {
lastName.setText((user.getLastName()));
}
if (favFood != null) {
favFood.setText((user.getFavFood()));
}
}
return convertView;
}
You have a method with the name of filter (from your adapter class that extends from BaseAdapter). In this method (that returns void) when checking of every data item , instead of check for hole of "input text" do check for every part of "input text" by a simple loop. In this order you could implement a "logical or" checking for existence of each part. When a part includes in the data item , you could stop searching(Ok, this item in true for displaying) and ... .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.