2016-06-13 96 views
0

我有一个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(); 添加断点,这是当我在搜索中输入一些东西时的结果查看enter image description here

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

+0

得到的数据可以发布您的适配器代码后? – himanshu1496

+0

检查更新的@ himanshu1496 –

+0

,你能告诉你想要过滤列表的caloocanDB对象的哪个元素? – himanshu1496

回答

1

通过哪个属性是你过滤,可以根据该写在适配器的方法,例如用餐厅的名字您的过滤,然后下面写代码

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

我想搜索resName,resLoc,resType。 –

+0

当我把'adapter.filter(文本);'它不读取任何变量或方法时,我得到一个错误。因为我在'public view getView'后插入'public void filter'' –

+0

可以发布logcat 吗? –

0

你好艾伦你可以尝试下面的东西,并且可以根据你的搜索要求修改过滤器类中的搜索逻辑。在下面的代码中,如果字符序列以字符序列开始,搜索将认为字符串是结果。

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

嗨,感谢您的想法,但约束不能解决符号。 –

+0

是的,我忘了改变这个变量,请检查我更新的答案! – himanshu1496

0

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

更改您的初始化工作要做,你从火力地堡

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