0

I have list of names (Firstname + LastName) and want to search by Firstname and lastname in that listView . I am using this code to search by frstname only

Android Search in ListView Example

can anyone give me hint what need to do changes in code so that I can search by Firstname and lastname also ?

dnana
  • 171
  • 1
  • 3
  • 14
  • please provide your source code here – Milind Dec 03 '12 at 06:20
  • how you are displaying `last name` ? it is `listview_array` or a separate array? – Mohsin Naeem Dec 03 '12 at 06:25
  • I answered a question like this a couple week ago, does this help? [ArrayAdapter - filtering with multiple search terms](http://stackoverflow.com/q/13371160/1267661) At the very least, it shows you how and why you need to create a custom ArrayAdapter to perform non-standard filtering. – Sam Dec 03 '12 at 06:31

2 Answers2

0

just follow this code :

String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                    "iPhone 4S", "Samsung Galaxy Note 800",
                                    "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);

        lv.setAdapter(adapter); 



MainActivity.java
inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);
    }

    @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
    }
});
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
0

ArrayAdapter already has filter functionality, and ListView can accept search text, so you don't need to re-invent the wheel.

Example:

Have a class hold the data, whenever data has multiple objects:

Public class FullName {
  public final String firstName;
  public final String lastName;

  public Fullname(String fn, String ln){
    this.firstName = fn;
    this.lastName  = ln;
  }

  @Override
  public String toString(){
    // this is what ArrayAdapter uses for display and search
    return firstName + " " + lastName; 
  }
}

Inside Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ListView nameList = new ListView(this);

  setContentView(nameList);

  ArrayAdapter<FullName> nameAdapter = new ArrayAdapter<FullName>(this,android.R.layout.simple_list_item_1);

  nameAdapter.add( new FullName("John","Doe")    );
  nameAdapter.add( new FullName("Bob","Carson")  );
  nameAdapter.add( new FullName("Kyle","Connor") );

  nameList.setAdapter(nameAdapter);

  //bring up the keyboard, start typing on a ListView and it will filter out matching results
  nameList.setTextFilterEnabled(true);
}

In case you really want to search using an EditText:

 nameAdapter.getFilter.filter(myEditText.getText());

clear filter:

 nameAdapter.getFilter.filter(null);
S.D.
  • 29,290
  • 3
  • 79
  • 130