我有一個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