2015-07-10 111 views
0

好了,所以,我有以下的ListView适配器:过滤的ListView与搜索查看

ListViewAdapter.java

public class ListViewAdapter extends CursorAdapter{ 
    public ListViewAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 


    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.listitem, parent, false); 
    } 


    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // Find fields to populate in inflated template 
     Button btnList = (Button) view.findViewById(R.id.btnList); 
     // Populate fields with cursor 
     btnList.setText(cursor.getString(0)); 

    } 
} 

这里是我的充气XML:

列表项。 xml

<?xml version="1.0" encoding="utf-8"?> 
<Button xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/btnList" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/listview_item" 
android:focusable="false" 
android:clickable="false" 
android:focusableInTouchMode="false"/> 

然后我使用它在这里给ListView的光标的内容:

MainActivity.java

ListViewAdapter lvAdapter = new ListViewAdapter(this, cursor); 
listView.setAdapter(lvAdapter); 

现在,如何将一个用搜索查看过滤的ListView每次我改变searchView文本? (像onTextChange什么的)?

我想出这个至今:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      todoAdapter.getFilter().filter(newText); 
      return true; 
     } 
}); 

但它不会做任何事......如果您需要任何更多信息,随意问:)

我的布局: My current Layout.

回答

0

我认为你应该使用这个 这是我的项目,我目前正在 下面部分是二的ArrayList一个showPr ojectList和另一个项目列表 projectlist包含您要过滤的实际数据,并且showprojectlist包含来自projectlist的数据,该数据将被过滤并显示。

searchview.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 

       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        if (searchview.getText().toString().length() == 0) { 
         showProjectList = projectList; 
         ProjectAdapter adapter = new ProjectAdapter(getInstance(), showProjectList); 
         projectListView.setAdapter(adapter); 

         noOfProjectFoundTextView.setText(showProjectList.size() + ""); 
         noProjectFoundLayout.setVisibility(View.GONE); 

        } else { 
         showProjectList = new ArrayList<ProjectModel>(); 
         showProjectList.clear(); 
         for (int i = 0; i < projectList.size(); i++) { 
          if (projectList.get(i).getProjectName() 
            .toString() 
            .toLowerCase() 
            .startsWith(
              searchview.getText().toString().toLowerCase()) || projectList.get(i).getProjectName() 
            .toString() 
            .toLowerCase() 
            .contains(
              searchview.getText().toString().toLowerCase())) { 

           ProjectModel mData = new ProjectModel(); 
           mData.projectId = projectList.get(i).getProjectId(); 
           mData.itemCount = projectList.get(i).getItemCount(); 
           mData.projectName = projectList.get(i).getProjectName(); 
           mData.createdDate = projectList.get(i).createdDate; 
           mData.lInfoList = projectList.get(i).getlInfoList(); 

           showProjectList.add(mData); 


          } 
         } 

         ProjectAdapter adapter = new ProjectAdapter(getInstance(), showProjectList); 
         projectListView.setAdapter(adapter); 

        } 

       } 
      });//search ended 
0

好吧,让我们说你有一个名为mainArrayList的数组列表。所以,现在你想要从中搜索某些东西。 创建一个方法(让我说mySearch(String str))并传递搜索字符串作为它的参数。以全局声明新的ArrayList,循环遍历mainArrayList &检查maiArrayList项是否包含字符串,如果mainArrayList包含该字符串,则在新的ArrayList中添加该项并在listView中使用新的ArrayList。

还,添加新的实现方法MYSEARCH(字符串str)内

@Override 
    public boolean onQueryTextChange(String newText) 
    { mySearch(newText); 

     return true; 
    }