我已经在ListView中从Firebase数据库检索数据。它具有两个Textviews 1)学生姓名2)学生班级。现在,我想用Edit_Text过滤此ListView,也要用ListView过滤onClickListener,但是我无法做到这一点。当我在Edit_Text中键入内容时,整个列表为空。这是我的代码
StudentsInfoAdapter.java
public class StudentsInfoAdapter extends ArrayAdapter<Students> {
private Activity context;
private List<Students> studentsList;
public StudentsInfoAdapter(Activity context,List<Students>studentsList){
super(context,R.layout.list_view,studentsList);
this.context=context;
this.studentsList=studentsList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.list_view,null,true);
TextView studentName = listView.findViewById(R.id.name);
TextView studentClass = listView.findViewById(R.id.class_of_student);
Students student = studentsList.get(position);
studentName.setText(student.getStudentName());
studentClass.setText(student.getStudentClass());
return listView;
}
}
activity_data_retrieved.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".DataRetrieved">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Search"
android:id="@+id/inputSearch"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:divider="@android:color/transparent"
android:id="@+id/list_view"/>
</LinearLayout>
DataRetrieved.java
public class DataRetrieved extends AppCompatActivity {
private ListView listView;
DatabaseReference databaseReference;
List<Students> studentsList;
public EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inputSearch = findViewById(R.id.inputSearch);
setContentView(R.layout.activity_data_retrieved);
listView=findViewById(R.id.list_view);
databaseReference = FirebaseDatabase.getInstance().getReference("students");
studentsList = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
inputSearch = findViewById(R.id.inputSearch);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot studentSnapshot : dataSnapshot.getChildren()){
Students student = studentSnapshot.getValue(Students.class);
studentsList.add(student);
}
final StudentsInfoAdapter studentsInfoAdapter = new StudentsInfoAdapter(DataRetrieved.this,studentsList);
listView.setAdapter(studentsInfoAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
studentsInfoAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
List<Students> studentsList = new ArrayList<>();
List<Students > studentsListSearch = new ArrayList<>();// add this line
public StudentsInfoAdapter(Activity context,List<Students>studentsList){
super(context,R.layout.list_view,studentsList);
this.context=context;
this.studentsList=studentsList;
studentsListSearch.addAll(studentsList);//add this line
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().length()>0){
studentsList.clear();
String searchTxt = holder.tvExpLstAdptrSlNo.getText().toString();
for(String s: studentsListSearch){
if(s.toLowerCase().contains(searchTxt)){
studentsList.add(s);
}
}
studentsInfoAdapter.notifyChanged();
}
else{
studentsList.addAll(studentsListSearch);
studentsInfoAdapter.notifyChanged();
}
}