0

我正在尝试提供可搜索的列表视图。每个列表项都有一个名称和地址文本框,但我只想过滤名称。我当前的代码什么都不做,即根本没有过滤。有没有办法设置要过滤的列?

    //class variables
private SimpleCursorAdapter mAdapter;
private EditText filterText = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

       setContentView(R.layout.add_customer_listview);

       //listViewCustomers = (ListView) findViewById(R.id.list);
       buildingListViewAdaptor();
       setListAdapter(mAdapter);

       // set up the filter
       filterText = (EditText) findViewById(R.id.search_box);
       filterText.addTextChangedListener(filterTextWatcher);

}


private TextWatcher filterTextWatcher = new TextWatcher() {


    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        mAdapter.getFilter().filter(s);
        Log.d(GlobalTools.ErrorCodes.INFO, "Searchtext="  + s.toString());
    }

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

};

private void buildingListViewAdaptor(){

    //1. Get the data
    CustomerLocationDataHandler clDataHandler = new CustomerLocationDataHandler(getContentResolver());
    Cursor cursor = clDataHandler.allCustomerLocations();
    clDataHandler=null;

    //2. Build the adaptor
    mAdapter = new SimpleCursorAdapter(this, 
            R.layout.list_item_custom_font, // was list_item_custom_font
            cursor,  
            new String[]{MyobiliseData.Columns_CustomerLocations.CUSTOMER_NAME,MyobiliseData.Columns_CustomerLocations.CITY},
            new int[] {R.id.text1,R.id.text2}
            );

}
4

2 回答 2

0

查看这篇文章:ListView、SimpleCursorAdapter、一个 EditText 过滤器——为什么它不做任何事情?

我已经添加了类似的方法,它现在正在工作。

于 2012-07-10T03:49:48.907 回答
0

为了实现这一点,我需要在用户每次在搜索框中输入时重新查询数据库。我在这里包含了工作代码:

public class AddCustomerActivity extends ListActivity{

//constants
public static final String B4ME = "B4ME";  //Used when passing a boolean to this activity

// dialog constants
private static final int DIALOG_CHECKINSERT = 0;


//class variables
private SimpleCursorAdapter mAdapter;
private EditText filterText = null;
private boolean mInsertB4;   //if false ->means insert after
private Long mRunDetailID;
private Long mCustomerLocationId;
private int mSelectedItemPosition;
private long mSelectedItemId;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

       setContentView(R.layout.add_customer_listview);
       readVariablesPassedToThis();

       //listViewCustomers = (ListView) findViewById(R.id.list);
       buildingListViewAdaptor();
       setListAdapter(mAdapter);


       // set up the filter
       filterText = (EditText) findViewById(R.id.search_box);
       filterText.addTextChangedListener(filterTextWatcher);

       mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
           public Cursor runQuery(CharSequence constraint) {

               return filterRefresh(constraint);
           }
       });

}


private TextWatcher filterTextWatcher = new TextWatcher() {


    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        mAdapter.getFilter().filter(s);
    }

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);
    mSelectedItemPosition = position;
    mSelectedItemId = id;
    Log.d(GlobalTools.ErrorCodes.INFO, "Selected ItemPosition=" + mSelectedItemPosition + " and itemid ="+mSelectedItemId);
    showDialog(DIALOG_CHECKINSERT);

}


private void buildingListViewAdaptor(){

    //2. Build the adaptor
    mAdapter = new SimpleCursorAdapter(this, 
            R.layout.list_item_custom_font, // was list_item_custom_font
            filterRefresh(null),  
            new String[]{MyobiliseData.Columns_CustomerLocations.CUSTOMER_NAME,MyobiliseData.Columns_CustomerLocations.CITY},
            new int[] {R.id.text1,R.id.text2}
            );

}

private Cursor filterRefresh(CharSequence constraint){
    //1. Get the data
    CustomerLocationDataHandler clDataHandler = new CustomerLocationDataHandler(getContentResolver());
    Cursor cursor = clDataHandler.customerLocationsFilteredOn(constraint);
    clDataHandler=null;
    return cursor;
}
于 2012-07-13T05:52:41.163 回答