簡體   English   中英

帶有圖片和子項目的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();添加了斷點adapter.filter(); 這是當我在searchView中鍵入內容時的結果 在此處輸入圖片說明

我要過濾resName,resLoc,resType,但不過濾任何內容,請看適配器> mObjects下的圖片,其中0、1、2、3、4、5是帶有文本的listview項。 我想過濾

通過過濾哪個屬性,您可以根據它在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 Listener中

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

嗨,艾倫,您可以嘗試以下類似操作,並且可以根據您的搜索要求修改過濾器類中的搜索邏輯。在下面的代碼中,如果字符串以這些字符序列開頭,則將其視為結果。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM