【问题标题】:How to set Filter to ListView with CursorAdapter?如何使用 CursorAdapter 将 Filter 设置为 ListView?
【发布时间】:2015-09-10 15:54:31
【问题描述】:

我有一个ListView。其中哪些项目来自自定义CursorAdapter。我为 CardView 的 ListView 项目创建了单独的布局。每张卡上有六个TextViews,它们是Database 值。现在我必须放置一个EditText 来根据在 EditText 中输入的文本过滤 ListView 项目。 我的代码是 ListView 的布局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1d4563">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView2"
    android:layout_marginTop="40dp"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText5"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/bg_edit"
    android:textColorHint="#d3d3d3"
    android:hint="enter Serial Number to search" />

ListView 项目布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android.support.v7.cardview:cardBackgroundColor="@color/primary_dark"
android:id="@+id/cardItems">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_margin="1dp"
    card_view:cardCornerRadius="0dp"
    card_view:contentPadding="2dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/card_style">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Username"
            android:id="@+id/textView24"
            android:textColor="#FFFFFF"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_marginTop="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mobile No."
            android:id="@+id/textView25"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_alignRight="@+id/textView24"
            android:layout_alignEnd="@+id/textView24" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Serial No."
            android:id="@+id/textView26"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView27"
            android:textColor="#FFFFFF"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView28"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/textView27"
            android:layout_alignStart="@+id/textView27" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView29"
            android:layout_alignTop="@+id/textView26"
            android:layout_alignLeft="@+id/textView28"
            android:layout_alignStart="@+id/textView28"
            android:layout_marginBottom="8dp"
            android:textColor="#FFFFFF"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

我的DisplayAdapter

public class DisplayAdapter extends CursorAdapter{

@SuppressWarnings("deprecation")
public DisplayAdapter(Context context,Cursor c){
    super(context,c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.card_items, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvUserName=(TextView)view.findViewById(R.id.textView27);
    tvUserName.setText(cursor.getString(2));

    TextView tvMobile=(TextView)view.findViewById(R.id.textView28);
    tvMobile.setText(cursor.getString(3));

    TextView tvSerail=(TextView)view.findViewById(R.id.textView29);
    tvSerail.setText(cursor.getString(5));
}

}

我的活动是 public class TableBikeActivity extends ActionBarActivity {

private DisplayAdapter adapter;
ListView bikeList;
DatabaseHelper databaseHelper;
EditText filterText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_table_bike);

    databaseHelper=new DatabaseHelper(this);
    bikeList=(ListView)findViewById(R.id.listView2);

    filterText=(EditText)findViewById(R.id.editText5);
    filterText.addTextChangedListener(filterTextWatcher);
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getAllDataBike());
            bikeList.setAdapter(adapter);
        }
    });
}

private TextWatcher filterTextWatcher=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) {
        adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

@Override
protected void onDestroy(){
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

}

我已经尝试过 TextChangedListener 但它不起作用。帮帮我。

【问题讨论】:

  • 您必须使用FilterQueryProvider
  • 从数据库中过滤ListView我必须使用FilterQueryProvider?
  • 好的。我想。谢谢。
  • 更简单的解决方案:覆盖runQueryOnBackgroundThread
  • 你为什么不实现一个 SearchView 或搜索栏而不是一个过滤器。它可以在用户键入时为您提供搜索建议。在此处了解更多信息developer.android.com/guide/topics/search/index.html

标签: android android-layout listview android-listview custom-adapter


【解决方案1】:

setTextFilterEnabled() 方法不会自动实现过滤,因为它不知道您的 Cursor 中的文本应针对什么进行过滤。

对于CursorAdapter 游标,您只需要使用setFilterQueryProvider,根据约束为您的游标运行另一个查询:

m_Adapter.setFilterQueryProvider(new FilterQueryProvider() {

  public Cursor runQuery(CharSequence constraint) {
    Log.d(LOG_TAG, "runQuery constraint:"+constraint);
    //uri, projection, and sortOrder might be the same as previous
    //but you might want a new selection, based on your filter content (constraint)
    Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    return cur; //now your adapter will have the new filtered content
  }

});

当添加约束时(例如,通过使用 TextView),必须过滤适配器:

public void onTextChanged(CharSequence s, int start, int before, int count) {
  Log.d(LOG_TAG, "Filter:"+s);
  if (m_slvAdapter!=null) {
    m_Adapter.getFilter().filter(s);
  }
}

【讨论】:

    【解决方案2】:
           inputSearch.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                adapter_members.getFilter().filter(s.toString());
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
    
            }
    

    【讨论】:

    • 这个解决方案在这里不起作用,因为你有一个包含 textview 对象的光标适配器,而不是一个可以像这样轻松解析的字符串列表。
    【解决方案3】:

    这是我的方法,因为我找不到任何对我有帮助的东西。我不知道是否有更好的解决方案,但至少它是一个可行的解决方案。

    1. 首先,您需要在您的列表视图所在的布局中添加一个 EditText 元素,我们称之为 inputSearch。

    2. 在您的活动中声明一个 MatrixCursor 类型的全局变量,稍后将用于过滤结果 - 这个特殊的游标是一个游标,它可以通过 addRow() 等简单函数处理元素:

      MatrixCursor lastValidSearchResult;

    3. 在代码中的某个地方,您初始化光标的地方,您将使用它来提供光标适配器,将光标的结果也保存在lastValidSearchResult

        Cursor myCursor = myDbObj.doMyQuery();
        lastValidSearchResult = myCursor;
    
    1. 对于 EditText 元素,您可以像这样实现 addTextChangedListener 方法:
    inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // yourCustomCursorAdapter is the adapter for which you implemented a custom class - so in your case of type display adapter             
                YourCurrentActivity.this.yourCustomCursorAdapter.getFilter().filter(cs.toString());
                YourCurrentActivity.this.yourCustomCursorAdapter.notifyDataSetChanged();
            }
            
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
            @Override
            public void afterTextChanged(Editable arg0) {}
        });
    
    1. 最后但并非最不重要的代码是执行过滤器的代码 sn-p。为此,您需要实现自定义适配器(在本例中为来自 DisplayAdapter 的对象)的函数 setFilterQueryProvider 的代码:
        yourCustomCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
    
            // constraint is the variable which is passed from your text change listener
            public Cursor runQuery(CharSequence constraint) {
                // if the search string is empty, then return the original cursor with all results from the original query
                if(constraint.equals(""))
                {
                    return myCursor;
                }
                MatrixCursor filteredValues = new MatrixCursor(new String[]{/*Same columns as your original cursor myCursor*/);
    
                while(myCursor.moveToNext()) {
                    String smth = myCursor.getString(myCursor.getColumnIndex(validColumnIndex);
                    String smth2 = ....
                    //Some condition to check previous data is not matched and only then add row
                    if (smth.toLowerCase().contains(constraint.toString().toLowerCase())) {
                        filteredValues.addRow(new String[]{smth, ...});
                    }
                }
                // do not forget next line since you manipulated your original cursor with moveToNext
                yourCursor.moveToFirst();
    
                // If your search value was not found at all return the last valid search result
                if(filteredValues.getCount() == 0)
                {                   
                    return lastValidSearchResult;
                }
                // else you can save the new correct search result and return the results
                lastValidSearchResult = filteredValues;
                return filteredValues; //now your adapter will have the new filtered content
            }});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多