我有一个searchView
我要过滤的resName,resLoc,resType,但遗憾的是它不工作Android的自定义列表查看图片和子项搜索视图不工作
public class caloocan extends AppCompatActivity {
String FIREBASE_URL = "https://restaulist1.firebaseio.com";
Firebase firebaseRef;
private List<caloocanDB> Restau = new ArrayList<>();
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloocan);
Firebase.setAndroidContext(this);
firebaseRef = new Firebase(FIREBASE_URL);
searchView = (SearchView) findViewById(R.id.searchView);
// populateListView();
// populateRestauList();
final MyListAdapter adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.listViewRest);
//populate restau List
firebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String[] resName = new String[(int) dataSnapshot.getChildrenCount()];
String[] resLoc = new String[(int) dataSnapshot.getChildrenCount()];
String[] resType = new String[(int) dataSnapshot.getChildrenCount()];
int i = 0;
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
resName[i] = dataSnapshot1.child("resname").getValue().toString();
resLoc[i] = dataSnapshot1.child("resloc").getValue().toString();
resType[i] = dataSnapshot1.child("foodtype").getValue().toString();
Restau.add(new caloocanDB(resName[i], resLoc[i], R.drawable.six, resType[i]));
i++;
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
list.setAdapter(adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
public class MyListAdapter extends ArrayAdapter<caloocanDB> {
public ArrayList<caloocanDB> tempRestList = new ArrayList<>();
public MyListAdapter(){
super(caloocan.this, R.layout.caloocan_list_view, Restau);
tempRestList = new ArrayList<caloocanDB>(Restau);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View caloocanView = convertView;
if (caloocanView == null)
caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);
caloocanDB restaurant = Restau.get(position);
//FILL VIEW
ImageView imageView = (ImageView)caloocanView.findViewById(R.id.imageView);
imageView.setImageResource(restaurant.getIconID());
// RESTAU NAME
TextView restauName = (TextView)caloocanView.findViewById(R.id.resnameTxt);
restauName.setText(restaurant.getResname());
//RESTAU LOCA
TextView location = (TextView)caloocanView.findViewById(R.id.reslocTxt);
location.setText(restaurant.getResloc());
//FOOD TYPE
TextView restype = (TextView)caloocanView.findViewById(R.id.restypeTxt);
restype.setText(restaurant.getType());
return caloocanView;
}
public void filter(String text) {
Restau.clear();
for (caloocanDB element : tempRestList) {
if (element.getResname().toLowerCase().startsWith(text) || element.getType().toLowerCase().startsWith(text) || element.getResloc().toLowerCase().startsWith(text)){
Restau.add(element);
}
}
super.notifyDataSetChanged();
}
}
}
我adapter.filter();
添加断点,这是当我在搜索中输入一些东西时的结果查看
我想过滤resName,resLoc,resType但没有过滤器,查看适配器下的图片> mObjects将0,1,2,3,4, 5是我的带有文本的列表视图项目。我想筛选就
得到的数据可以发布您的适配器代码后? – himanshu1496
检查更新的@ himanshu1496 –
,你能告诉你想要过滤列表的caloocanDB对象的哪个元素? – himanshu1496