This question already has an answer here:
How to create a reorder-able TableView in JavaFx
(1 answer)
Closed 7 years ago.
I'm having trouble with JavaFXs ListView items. The thing is that I can not find a way to reorder them at runtime. Just with dragging item with mouse. Also I want to change items order in underlaying ObservableList.
How could this be done?
You can change the order of the ObservableList that forms the underlying model of the ListView at any time to change the order of your list view items...
... for example: to use a Comparator to re-order the contents of your list view you could use something like:
javafx.scene.control.ListView<TYPE> listView; // Your ListView, defined somewhere else.
java.util.Collections.sort(listView.getItems(), new java.util.Comparator<TYPE>() {
#Override
public int compare(TYPE o1, TYPE o2) {
// Implement your comparator here.
}
});
... whereas "TYPE" matches the type of your ListView instance.
See http://docs.oracle.com/javafx/2/api/javafx/scene/control/ListView.html#itemsProperty for further details of the list view items definition.
You can add, remove and shuffle the items around as you like and the ListView will automatically adjust it's appearance...
Related
I've figured out how to get the sorted data with a query and pass that to the Recycler view, but it only adds to the RecyclerView instead of replacing the already existing items. Uploaded the code to Pastebin since code irrelevant to the problem is in the class.
MyDatabaseHelper class = https://pastebin.com/fXHw5ccb
CustomAdapter class = https://pastebin.com/4epLqWzJ
MainActivity class = https://pastebin.com/h5mDQunv
Including
customAdapter.notifyDataSetChanged();
recyclerView.setAdapter(customAdapter);
or removing it doesn't solve the problem.
How it looks: https://i.imgur.com/C60ur8b.png
How I want it to look after clicking the menu sort item: https://i.imgur.com/wjZP9yX.png
Solved: Had to pass Custom Adapter to a custom object and read the ArrayList from the object, which let me sort based on SQL queries.
It looks like you call storeFoodDataInArrayList() multiple times which populates the food_ID array, but you never clear that array.
Clear the food_ID array before populating it.
I have a list and its size is x numbers. I want to show 6 items on the screen even user scrolled to view the list , it should show 6 items on the screen.
I tried this Answer but my item's height was cropped and it didn't work for me.
Edit I want to retrieve all list as usual. I just want to make screen carry 6 items .
// get the reference of RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
// set a GridLayoutManager with 6 number of columns, Vertical gravity
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),6,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
In lack of code I only can assume you like to get a list of X number from Api or database, etc and you only want to show max of 6 items
it would have been easier to see some code of what you have done so-far so the correction/suggestion will suite you better, in lack of code I give you few suggestions:
your view should not know anything on how to handle data , it should simply show the data , in you case it should receive a 6 items and show them in the view (recyclerView in your case).
I assume you are not using any pattern (like MVVM or MVP) and you just obtaining your list from (api or database) inside your Activity - (extra tip -> it is always better to use sort of Architecture design to separate your logic into a Presenter / viewModel layer for easy unit test purposes later)
lets assume you define your Adapter in Activity and passing your list like:
In your activity
var listOfAllYourItems = arrayListOf(object1,object2,...,object100)
val myAdapter = MyAdapter()
myAdapter.setItems(listOfAllYourItems.sublist(1,6)) //this will only pass the first 6 items
the rest of normal stuff ...
so this is like you have only 6 items and displaying it in recyclerView - simple (view does not hold any logic on how to handle items over 6 etc, view just know how to display item and should not care on how to handle anything else)
Extra point :
you can add a next / previous button in your view (perhaps below you RecyclerView in you activity xml), and then you add some logic to them to show next 6 items from list onNextTapped or previous 6 items when tapped previous button like :
In your activity
fun noNextTapped(){
myAdapter.setItems(listOfYour6Items.sublist(6,12))
//of course you need to add your logic to handle edge cases like what if the list size does not have enough items (like if it is only have 10 items) etc , and some other logic on how to know which page you are displaying so you can pass the correct number of items to the adapter onNextTapped fun (each page shows 6 items) I leave those to you
}
in your MyAdapter :
class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
var list List<Objects> ;
fun setItems(new6Items : List<Objects>){
list.clear()
list.addAll(new6Items)
notifyDataSetChanged()
}
...
}
I'm trying to add a feature to a To-Do list that causes a "checked" item to go to the bottom of the the list. I'm using an ArrayList to hold the items which are organized using a RecyclerView so I know that key will be to update the position but I'm not sure how to do that.
I'm confused how you'd move everything up by one and then put the checked item in the last position so that it's at the bottom of the list.
This is how I'm doing the checking and unchecking of the boxes:
taskCheckBox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (holder.taskCheckBox.isChecked())
{
bucketItem.setSelected(false);
holder.taskCheckBox.setChecked(false);
}
else
{
bucketItem.setSelected(true);
holder.taskCheckBox.setChecked(true);
}
}
});
I don't know what I'd add to update the position and I'm still confused by how I always know what the "bottom" is because there isn't a fixed number of item in the list.
Android team added a support for this kind of actions. Check out DiffUtils:
https://github.com/mrmike/DiffUtil-sample
In a nutshell, you only need to provide a proper data set, and let the library figure out what changed.
If you still want to do this manually, you'll need to do something like this:
On click, update the underlying model to have proper selected state
Reorder that item to a proper place in a list. (list.remove(position) and then list.add(item) perhaps)
Call notifyDataSetChanged() on your adapter. (or even better notifyRangeChanged(from, to)
I have a ListView.
I'm using anArrayAdapter and I'd like to:
when the user clicks any item on the list, its LinearLayout (is just a content for x information) fades out and is substituted by other LinearLayout (with y information) which fades in.
However, I do not know how to apply this on my ArrayAdapter. I've searched for a while but I'm not understanding how can I access a single item from the Adapter and make it's children fade out or fade in.
Any help would be very appreciated. Thank you very much for your time.
In general - adapters do not hold items the role of adapter is to get data model, and to produce item views when i.e. list view needs them.
The main concept of replacing item is to replace data inside the adapter and call notifyDataSetChanged(); - thats all.
I don't like ArrayAdapter as it's aimed for very simple scenarios. Usually I just create my own Adapter class extending BaseAdapter (just 4 methods to implement).
The transitions of items on ListView are described here: Adding animation to a List View in Android
This question already has an answer here:
Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
(1 answer)
Closed 10 years ago.
In my code I have a ListActivity. One of the context menu options for a list item is "delete", which opens a dialog confirming the action. I intended to implement this functionality by first deleting the item's data in the database and then removing it from the ArrayAdapter. It is in removing it from the ArrayAdapter that I get an UnsupportedOperationException...
public void onClick(DialogInterface dialog, int id)
{
asynchronousDeleteEntry(CONTEXT_SELECTED_ID);
dialog.dismiss();
//I -know- that the adapter will always be an object
//of ArrayAdapter<JournalEntry> because this is the only type
//I ever call setListAdapter with. Debugging confirms this
#SuppressWarnings("unchecked")
final ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>)
journalViewerListActivity.this.getListAdapter();
//EXCEPTION OCCURS HERE
adapter.remove(adapter.getItem(CONTEXT_SELECTED_POSITION));
//refreshes the ListView to show the new items
adapter.notifyDataSetChanged();
Any help appreciated.
Thanks!
It seems that this problem crops up when you initialize your ArrayAdapter with an array. Try initializing it with a List<JournalEntry>. Reference: Why can't one add/remove items from an ArrayAdapter?
You're trying to modify list which is declared as final. Compiler tried to warn you, but you've suppressed warning by #SuppressWarnings("unchecked")