I want the MainActivity to be empty when I start it and only after submitting the search should the recyclerview show the results (so it never shows the whole recyclerview list). As of now, the app works but MainActivity starts with all the data thats pushed into the recyclerview.
If the list was huge it would affect performance, so I either need a way to start it empty (or fill it but not show it) and show recyclerview with just the search results, or make a new activity that would start before MainActivity which would only have the Searchview and then the results on MainActivity.
This is my current code:
MainActivity.class (excluding imports)
public class MainActivity extends AppCompatActivity{
private AdapterListe adapter;
private RESTTask asyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.lista);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new AdapterListe(this);
//adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
asyncTask = new RESTTask();
asyncTask.execute( getString(R.string.REST_URL));
}
private class RESTTask extends AsyncTask<String, Void, List<Ontologija>> {
protected List<Ontologija> doInBackground(String... adresa) {
String stringUrl = adresa[0];
List<Ontologija> vrati = null;
try {
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.connect();
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
Type listType = new TypeToken<ArrayList<Ontologija>>() {
}.getType();
vrati = new Gson().fromJson(reader, listType);
reader.close();
streamReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return vrati;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(List<Ontologija> podaci) {
adapter.setPodaci(podaci);
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.search_icon);
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint("Pretraži...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
Adapter
public class AdapterListe extends RecyclerView.Adapter<AdapterListe.Red> implements Filterable {
private List<Ontologija> podaci;
private List<Ontologija> podaciTemp;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// Podatke proslijedimo kroz konstruktor
public AdapterListe(Context context) {
this.mInflater = LayoutInflater.from(context);
podaci = new ArrayList<>();
}
// napuni predložak red (datoteka red_liste.xml)
@Override
public Red onCreateViewHolder(ViewGroup roditelj, int viewType) {
podaciTemp = new ArrayList<>(podaci);
View view = mInflater.inflate(R.layout.red_liste, roditelj, false);
return new Red(view);
}
// Veže podatke za svaki red
@Override
public void onBindViewHolder(Red red, int position) {
Ontologija o = podaci.get(position);
red.naziv.setText(o.getNaziv());
red.tip.setText(o.getTip());
red.opis.setText(o.getOpis());
red.anotacija.setText(o.getAnotacija());
}
// Ukupan broj redova (mora biti implementirano)
@Override
public int getItemCount() {
return podaci==null ? 0 : podaci.size();
}
// Pohranjuje i reciklira pogled kako se prolazi kroz listu
public class Red extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView naziv;
private TextView tip;
private TextView opis;
private TextView anotacija;
Red(View itemView) {
super(itemView);
naziv = itemView.findViewById(R.id.naziv);
tip = itemView.findViewById(R.id.tip);
opis = itemView.findViewById(R.id.opis);
anotacija = itemView.findViewById(R.id.anotacija);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// klikom na listu dobijemo samo poziciju koju stavku liste smo odabrali.
// Ova metoda pomaže da na osnovu pozicije dobijemo cijeli objekt u toj stavci
public Ontologija getItem(int id) {
return podaci.get(id);
}
public void setPodaci(List<Ontologija> itemList) {
this.podaci = itemList;
}
// dopusti hvatanje odabira (klik/dotakni)
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// potrebno kako bi mogli hvatati klikove/dodire
public interface ItemClickListener {
void onItemClick(View view, int position);
}
@Override
public Filter getFilter() {
return new Filter() {
//background
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Ontologija> filteredList = new ArrayList<>();
if (constraint.toString().isEmpty()) {
filteredList.addAll(podaciTemp);
} else {
if(constraint.toString().length() >= 3) {
for (Ontologija element : podaciTemp) {
if (element.getNaziv().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(element);
}
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
//ui
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
podaci.clear();
podaci.addAll((Collection<? extends Ontologija>) filterResults.values);
notifyDataSetChanged();
}
};
}
}
Either code, some reference videos/posts or basically any type of help would be appreciated, but so far I've only seen basic filter searchview tutorials of whole recyclerview, nothing of help for my specific situation.
if you dont want show just remove notifyDataSetChanged
protected void onPostExecute(List<Ontologija> podaci) {
adapter.setPodaci(podaci);
//adapter.notifyDataSetChanged();
}
Edit :
if (!query.isEmpty()) {
adapter.getFilter().filter(query);
}
or use searchView.clearFocus();