2016-08-12 1 views
0

Je développe une application Android où une liste de titre provient de la base de données. Je veux ajouter une fonction de recherche pour rechercher le titre. J'essaye d'ajouter la fonction de recherche. Mais je ne peux pas faire ça. Lorsque je clique sur la recherche d'entrée, l'application s'est arrêtée malheureusement. Voici mon code:Ma fonction de recherche ne fonctionne pas. Comment puis-je ajouter la fonction de recherche

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





} 

Répondre

0

utiliser le code ci-dessous:

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