我的搜索功能不起作用。如何添加搜索功能

时间:2016-08-12 17:22:04

标签: android search

我正在开发一个Android应用程序,其中标题列表来自数据库。我想添加搜索功能来搜索标题。我正在尝试添加搜索功能。但我无法做到这一点。当我点击输入搜索时,app很不幸地停了下来。这是我的代码:

public class Title extends AppCompatActivity implements OnVersionNameSelectionChangeListener{
private static final String TAG = Title.class.getSimpleName();

String str, arr[];
EditText inputSearch;

ArrayAdapter<String> adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_title);

    inputSearch = (EditText) findViewById( R.id.inputSearch);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            Title.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
        }
    });



    str = getIntent().getExtras().getString("string","defaultValue");
    //Toast.makeText(getApplicationContext(), str , Toast.LENGTH_SHORT).show();

    // Check whether the Activity is using the layout version with the fragment_container
    // FrameLayout and if so we must add the first fragment

    if (findViewById(R.id.fragment_container) != null){

        // However if we are being restored from a previous state, then we don't
        // need to do anything and should return or we could end up with overlapping Fragments
        if (savedInstanceState != null){
            return;
        }

        // Create an Instance of Fragment
        VersionsFragment versionsFragment = new VersionsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key", str);
        //versionsFragment.setArguments(getIntent().getExtras());
        versionsFragment.setArguments(bundle);
        getFragmentManager().beginTransaction().add(R.id.fragment_container, versionsFragment).commit();
    }
}

@Override
public void OnSelectionChanged(int versionNameIndex, String[] answer) {
    DescriptionFragment descriptionFragment = (DescriptionFragment) getFragmentManager()
            .findFragmentById(R.id.version_description);

    if (descriptionFragment != null){
        // If description is available, we are in two pane layout
        // so we call the method in DescriptionFragment to update its content
        // Bundle args = new Bundle();
        // args.putString("key", str);
        // args.putStringArray("answer", answer );
        //args.putInt(DescriptionFragment.KEY_POSITION,versionNameIndex);
        //descriptionFragment.setArguments(args);
        setArray(answer);
        //setStr(str);
        descriptionFragment.setDescription(versionNameIndex);
    } else {
        DescriptionFragment newDescriptionFragment = new DescriptionFragment();
        Bundle args = new Bundle();
        //args.putString("key", str);
        //args.putStringArray("answer", answer );
        setArray(answer);
        args.putInt(DescriptionFragment.KEY_POSITION,versionNameIndex);

        newDescriptionFragment.setArguments(args);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the backStack so the User can navigate back
        fragmentTransaction.replace(R.id.fragment_container,newDescriptionFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
public String getStr(){
    return str;
}

public void setArray(String[] s)
{
    arr = s;
    //Log.d(TAG, "setArray: " + arr[0]);
}
public String[] getArray(){
    return arr;
}





}

1 个答案:

答案 0 :(得分:0)

使用以下代码:

Edittext input_search = (EditText)findViewById(R.id.input_search);

 input_search.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.toString());
        }

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