-1

My code looks like:

   public class MainActivity extends ListActivity {


@Override
public int getSelectedItemPosition() {
    return super.getSelectedItemPosition();
}

@Override
public long getSelectedItemId() {
    return super.getSelectedItemId();
}

ListView lv;
Cursor cursor1;
EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText)findViewById(R.id.txtsearch);

    initList();

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (s.toString().equals("")){
                //reset listview
                initList();
            }
            else{
                //perform search
                searchItem(s.toString());  //Searchitem
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}

public void initList(){
    cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null , null , null , null);
    startManagingCursor(cursor1);

    String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
    int[] to = {android.R.id.text1, android.R.id.text2};

    SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to );
    setListAdapter(listadapter);

    lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}

public void searchItem(String textToSearch) {
   //how should I iterate through the listview and remove items that do not start with "textToSearch"
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

I would like to do the search with the help of the searchItem(String) method, but I don't know how to iterate through the list and remove the items that do not start with the searchtext.

Code-G
  • 41
  • 5

2 Answers2

0
mySearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                // Serach the list from database based on Search Text, Eg Name is Test. 
                // and pass the list to the adapter
                ArrayList<ShortInfo> searchArrayList = myDbHandler.getSearchList(query);
                if (searchArrayList != null) {

                    myAdapter = new Adapter(Activity.this, searchArrayList);
                    myListView.setAdapter(myAdapter);


                } 
                return false;
                }

            @Override
            public boolean onQueryTextChange(String newText) {

                // Display Arraylist which contains whole data, when serach text is empty
                if (newText.isEmpty()) {
                    myAdapter = new Adapter(Activity.this, myInfoArrayList);
                    myListView.setAdapter(myAdapter);

                }
                return false;
                }
        });
jessica
  • 1,700
  • 1
  • 11
  • 17
0

If you have ArrayList of model and you are using it to showing in ListView and want to search then do like this:

In your custom adapter class:

private Context context;
//Arraylist of data which you had passed to adapter by addItems function
private List<Model> arrayList;
//This is the copy list from arrayList to save result of search and return
private List<Model> copyList;

/**
 * Custructor of adapter class
 * @param context
 */
public AdapterListing(Context context) {
    this.context = context;
    this.arrayList = new ArrayList<Model>();
    copyList = new ArrayList<Model>();
}

/**
 * Function used for passing arralist of model to adapter
 * @param arrayList
 */
public void addItems(ArrayList<Model> arrayList) {
    this.arrayList = arrayList;
    for (int i = 0; i < arrayList.size(); i++) {
        copyList.add(arrayList.get(i));
    }
    notifyDataSetChanged();
}

/**
 * Search here
 * @param text
 * @return
 */
public ArrayList<Model> onTextChange(String text) {
    arrayList.clear();
    if (copyList == null)
        return null;

    if (text.isEmpty()) {
        arrayList.addAll(copyList);
    } else {
        for (Model model : copyList) {
            if (model.name.toLowerCase(new Locale(text))
                    .contains(text.toLowerCase(new Locale(text)))) {
                arrayList.add(model);
            }
        }
    }
    notifyDataSetChanged();
    return arrayList;
}

Now in your Fragment / Activity add this function and make a call after editText view reference:

private void setAutoSuggestionText() {
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (adapterListing != null) {
                ArrayList<Model> mArrayList = adapterListing.onTextChange("" + s);
                if (mArrayList != null && mArrayList.size() != 0) {
                    mListView.setVisibility(View.VISIBLE);
                } else {
                    mListView.setVisibility(View.GONE);
                }
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

Here my used variables points to:

editText - your EdittextView

mListView - ListView in which you have to create search functionality

adapterListing - ArrayList of model class in which search is required

Model - This is the class of object to make ArrayList

So this is the my logic behind search in ListView.

Ready Android
  • 3,529
  • 2
  • 26
  • 40