Currently, i can sort my documents in a Query, with it's:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100);
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query,
AnuncioPrincipal.class).build();
adapter = new AnuncioAdapter(options);
RecyclerView recyclerView = root.findViewById(R.id.recyclerCadAnun);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
This works fine, but, i need that it change when the user click on button, i need that var "campo" must be changed to "dataPreco" and the list in recycler view must be updated:
botaoFiltrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
campo = "dataAnuncio";
}
});
In spite of "campo" be changed to "dataAnuncio", the recyclerView still remains equals.
I need that the button click, the recyclerView change to the new query orderBy() method.
The FirestoreRecyclerAdapter has an updateOptions method that you can call to update the options.
So you could call that with something like:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100); // new campo
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query, AnuncioPrincipal.class).build();
adapter.updateOptions(options);
Related
I'm adding content to my database (Firebase) which are suppose to be loaded into my recyclerview. But the new content always appear at the bottom of the recyclerview. Is there any way to make them appear at the top rather than appear at the bottom
If you want to appear new Items on the top just Add this code in your Activity.
you need to reverse your RecyclerView, then you totally need this code segment. It does not require any modification or any special library. It’s so simple to work on LinearLayoutManager .
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLayoutManager);
First you need to create a LinearLayoutManager object and use “setReverseLayout(true)” and “setStackFromEnd(true)” methods to reverse and add item at the end.
I finally solved the issue all i needed to do was to initialize an integer variable as 0,add it along side with the object of the model class that is to be gotten from the Firebase database and finally notifying an item inserted to the adapter.The code will explain better
int position = 0;
ArrayList<Places> data = new ArrayList<>();
LinearLayoutManager lg = new LinearLayoutManager(this);
then the final code was this
valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Places content = ds.getValue(Places.class);
data.add(position,content);
adapter = new YourAdapter(data, getApplicationContext);
recyclerview.setLayoutManager(new Line);
recyclerview.setAdapter(adapter);
adapter.notifyItemInserted(position);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Handle error code here
}
});
i've created a custom list with four textviews...the data in this list is saved through a dialog which has a ok button. when i add the data, it gets saved in the list(works fine till now). when i add the next element, all the rows gets same value as the last one...the notifyDatasetChanged() is also not working...am i wrong somewhere?...this is my code...
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val = editquantity.getText().toString();
valq1 = Integer.parseInt(val);
ListView l;
l = (ListView) v.findViewById(R.id.order_listview);
myAdapter adapter = new myAdapter(getActivity(), row);
l.setAdapter(adapter);
row.add("");
adapter.notifyDataSetChanged();
builder.dismiss();
}
});
builder.setView(dialog);
builder.show();
You created new adapter everytime you click ok. Put your adapter code outside the onclick method:
ListView l = (ListView) v.findViewById(R.id.order_listview);
myAdapter adapter = new myAdapter(getActivity(), row);
l.setAdapter(adapter);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val = editquantity.getText().toString();
valq1 = Integer.parseInt(val);
row.add(""); //add new item to the list
adapter.notifyDataSetChanged(); //notify adapter
builder.dismiss();
}
});
builder.setView(dialog);
builder.show();
You need to use addAll() method for adding new values remaining old values are same in arraylist.
row.addAll("test");
Why are you adding row as blank.
Secondly you have set row in adapter without defining it.
Please provide more details to the question as i cannot understand where row has been declared.
Coming to the similar value problem.
You need to update row array because it has been set on adapter.
There are three things you need to change
1. Declare your ListView object outside of the onCreate()
2. Declare your adapter object outside of the onCreate()
Now this initialization of this below line needs to be initialized only once in onCreate(). You need not to initialize it again and again when you add new item.So below line write in your onCreate().
myAdapter adapter = new myAdapter(getActivity(), row);
When you add new item you only need to add that item in your arraylist named row onClick of ok button and then call adapter.notifyDataSetChanged(); it will work fine.
i am using a recyclerView and loading the data from database.The recyclerView item have 2 buttons. The recyclerView loads fine and clicks work fine for the following code.
allData=db.getCatData(CATEGORYLIST_CIDD,1);
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);
later i rearrange the adapter data for displaying the data in descending order by using this code
db=new DatabaseHandler(getActivity().getApplicationContext());
allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);
textlistadapter.notifyDataSetChanged();
Now the items are rearranged on the recyclerView, but the onShareButtonClickListener returns null, here is the code of the adapter.
private OnShareButtonClickListener onShareButtonClickListener;
public interface OnShareButtonClickListener {
void onItemClick(View view, Pojo obj, int position);
}
public void setOnShareButtonClickListener(final OnShareButtonClickListener onShareButtonClickListener) {
this.onShareButtonClickListener = onShareButtonClickListener;
}
the click listener code is here
holder.bt_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Snackbar.make(view, "Share Clicked", Snackbar.LENGTH_SHORT).show();
if (onShareButtonClickListener != null) {
onShareButtonClickListener.onItemClick(view, c, position);
}
}
});
The click does not work since the onShareButtonClickListener is null.
The same code works well for the first time and once i change the order then the click events are not working, all other functions are working well.
you should only need to do this once:
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);
later on, if you want to update/change the data, then you should do something like this:
textlistadapter.setListItems(allData);
textlistadapter.notifyDataSetChanged();
Where setListItems() is a public method in your adapter, an example like this:
public void setListItems(List<> allData) {
this.data.clear();
this.data.addAll(allData);
}
It's easy to understand what you are doing wrong. You populate an array which will feed your adapter with data, that meaning that, after setting the adapter, if you want it's data to change, you only need to change the array you populated the data with, and notify the adapter for the data change.
For example:
myData = db.getData();
myAdapter = new MyAdapter(this,myData);
recyvlerview.setAdapter(myAdapter);
Now every time you want to change the data, you only need to change your myData and notifity the adapter
myAdapter.notifyDataSetChange();
Basically instead of doing this:
db=new DatabaseHandler(getActivity().getApplicationContext());
allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
textlistadapter = new TextListAdapter(getActivity(), allData);
recyclerView.setAdapter(textlistadapter);
textlistadapter.notifyDataSetChanged();
This should suffice
db=new DatabaseHandler(getActivity().getApplicationContext());
allData=db.getCatData(CATEGORYLIST_CIDD,order_id);
textlistadapter.notifyDataSetChanged();
I am trying to set up a gridview that will refresh when a new search is executed.
this is my call to set up the gridview
final GridElement_Results adapter = new GridElement_Results(this, savedInstanceState, track_name, album_name, track_num, album_id, album_link);
grid = (GridView)findViewById(R.id.grid_results);
grid.setAdapter(adapter);
As you can see, I am passing it a bunch of arrays.
On a button click, the function getTracks() is called which gets new data from the server. The arrays are updated with the new data, which I have verified is working.
icon_activesearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getTracks("newsearchterm");
adapter.notifyDataSetChanged();
grid.invalidateViews();
}
}
);
And then I call adapter.notifyDataSetChanged() and grid.invalidateViews() to update and redraw the grid. But obviously I'm doing something wrong here.
Is it because I'm setting the adapter as final? To access it within the onClick method I need to set it to final.
What should I change to get it to work.
you should wait until new data fetch then notify adapter but you call notify just after getTracks() without any delay
I have some tables in the database, every table has many rows,I want to show the tables's data in a list, so I use a RecyclerView to show the data, and a spinner to select the table which I want to show.
But now, when I select an item of spinner, the RecyclerView doesn't have any changes ,it can't show the new data only after I reopen the fragment.
My code:
In adapter:
1.removeItem works
the item in the RecyclerView will be removed when removeItem called.
public void removeItem(WordCls wordCls) {
int position = wordsList.indexOf(wordCls);
wordsList.remove(position);
notifyItemRemoved(position);
}
2.updateList doesn't work
But when updateList called,nothing happened in the view
public void updateList(List<WordCls> wordsList) {
//this.wordsList = wordsList;
this.wordsList.clear();
this.wordsList.addAll(wordsList);
notifyDataSetChanged();
}
In fragment:
in onCreateView():
recyclerView = (RecyclerView) view.findViewById(R.id.wordbook_recycler_list);
recyclerView.setLayoutManager(new LinearLayoutManager(activity));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
wordsList = WordsManager.getWordsList(tableName);
wordCardAdapter = new WordCardAdapter(activity, tableName, wordsList);
recyclerView.setAdapter(wordCardAdapter);
wordCardAdapter.setOnItemClickListener(new WordCardAdapter.OnRecyclerViewItemClickListener() {
#Override
public void onItemClick(View view, WordCls wordCls) {
Toast.makeText(getActivity(), wordCls.getWord(), Toast.LENGTH_SHORT).show();
wordCardAdapter.removeItem(wordCls);
WordsManager.deleteWord(tableName, wordCls.getWord());
}
});
in SpinnerItemSelectedListener:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
tableName= parent.getItemAtPosition(position).toString();
prefEditorSettings.putInt(KEY_SPINNER_SELECTED_ITEM, position);
prefEditorSettings.commit();
tableName = getWordbookList().get(spnSelectedPosition);
wordsList = WordsManager.getWordsList(tableName);
wordCardAdapter.updateList(wordsList);
Toast.makeText(activity, tableName, 2000).show();
}
==============================================
update:
the method updateList is right.
just my careless to have passed a wrong param at
tableName = getWordbookList().get(spnSelectedPosition);
it should be
tableName = getWordbookList().get(position);
now, it works.
Actually, I don't know if RecyclerView is the best way to fulfill your purpose.
RecyclerView(s) are to be used when you have to show multiple instances of your List, in a list-y way :)
If you have just one View to update (that contains multiple fields to be updated), I'd choose some data-binding library like RoboBinding or the new beta version of google binding library (MVVM paradigm)
I'm not going into further details as I just want to warn you about MVVM programming paradigm that can be a worthy solution in your case.
instead of wordCardAdapter.updateList(wordsList) , add new data to wordsList and wordCardAdapter.notifyDataSetChanged() inside spinner
hope it works.