Android TV: How to notify Card Presenter items change - android

So, I'm making a screen with Leanback's Browse Fragment and CardPresenter.
Inside my fragment that extends BrowseFragment, I have a method for drawing the UI:
private void loadCardRows() {
mRowsAdapter = new CustomArrayObjectAdapter(new ListRowPresenter());
final List<UiType> uiTypeList = new ArrayList<>(uiTypes);
for (UiType uiType : uiTypeList) {
HeaderItem cardPresenterHeader = new HeaderItem(0, uiType.getName());
List<TypeReportItem> items = performUiTypeFiltering(uiType.getEndpointType());
CardPresenter cardPresenter = new CardPresenter(attributesHelper);
CustomArrayObjectAdapter cardRowAdapter = new CustomArrayObjectAdapter(cardPresenter);
for (TypeReportItem item : items) {
cardRowAdapter.add(item);
}
mRowsAdapter.add(new ListRow(cardPresenterHeader, cardRowAdapter));
}
setAdapter(mRowsAdapter);
}
Now I'm having a service that loads some data every few seconds. That data is reachable through attributesHelper that I'm passing to CardPresenter.. How am I supposed to reload that data without causing the screen to blink every few seconds?

mRowAdapter.notifyArrayItemRangeChanged(startingIndex, mRowAdapter.size());
Starting index is the one from which position your are updating data.Dont give starting index something like 0,which will result in screen blink from index 0

Maybe this anwser will be helpful
for (int i = 0; i < mAdapter.size(); i++) {
ListRow listRow = ((ListRow) mAdapter.get(i));
ArrayObjectAdapter listRowAdapter = ((ArrayObjectAdapter) listRow.getAdapter());
if (listRowAdapter.size() > 0) {
listRowAdapter.notifyArrayItemRangeChanged(0, listRowAdapter.size());
}
}
android tv -Reloading adapter data
I have used this code in my project. No blink happened.

notifyArrayItemRangeChanged
caused blinking, so try this Adapter
class RefreshableArrayObjectAdapter(presenterSelector: PresenterSelector) :
ArrayObjectAdapter(presenterSelector) {
fun refresh() {
notifyChanged()
}
}

Related

Adding a Second Presenter to the ArrayObjectAdapter Instance

I am using my custom fragment instead of the RowsFragment in my Android TV app which implements the leanback library. The custom fragment consists of two equally sized horizontal LinearLayouts (upper and lower halves of the screen) in a FrameLayout. The lower part displays a grid of cards using a VerticalGridPresenter.
What I want to accomplish is to update the views on the upper part of the screen when the user navigates between the cards. I can do so by using the methods below. However, I would like to know whether it would be a better/cleaner approach to update the upper part of the fragment (screen) using another presenter. (ActualShowInformationPresenter) How can I add a second presenter to the ArrayObjectAdapter instance? Would that be the correct approach? Thanks in advance.
private void gridOnItemSelected(int position) {
if (position != mSelectedPosition) {
mSelectedPosition = position;
Channel c = (Channel) mAdapter.get(position);
//ActualShowInformationPresenter actualShowInformationPresenter = new ActualShowInformationPresenter();
Context context = getMainFragmentAdapter().getFragment().getContext();
if (c.getProgramme()!= null)
tvActualShowTitle.setText(c.getProgramme().get(0).getTitle().get(0));
}
}
}
...
private OnItemViewSelectedListener mOnItemViewSelectedListener = new OnItemViewSelectedListener() {
#Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
int position = mGridViewHolder.getGridView().getSelectedPosition();
Log.v(TAG, "grid selected position " + position);
gridOnItemSelected(position);
}
};
...
public void setGridPresenter(VerticalGridPresenter gridPresenter) {
if (gridPresenter == null) {
throw new IllegalArgumentException("Grid presenter may not be null");
}
mGridPresenter = gridPresenter;
mGridPresenter.setOnItemViewSelectedListener(mOnItemViewSelectedListener);
if (mOnItemViewClickedListener != null) {
mGridPresenter.setOnItemViewClickedListener(mOnItemViewClickedListener);
}
}
...
private void setupAdapter() {
VerticalGridPresenter presenter = new VerticalGridPresenter(ZOOM_FACTOR);
presenter.setNumberOfColumns(COLUMNS);
setGridPresenter(presenter);
CardPresenter cardPresenter = new CardPresenter();
mAdapter = new ArrayObjectAdapter(cardPresenter);
setAdapter(mAdapter);
}

How to use setRowViewSelected of ListRowPresenter

I am using default project for Android TV. Following is the code for creating cards in my BrowseFragment:
private void loadRows() {
List<Movie> list = MovieList.setupMovies();
ListRowPresenter mListRowPresenter = new ListRowPresenter();
mRowsAdapter = new ArrayObjectAdapter(mListRowPresenter);
mListRowPresenter.setRowViewSelected(/*HOW TO GET VIEWHOLDER HERE?*/, false);
CardPresenter cardPresenter = new CardPresenter();
int i;
for (i = 0; i < NUM_ROWS; i++) {
if (i != 0) {
Collections.shuffle(list);
}
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
for (int j = 0; j < NUM_COLS; j++) {
listRowAdapter.add(list.get(j % 5));
}
HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
setAdapter(mRowsAdapter);
}
I am doing this as I don't want to make first card of row get selected when I launch app. It should only get selected after user press down button on Dpad. If I can't do it this way, what should I do to get mentioned behavior?
You can setRowViewSelected by subclassing ListRowPresenter and overriding initializeRowViewHolder(RowPresenter.ViewHolder holder)
#Override
protected void initializeRowViewHolder(RowPresenter.ViewHolder holder) {
super.initializeRowViewHolder(holder);
setRowViewSelected(holder, false);
}
But I don't think you can unselect all items in BrowseFragment using this approach.
Try setting your ItemViewSelectedListener after your data is loaded instead of setting in onActivityCreated to have all items unselected on initial launch.
Possible reason why top left item of row will always get selected by default and you cannot have all unselected items on initial launch:
BrowseFragment's onItemSelected method (line 1372-1382) on initial launch calls mMainFragmentRowsAdapter.getSelectedPosition()
#Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
int position = mMainFragmentRowsAdapter.getSelectedPosition(); //<--
if (DEBUG) Log.v(TAG, "row selected position " + position);
onRowSelected(position);
if (mExternalOnItemViewSelectedListener != null) { //<--
mExternalOnItemViewSelectedListener.onItemSelected(itemViewHolder, item,
rowViewHolder, row);
}
}
where getSelectedPosition() always returns 0
(line 483-485)
public int getSelectedPosition() {
return 0;
}
It also calls mExternalOnItemViewSelectedListener.onItemSelected where mExternalOnItemViewSelectedListener is the ItemViewSelectedListener that you set in MainFragment of your app.
So on initial launch, 0th item in 0th row gets selected as a default selected item but if you delay setting mExternalOnItemViewSelectedListener this call will not reach your item selected listener the first time.
you can use this callback method .
void onRowViewSelected (RowPresenter.ViewHolder vh,
boolean selected)
Called when the given row view changes selection state. A subclass may override this to respond to selected state changes of a Row. A subclass may make visual changes to Row view but must not create animation on the Row view.
mListRowPresenter.setRowViewSelected(vh, false);
why you are deselecting initially ? i didn't get your Question Clearly can you please explain what you want to do Exactly ??

Android FireBase change Listview order

My application is returning the latest data from firebase to the buttom of the ListView. But I want it to be on the top! I have thought about it and I think there is only two possible ways to do it.
1. Invert the Listview.
I think that this way is how it should be done but I couldn't figure it out. I have searched a lot on the web but no suitable solution for my case
This is my adapter code
public void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
final ListView listView = getListView();
// Tell our list adapter that we only want 50 messages at a time
mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, mUsername);
listView.setAdapter(mChatListAdapter);
}
And this is the code for the ChatListAdapter constructor for a custom list class ChatListAdapter which extends special list adapter class FirebaseListAdapter:
public ChatListAdapter(Query ref, Activity activity, int layout, String mUsername) {
super(ref, Chat.class, layout, activity);
this.mUsername = mUsername;
}
[Edit] This is some of the code for FirebaseListAdapter which extends BaseAdapter class
public FirebaseListAdapter(Query mRef, Class<T> mModelClass, int mLayout, Activity activity) {
this.mRef = mRef;
this.mModelClass = mModelClass;
this.mLayout = mLayout;
mInflater = activity.getLayoutInflater();
mModels = new ArrayList<T>();
mModelKeys = new HashMap<String, T>();
// Look for all child events. We will then map them to our own internal ArrayList, which backs ListView
mListener = this.mRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
T model = dataSnapshot.getValue(FirebaseListAdapter.this.mModelClass);
mModelKeys.put(dataSnapshot.getKey(), model);
// Insert into the correct location, based on previousChildName
if (previousChildName == null) {
mModels.add(0, model);
} else {
T previousModel = mModelKeys.get(previousChildName);
int previousIndex = mModels.indexOf(previousModel);
int nextIndex = previousIndex + 1;
if (nextIndex == mModels.size()) {
mModels.add(model);
} else {
mModels.add(nextIndex, model);
}
}
}
2. Descending query the data.
The second way seams impossible to me, because when I searched on Firebase API documentation and on the web, I couldn't find anyway to order retraived data on descending way.
My data on firebase look like the following:
glaring-fire-9714
chat
-Jdo7-l9_KBUjXF-U4_c
author: Ahmed
message: Hello World
-Jdo71zU5qsL5rcvBzRl
author: Osama
message: Hi!
Thank you.
A simple solution would be to manually move the newly added data to the top of the listview. As you rightly noticed, new data added to a listview will automatically be appended to the bottom of the list, but you may freely move entries once they are added. Something like the following would help you manually move the newest entry to the top of the list:
int iSwapCount = listView.getCount() - 1;
int iPosition = listView.getCount() - 1;
for (int j = 0; j < iSwapCount; j++)
{
Collections.swap(yourlistobject, iPosition, iPosition - 1);
iPosition = iPosition - 1;
}
The above code will begin by calculating the number of swaps that will be required to move last list entry to the top of the list, which is determined by the number of elements in the list - 1. The same is true for calculating the last position in the list. From there Collections.swap will be used to swap the last element in the list with the element before it; this will be repeated until the last element is now the first element, with the rest of the entries in the list remaining in the same order. This code would have to be called each time a new entry is added so that the overall order of the list is maintained.
I realize it has been a while since you asked but I had the same issue. It does not appear that there is a direct answer here.
Here's the change to the firebase adapter to get new items on the top of the list.
Notice the change from add(...) to add(0,...) and add(next...) to add(prev...)
Look for comments:
// prepend instead append
Example:
...
// Insert into the correct location, based on previousChildName
if (previousChildName == null) {
mModels.add(0, model);
mKeys.add(0, key);
} else {
int previousIndex = mKeys.indexOf(previousChildName);
int nextIndex = previousIndex + 1;
if (nextIndex == mModels.size()) {
//mModels.add(model);
//mKeys.add(key);
// prepend instead append
mModels.add(0,model);
mKeys.add(0,key);
} else {
//mModels.add(nextIndex, model);
//mKeys.add(nextIndex, key);
// prepend instead append
mModels.add(previousIndex, model);
mKeys.add(previousIndex, key);
}
}
...
Here is a simple way to invert a FirebaseUI list using a RecyclerView:
boolean reverseList = true;
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, reverseList);
if (reverseList) {
manager.setStackFromEnd(true);
}
mRecyclerView.setLayoutManager(manager);

Move item to top of listview when a new message arrives

I am working on a XMPP based chat in android.. and I am struck at a point where I need to update the position of an item in the listview to the top in case a new.message arrives.
The use case is.. I am on Contacts screen of the app and a new message comes.. so this contact should move to top of the list and get bold. This is what is similar to whatsapp as well
How can this be done. My class imolemebts activity and i have implemented custom list adapter.
So howcan I find if an item exists in the listview and secondly how to dynamically change position
First, keep in mind that a ListView is just a representation of a list of Objects. So if you want to know if an item is in the ListView, you just have to check if the corresponding Object is in your list of Objects.
Is the same when you want to change the position of one item, you have to change the position of the Object in the list.
Start by defining these objects:
private ArrayList<MyObject> lists = new ArrayList<MyObject>();
private MyCustomAdapter myAdapter;
The first time you create your ListView, just do as usually:
//fill your list with your objects
lists.add(myObject1);
lists.add(myObject2);
lists.add(myObject3);
//create and set the adapter
myAdapter = new MyCustomAdapter(..., ..., lists);
myListView.setAdapter(myAdapter);
Now you can know if your lists contains a specific object (which is the same that checking if an item is in your ListView) by simply testing that:
lists.contains(anObject);
Then, if you want to change the position of a specific item in the ListView, you have to create a new list and put the elements in the correct order. You can use something like that (not tested but it should work):
private ArrayList<MyObject> moveItemToTop(ArrayList<MyObject> lists, int positionOfItem) {
if (lists == null || positionOfItem < 0 || positionOfItem >= lists.size()) {
return lists;
}
ArrayList<MyObject> sortedList = new ArrayList<MyObject>();
//add the item to the top
sortedList.add(lists.get(positionOfItem));
for (int i=0; i<lists.size(); i++) {
if (i != positionOfItem) {
sortedList.add(lists.get(i));
}
}
return sortedList;
}
Or even this (which is way easier...).
Finally, call these two methods to update your ListView:
myAdapter = new MyCustomAdapter(..., ..., moveItemToTop(lists, itemPosition));
myListView.setAdapter(myAdapter);
This is how I resolved it
private void moveMessageToTop(MessageObject message) {
int index = 0;
for (Friends friend : mFriends) {
if (friend.getName().equalsIgnoreCase(message.getFrom().split("#")[0])) {
index = mFriends.indexOf(friend);
break;
}
}
if (index != 0) {
mFriends.add(0,new Friends(message.getFrom().split("#")[0], message
.getMessage()));
} else {
Friends frnd = mFriends.get(index);
frnd.setStatus(message.getMessage());
mFriends.add(0, frnd);
mFriends.remove(index);
}
((ListAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
}

Set selected item of spinner programmatically

I am working on an android project and I am using a spinner which uses an array adapter which is populated from the database.
I can't find out how I can set the selected item programmatically from the list. For example if, in the spinner I have the following items:
Category 1
Category 2
Category 3
How would I programmatically make Category 2 the selected item when the screen is created. I was thinking it might be similar to c# I.E Spinner.SelectedText = "Category 2" but there doesn't seem to be any method similar to this for Android.
Use the following:
spinnerObject.setSelection(INDEX_OF_CATEGORY2).
No one of these answers gave me the solution, only worked with this:
mySpinner.post(new Runnable() {
#Override
public void run() {
mySpinner.setSelection(position);
}
});
public static void selectSpinnerItemByValue(Spinner spnr, long value) {
SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
for (int position = 0; position < adapter.getCount(); position++) {
if(adapter.getItemId(position) == value) {
spnr.setSelection(position);
return;
}
}
}
You can use the above like:
selectSpinnerItemByValue(spinnerObject, desiredValue);
& of course you can also select by index directly like
spinnerObject.setSelection(index);
Some explanation (at least for Fragments - never tried with pure Activity). Hope it helps someone to understand Android better.
Most popular answer by Arun George is correct but don't work in some cases. The answer by Marco HC uses Runnable wich is a last resort due to additional CPU load. The answer is - you should simply choose correct place to call to setSelection(), for example it works for me:
#Override
public void onResume() {
super.onResume();
yourSpinner.setSelection(pos);
}
But it won't work in onCreateView(). I suspect that is the reason for the interest to this topic.
The secret is that with Android you can't do anything you want in any method - oops:( - components may just not be ready. As another example - you can't scroll ScrollView neither in onCreateView() nor in onResume() (see the answer here)
To find a value and select it:
private void selectValue(Spinner spinner, Object value) {
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).equals(value)) {
spinner.setSelection(i);
break;
}
}
}
Why don't you use your values from the DB and store them on an ArrayList and then just use:
yourSpinner.setSelection(yourArrayList.indexOf("Category 1"));
The optimal solution is:
public String[] items= new String[]{"item1","item2","item3"};
// here you can use array or list
ArrayAdapter adapter= new ArrayAdapter(Your_Context, R.layout.support_simple_spinner_dropdown_item, items);
final Spinner itemsSpinner= (Spinner) findViewById(R.id.itemSpinner);
itemsSpinner.setAdapter(adapter);
To get the position of the item automatically add the following statement
itemsSpinner.setSelection(itemsSpinner.getPosition("item2"));
You can make a generic method for this kind of work as I do in my UtilityClass which is
public void SetSpinnerSelection(Spinner spinner,String[] array,String text) {
for(int i=0;i<array.length;i++) {
if(array[i].equals(text)) {
spinner.setSelection(i);
}
}
}
You can easily set like this: spinner.setSelection(1), instead of 1, you can set any position of list you would like to show.
I have a SimpleCursorAdapter so I have to duplicate the data for use the respose in this post. So, I recommend you try this way:
for (int i = 0; i < spinnerRegion.getAdapter().getCount(); i++) {
if (spinnerRegion.getItemIdAtPosition(i) == Integer
.valueOf(signal.getInt(signal
.getColumnIndexOrThrow("id_region")))) {
spinnerRegion.setSelection(i);
break;
}
}
I think that is a real way
In Kotlin I found a simple solution using a lambda:
spinnerObjec.post {spinnerObjec.setSelection(yourIndex)}
This is what I use in Kotlin:
spinner.setSelection(resources.getStringArray(R.array.choices).indexOf("Choice 1"))
I know that is already answered, but simple code to select one item, very simple:
spGenre.setSelection( ( (ArrayAdapter) spGenre.getAdapter()).getPosition(client.getGenre()) );
This is work for me.
spinner.setSelection(spinner_adapter.getPosition(selected_value)+1);
This is stated in comments elsewhere on this page but thought it useful to pull it out into an answer:
When using an adapter, I've found that the spinnerObject.setSelection(INDEX_OF_CATEGORY2) needs to occur after the setAdapter call; otherwise, the first item is always the initial selection.
// spinner setup...
spinnerObject.setAdapter(myAdapter);
spinnerObject.setSelection(INDEX_OF_CATEGORY2);
This is confirmed by reviewing the AbsSpinner code for setAdapter.
If you have a list of contacts the you can go for this:
((Spinner) view.findViewById(R.id.mobile)).setSelection(spinnerContactPersonDesignationAdapter.getPosition(schoolContact.get(i).getCONT_DESIGNATION()));
for (int x = 0; x < spRaca.getAdapter().getCount(); x++) {
if (spRaca.getItemIdAtPosition(x) == reprodutor.getId()) {
spRaca.setSelection(x);
break;
}
}
Most of the time spinner.setSelection(i); //i is 0 to (size-1) of adapter's size
doesn't work. If you just call spinner.setSelection(i);
It depends on your logic.
If view is fully loaded and you want to call it from interface I suggest you to call
spinner.setAdapter(spinner_no_of_hospitals.getAdapter());
spinner.setSelection(i); // i is 0 or within adapter size
Or if you want to change between activity/fragment lifecycle, call like this
spinner.post(new Runnable() {
#Override public void run() {
spinner.setSelection(i);
}
});
Here is the Kotlin extension I am using:
fun Spinner.setItem(list: Array<CharSequence>, value: String) {
val index = list.indexOf(value)
this.post { this.setSelection(index) }
}
Usage:
spinnerPressure.setItem(resources.getTextArray(R.array.array_pressure), pressureUnit)
This Worked For me:
mySpinner.post(new Runnable() {
#Override
public void run() {
mySpinner.setSelection(position);
spinnerAdapter.notifyDataSetChanged();
}
});
Yes, you can achieve this by passing the index of the desired spinner item in the setSelection function of spinner. For example:
spinnerObject.setSelection(INDEX_OF_CATEGORY).
In my case, this code saved my day:
public static void selectSpinnerItemByValue(Spinner spnr, long value) {
SpinnerAdapter adapter = spnr.getAdapter();
for (int position = 0; position < adapter.getCount(); position++) {
if(adapter.getItemId(position) == value) {
spnr.setSelection(position);
return;
}
}
}
I had made some extension function of Spinner for loading data and tracking item selection.
Spinner.kt
fun <T> Spinner.load(context: Context, items: List<T>, item: T? = null) {
adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, items).apply {
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
if (item != null && items.isNotEmpty()) setSelection(items.indexOf(item))
}
inline fun Spinner.onItemSelected(
crossinline itemSelected: (
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) -> Unit
) {
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
itemSelected.invoke(parent, view, position, id)
}
}
}
Usaage Example
val list = listOf("String 1", "String 2", "String 3")
val defaultData = "String 2"
// load data to spinner
your_spinner.load(context, list, defaultData)
// load data without default selection, it points to first item
your_spinner.load(context, list)
// for watching item selection
your_spinner.onItemSelected { parent, view, position, id ->
// do on item selection
}
This worked for me:
#Override
protected void onStart() {
super.onStart();
mySpinner.setSelection(position);
}
It's similar to #sberezin's solution but calling setSelection() in onStart().
I had the same issue since yesterday.Unfortunately the first item in the array list is shown by default in spinner widget.A turnaround would be to find the previous selected item with each element in the array list and swap its position with the first element.Here is the code.
OnResume()
{
int before_index = ShowLastSelectedElement();
if (isFound){
Collections.swap(yourArrayList,before_index,0);
}
adapter = new ArrayAdapter<String>(CurrentActivity.this,
android.R.layout.simple_spinner_item, yourArrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item; yourListView.setAdapter(adapter);
}
...
private int ShowLastSelectedElement() {
String strName = "";
int swap_index = 0;
for (int i=0;i<societies.size();i++){
strName = yourArrayList.get(i);
if (strName.trim().toLowerCase().equals(lastselectedelement.trim().toLowerCase())){
swap_index = i;
isFound = true;
}
}
return swap_index;
}

Categories

Resources