-1

I am facing problem while filtration of list .I am using onTextChange() method for it .It is working properly but problem is that as i type single character in edit Text the soft keyboard gets disappear and I have to tap edit text again to type complete string .I have also done code so that soft keyboard does not disappear after text change but to type still I have to tap on edit text.Here is my code :

 search.addTextChangedListener(new TextWatcher() {
        @ Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            String startwith = s.toString();
            ArrayList < Myshares > filterList = new ArrayList < Myshares > ();
            for (int i = 0; i < fileList.size(); i++) {
                if (fileList.get(i).getName().toLowerCase().startsWith(startwith) || fileList.get(i).getName().toUpperCase().startsWith(startwith)) {
                    filterList.add(fileList.get(i));
                }
            }
            adapter = new MListAdapter(PlayListActivity.this, filterList);

            playListView.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }
        @
        Override
        public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {    
        }
        @
        Override
        public void afterTextChanged(Editable s) {
            search.setSelection(search.getText().length());
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(search, InputMethodManager.SHOW_IMPLICIT);
        }
    });

here is my xml code for edit text:

<EditText
         android:ems="10"
        android:id="@+id/search_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:background="@drawable/searchbox"
        android:editable="true"
        android:focusable="true"
        android:inputType="textFilter"
        android:hint="Search"
        android:keepScreenOn="true"
        android:paddingLeft="10dp"
        android:textSize="15dp" />

i have also set android:focusable="false " property on list view so that it doesn't overlap soft keyboard. But the problem with edit text is still same .Here are screenshots

enter image description here

As I insert A Single character list gets filter and cursor is gone from edit text box.

enter image description here

Sneha Bansal
  • 941
  • 1
  • 13
  • 38
  • http://chat.stackoverflow.com/rooms/1278/android-discussion – Make it Simple May 17 '13 at 07:07
  • 1
    You can read my answer from [here](http://stackoverflow.com/questions/16536663/how-to-add-search-functionality-on-list-view-items-in-listview-are-from-baseadap/16536801#16536801) about this problem I hope this will help – Chirag Jain May 14 '13 at 13:09

2 Answers2

0
//ArrayAdapter Definition. Make it activity class field.
ArrayAdapter<String> adapter;

//initialize the adapter by loading complete data and set Adapter to ListView.
//**Note: I have used ArrayList<String> below.**
ArrayList <String> filterList = new ArrayList <Myshares> ();

for (int i = 0; i < fileList.size(); i++) {
            if (fileList.get(i).getName().toLowerCase() || fileList.get(i).getName().toUpperCase()) {
                filterList.add(fileList.get(i));
            }
        }

adapter = new ArrayAdapter<String>(this, R.layout.searchresult_item, R.id.search_item_name,filterList);

//Here, searchresult_item is the layout file name. and search_item_name is the Textview inside it.

search.addTextChangedListener(new TextWatcher() {

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

        YourActivity.this.Youradapter.getFilter().filter(cs);

}

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


}

@Override
public void afterTextChanged(Editable s) {


}
});
SKK
  • 5,261
  • 3
  • 27
  • 39
  • do i need to define getFilter() method for my adapter as I am using custom adapter – Sneha Bansal May 14 '13 at 13:33
  • check my edited answer. i have not used custom adapter though. i don't think u can override or define getFilter() method in your adapter. And if its just one field being populated in the listview, i think u can use the above approach. its simple and works fine. – SKK May 14 '13 at 19:37
  • @ Santhosh thanks for reply but I am not having problem in filtration.It is working properly but problem is that I have to tap on edit text for inserting a single character to bring cursor back.. – Sneha Bansal May 15 '13 at 05:43
  • May be the focus is getting changed when you are setting the adapter inside onTextChanged. Try to set ListView's android:focusable property to false. and if required change it to true in afterTextChanged. also remove adapter.notifyDataSetChanged();. its not required since u are setting the adapter in the above line. it will refresh the listview again. Hope this will solve ur issue. i can't give u a working solution as i can't reproduce ur work. all i can is suggest u some ideas. – SKK May 15 '13 at 05:54
0

I guess you have added the edit text in list view by addView() Method. It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;

in getView for your adapter: Add an onFocusChanged listener to edit text and when that edit text gains focus, set currentlyFocusedRow (int variable) = row that focused(contained) edit text happens to be in. Also set any edited text that is in the currentlyFocusedRow to be focused.

Reply me if you see some problems in this.

Nishit Joshi
  • 111
  • 3