Last active
August 30, 2016 13:07
-
-
Save DANISSIMO9699/4de88cb9e2a299287db207c8cb2970d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchActivity extends AppCompatActivity { | |
ArrayList<Accordo> chords; | |
RecyclerView rv; | |
SearchView sv; | |
ArrayList<Accordo> filteredList; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.search_layout); | |
/** gestisce la pubblicita */ | |
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544/6300978111"); | |
AdView searchBanner = (AdView) findViewById(R.id.search_ad); | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
searchBanner.loadAd(adRequest); | |
/**-------------------------------*/ | |
Intent intent = this.getIntent(); | |
Bundle bundle = intent.getExtras(); | |
chords = bundle.getParcelableArrayList("chords"); | |
filteredList = bundle.getParcelableArrayList("chords"); // THIS IS WHAT I DID TO FIX THE CODE | |
sv = (SearchView) findViewById(R.id.testo_ricerca); | |
sv.setIconifiedByDefault(false); | |
rv = (RecyclerView) findViewById(R.id.lista_ricerca); | |
rv.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayoutManager.VERTICAL, false)); | |
rv.setHasFixedSize(true); | |
final SearchAdapter adapter = new SearchAdapter(this, chords); | |
rv.setAdapter(adapter); | |
//SEARCH | |
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
@Override | |
public boolean onQueryTextSubmit(String query) { | |
return false; | |
} | |
@Override | |
public boolean onQueryTextChange(String newText) { | |
//FILTER AS YOU TYPE | |
List<Accordo> filteredModelList = filter(chords, newText); | |
adapter.setFilter(filteredModelList); | |
return true; | |
} | |
}); | |
/** gestisce cosa succede quando un elemento della lista viene cliccato */ | |
ItemClickSupport.addTo(rv).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){ | |
@Override | |
public void onItemClicked(RecyclerView recyclerView, int position, View v) { | |
Intent intent = new Intent(SearchActivity.this, ChordActivity.class); | |
Bundle bundle = new Bundle(); | |
bundle.putParcelable("selected", filteredList.get(position)); | |
intent.putExtras(bundle); | |
startActivity(intent); | |
} | |
}); | |
} | |
private List<Accordo> filter(List<Accordo> models, String query) { | |
query = query.toLowerCase(); | |
filteredList = new ArrayList<>(); | |
for (Accordo model : models) { | |
final String text = model.getName().toLowerCase(); | |
if (text.contains(query)) { | |
filteredList.add(model); | |
} | |
} | |
return filteredList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment