1

我有一個ListView。哪些是來自定製CursorAdapter。我爲ListView項目創建了單獨的佈局,這是一個CardView。我每個卡上有六個TextViews他們是Database值。現在,我必須根據在EditText中輸入的文本,將EditText過濾ListView項目。 我的代碼是 佈局的ListView是如何使用CursorAdapter將過濾器設置爲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但它不工作。幫我。

+0

您必須使用'FilterQueryProvider' – Skynet

+0

過濾從數據庫'ListView'我必須使用'FilterQueryProvider'? –

+0

答案是肯定的 – Skynet

回答

0

setTextFilterEnabled()方法不會自動實現過濾,因爲它不知道你的光標什麼文本應該針對被過濾。

對於CursorAdapter光標,你只需要使用setFilterQueryProvider,運行另一個查詢你的光標,基於約束:(例如,通過使用一個TextView)

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 
    } 

}); 

當限制添加適配器必須經過過濾:

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

     } 
相關問題