真的很麻烦。我只希望能够按文本过滤。这是带有listView的活动:
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
public class HomeOwnerActivity extends AppCompatActivity {
private ExpandableListView listView;
private HomeOwnerAdapter serviceAdapter;
private FullService service;
private ArrayList<FullService> services;
private ArrayList<String> names = new ArrayList<>();
private HashMap<FullService,ArrayList<String>> listHashMap;
private DatabaseReference databaseUsers;
private HomeOwnerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_owner);
EditText theFilter = (EditText) findViewById(R.id.search_box);
databaseUsers = FirebaseDatabase.getInstance().getReference("services");
listView = (ExpandableListView)findViewById(R.id.allList);
services = new ArrayList<>();
listHashMap = new HashMap<>();
serviceAdapter = new HomeOwnerAdapter(HomeOwnerActivity.this, services, listHashMap);
listView.setAdapter(serviceAdapter);
theFilter.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) {
(HomeOwnerActivity.this).adapter.getFilter().filter();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
protected void onStart(){
super.onStart();
databaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()){
FullService service = postSnapshot.getValue(FullService.class);
services.add(service);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
GetFilter()当然由于自定义arrayadapter而无法工作。 这是我的适配器类,我添加了Filterable实现,但是我不确定从这里开始。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;;
public class ServiceAdapter extends ArrayAdapter<Service> implements Filterable {
private final Context context;
private final ArrayList<Service> services;
public ServiceAdapter(Context context, ArrayList<Service> values) {
super(context, R.layout.list_item_layout, values);
this.context = context;
this.services = values;
}
public View getView(int position, View convertView, ViewGroup parent) {
//Getting Recipe
Service curService = services.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_layout, parent, false);
TextView serviceName = (TextView) rowView.findViewById(R.id.itemName);
TextView hourlyRate = (TextView) rowView.findViewById(R.id.itemDescription);
//Placing content into recipe List Item
serviceName.setText(curService.getService());
hourlyRate.setText((curService.getRate()));
return rowView;
}
@Override
public void add(Service newService) {
services.add(newService);
notifyDataSetChanged();
}
public void delete(Service currentService) {
services.remove(currentService);
notifyDataSetChanged();
}
}
这是我的布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ExpandableListView
android:id="@+id/allList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
我读过的很多文章都使用BaseAdapter,目前我不能切换到那个。有什么方法可以实现getFilter()并使它正常工作吗?