Android 无法筛选包含2行的listview

Android 无法筛选包含2行的listview,android,Android,我试图提供一个可搜索的列表视图。每个listitem都有一个名称和地址文本框,但我只想筛选名称。我当前的代码什么都不做,也就是说根本没有进行过滤。有没有办法将列设置为筛选依据 //class variables private SimpleCursorAdapter mAdapter; private EditText filterText = null; @Override protected void onCreate(Bundle savedInstanceState) {

我试图提供一个可搜索的列表视图。每个listitem都有一个名称和地址文本框,但我只想筛选名称。我当前的代码什么都不做,也就是说根本没有进行过滤。有没有办法将列设置为筛选依据

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

}
查看此帖子:


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

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

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