2017-06-04 30 views
1

我在這一個,在閱讀了幾篇文章後,我仍然不知道如何處理我目前的情況。但是,當我過濾我的光標適配器時,該列表不會更新或過濾任何東西,它就像什麼都沒有發生。適配器getFilter沒有過濾

這裏是我查看客戶

public class ViewClientActivity extends Activity 
{ 

EditText inputSearch; 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewclient_activity); 

    inputSearch = (EditText) findViewById(R.id.search_view); 

    DBHandler handler = new DBHandler(this, null, null, 1); 
    SQLiteDatabase db = handler.getReadableDatabase(); 
    final Cursor ClientCursor = db.rawQuery("SELECT * FROM clients", null); 
    final ListView allClients = (ListView) findViewById(R.id.allClients); 
    final ClientCursorAdapter clientAdapter = new ClientCursorAdapter(this, ClientCursor); 

    allClients.setAdapter(clientAdapter); 



    allClients.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     { 
      Cursor cur = (Cursor) parent.getItemAtPosition(position); 
      int clientIdint = cur.getInt(cur.getColumnIndex("_id")); 
      String clientId = Integer.toString(clientIdint); 
      Intent AddClientIntent = new Intent(getApplicationContext(), AddClientActivity.class); 
      AddClientIntent.putExtra("CLIENT_IDINT", clientIdint); 
      AddClientIntent.putExtra("CLIENT_ID", clientId); 
      startActivity(AddClientIntent); 
     } 
    }); 

    inputSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) 
     { 
      // When user changed the Text 
      clientAdapter.getFilter().filter(cs.toString()); 
      allClients.setAdapter(clientAdapter); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
             int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }); 

} 
} 

列表的活動,這裏是我的光標類

public class ClientCursorAdapter extends CursorAdapter 
{ 

public ClientCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor, 0); 
} 

// The newView method is used to inflate a new view and return it, 
// you don't bind any data to the view at this point. 
@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return LayoutInflater.from(context).inflate(R.layout.display_client_row, parent, false); 
} 

// The bindView method is used to bind all data to a given view 
// such as setting the text on a TextView. 
@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    // Find fields to populate in inflated template 
    TextView txtViewName = (TextView) view.findViewById(R.id.txtViewName); 
    TextView txtViewAddress = (TextView) view.findViewById(R.id.txtViewAddress); 
    TextView txtViewPhoneNum = (TextView) view.findViewById(R.id.txtViewPhoneNum); 
    // Extract properties from cursor 
    String name = cursor.getString(cursor.getColumnIndexOrThrow("name")); 
    String address = cursor.getString(cursor.getColumnIndexOrThrow("address")); 
    String homenum = cursor.getString(cursor.getColumnIndexOrThrow("homenum")); 
    String cellnum = cursor.getString(cursor.getColumnIndexOrThrow("cellnum")); 
    String worknum = cursor.getString(cursor.getColumnIndexOrThrow("worknum")); 
    // Populate fields with extracted properties 
    txtViewName.setText(name); 
    txtViewAddress.setText(address); 

    if (!cellnum.equals("") && !cellnum.equalsIgnoreCase("cell phone")) 
    { 
     txtViewPhoneNum.setText(String.valueOf(cellnum)); 
    } 
    else if (!homenum.equals("") && !homenum.equalsIgnoreCase("home phone")) 
    { 
     txtViewPhoneNum.setText(String.valueOf(homenum)); 
    } 
    else if (!worknum.equals("") && !worknum.equalsIgnoreCase("work phone")) 
    { 
     txtViewPhoneNum.setText(String.valueOf(worknum)); 
    } 
    else 
    { 
     txtViewPhoneNum.setText("No phone listed."); 
    } 

} 

} 

感謝您的時間,讓我知道,如果你需要的任何其他信息!

+0

'addTextChangedListener'不會直接與'CursorAdapter'工作 – Yupi

+0

這樣生病了,必須做一個新的適配器? – Shawnzey

回答

0

要過濾CursorAdapterSimpleCursorAdapter您必須使用clientAdapter.setFilterQueryProvider比您可以過濾您的內容。

clientAdapter .setFilterQueryProvider(new FilterQueryProvider() { 
    public Cursor runQuery(CharSequence constraint) { 
    //your code here 
    } 

}); 

你可以在這裏看到更詳細的解釋:How to set Filter to ListView with CursorAdapter? 你也不必安裝適配器從addTextChangedListener方法兩次刪除allClients.setAdapter(clientAdapter);。如果需要,你可以撥打電話notifyDataSetChanged();

+0

仍然會在onTextChanged方法內正確嗎? – Shawnzey

+0

感謝您的幫助。我沒有運行,但這是我需要做的! – Shawnzey

相關問題