英文:
Show Recyclerview only after submitting search
问题
I understand your request. Here's the translated code part without additional content:
我想要在启动MainActivity时使其为空,只有在提交搜索后才应显示recyclerview的结果(因此它永远不会显示整个recyclerview列表)。到目前为止,应用程序可以正常工作,但MainActivity启动时显示了推送到recyclerview中的所有数据。
如果列表很大,这将影响性能,因此我需要一种方法来使其为空(或填充但不显示它),并仅显示具有搜索结果的recyclerview,或者创建一个在MainActivity之前启动的新活动,该活动仅包含SearchView,然后在MainActivity上显示结果。
这是我的当前代码:
MainActivity.class(不包括导入)
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);
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) {
// 省略异步任务的代码部分
}
protected void onPostExecute(List<Ontologija> podaci) {
adapter.setPodaci(podaci);
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu 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;
// 构造函数接收上下文作为参数
public AdapterListe(Context context) {
this.mInflater = LayoutInflater.from(context);
podaci = new ArrayList<>();
}
// 省略 onCreateViewHolder 和 onBindViewHolder 方法的代码部分
// 获取总项数
@Override
public int getItemCount() {
return podaci == null ? 0 : podaci.size();
}
// 省略其他方法的代码部分
}
请注意,这只是代码的部分翻译。如果您需要任何其他帮助或有其他问题,请随时提出。
英文:
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.
答案1
得分: 1
Here is the translated code:
if you dont want show just remove notifyDataSetChanged
protected void onPostExecute(List<Ontology> data) {
adapter.setData(data);
//adapter.notifyDataSetChanged();
}
Edit:
if (!query.isEmpty()) {
adapter.getFilter().filter(query);
}
or use `searchView.clearFocus();`
英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论