Android 带有图片和子项搜索视图的自定义列表视图不起作用

Android Custom List View with pictures and sub items search view not working

我有一个 searchView 我想过滤 resName、resLoc、resType,但不幸的是它不起作用

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();中添加了断点 这是我在 searchView

中输入内容时的结果

我想过滤 resName、resLoc、resType 但没有过滤, 查看适配器> mObjects 下的图片,0、1、2、3、4、5 是我的带有文本的列表视图项目。我想过滤它

你属性是通过哪个来过滤的,你可以根据那个在Adapter中写一个方法,比如你用餐厅名过滤,然后写下面的代码

private class MyListAdapter extends ArrayAdapter<caloocanDB> {
        public static ArrayList<coloocanDB> tempRestList = new ArrayList<>();
        public static ArrayList<coloocanDB> restList = new ArrayList<>();
        public MyListAdapter(ArrayList<caloocanDB> objects){
            super(caloocan.this, R.layout.caloocan_list_view, objects);        tempRestList = new ArrayList<>();
            restList = objects;
            tempRestList.addAll(objects);
        }

        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 = restList.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 filter) {
      restList.clear();
      if(filter != null && filter.trim().length() > 0){
        for (caloocanDB element : tempRestList){
          if (element.getResname().contains(filter) || element.getType().contains(filter) || element.getResLoc().contains(filter))
             restList.add(element);
          }
      }else{
             restList.addAll(tempRestList);
      } 
      super.notifyDataSetChanged();
   }
}

并且在你的 searchView 监听器中

   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String text) {
            adapter.filter(text);
            return false;
        }
    });

在你的Activity

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++;
                }
        final MyListAdapter adapter = new MyListAdapter(Restau);
        ListView list = (ListView) findViewById(R.id.listViewRest);
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });

你好,Allen,你可以试试下面的方法,过滤器 class 中的搜索逻辑可以根据你的搜索要求进行修改,在下面的代码中,搜索将把以这些开头的字符串视为结果字符序列。

private class MyListAdapter extends ArrayAdapter<caloocanDB> {
public static ArrayList<coloocanDB> tempRestList = new ArrayList<>();
public MyListAdapter(){
    super(caloocan.this, R.layout.caloocan_list_view, Restau);              tempRestList = 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();
}
}

您正在将适配器初始化为空,因此未过滤任何内容。

将初始化更改为在从 Firebase 获取数据后完成

final ListView list = (ListView) findViewById(R.id.listViewRest);
    //populate restau List

//inside the dataobserver
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++;
}
MyListAdapter adapter = new MyListAdapter(Restau);
list.setAdapter(adapter);