Android自定义列表视图,图片和子项搜索视图无法正常工作

时间:2016-06-13 13:38:21

标签: java android listview searchview

我有一个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 enter image description here

中输入内容时的结果

我想过滤resName,resLoc,resType但没有过滤器, 看看适配器下的图片&gt; mObjects 0,1,2,3,4,5是带有文本的listview项。我想过滤它

3 个答案:

答案 0 :(得分:1)

你要过滤哪个属性,你可以根据它在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;
        }
    });

并在您的活动中

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) {
            }
        });

答案 1 :(得分:0)

您好Allen可以尝试下面的内容,并且可以根据您的搜索要求修改过滤器类中的搜索逻辑。在下面的代码中,如果字符串以字符序列开头,则会将字符串视为结果。

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();
}
}

答案 2 :(得分:0)

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

从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);