How to get item position in recycler adapter by item's parameter? - android

I have two Fragments. In Fragment A I choose item with its unique ID. In Fragment B I have recycler adapter that consists of those items. I would like to take item from Fragment A, and open Fragment B with recycler adapter set to the position of chosen item. I found that in order to open recycler adapter on specific spot, I have to use something like that rv.getLayoutManager().scrollToPosition(positionInTheAdapter). The question is: can I get that position by item's id? Or maybe there's a better way of what I'm trying to achieve.
Thank you

You must be passing a list of items in Fragment B. Now when you select any item in first Fragment A then all you need to do is run a loop through all list of items passed to adapter and match on which position the item id matches that position can then be passed to rv.getLayoutManager().scrollToPosition(positionInTheAdapter)
For more info on how to pass data among fragments you can check this article.

Related

Get ListView item position by uri / by the data it contains

I have a ListView which I populate using a custom CursorAdapter.
Now I want to manually update just one specific item in the ListView. I have the content URI of that item. Is it possible to use just this info to get the position of the item in the listView?
If I have the position I can do something like
View v = mListView.getChildAt(itemPosition - mListView.getFirstVisiblePosition());
to update the view. But how can I get itemPosition?
I know that I get the position in the onItemClickListener, but I need to update the view without it being clicked.
Any help guys?
ListView does not support updating a single position.
You must update the adapter with the new data (even if you change a single position) and the, invoke mAdapter.notifyDataSetChanged()
If you get the View by its position (via listView.getChildAt()) will work. However, if you scroll up and down the list, that view will display the old data again because the adapter is not aware of the change (and it is the adapter which update the view contect view getView()).
When you invoke notifyDataSetChanged(), you are telling to the adapter that your data set has new info and the ListView/Adapter will re-draw the visible items (it won't re-draw whole list at once.. only the visible items).
You may want to consider to change to RecyclerView in the future. The BaseAdapter used in a RecyclerView support actions such as add/remove/update a single position.

Android cursor move selected to the top

I have a listview that is populated by data from ContactsContract.RawContacts, and I want to have some items to be selected when presented to the user. Is it possible to move the selected items to the top of the list?
Since add view to a specific location in ListView will throw UnsupportedOperationException. We couldn't add or remove in the list view directly, but you could try to get current selected position data, and rearrange the position in adapter's data to the first place. Then call notifyDataSetChanged to update your list view.

Android listview row detail page sliding

I have ListView which has many items(rows), onClick of any item goes to the DetailActivity which explains more about that particular item. If I want to see the detail of the next item, I have to come back to the ListView and click next item. How to design to see the next or previous item's detail from DetailActivity by swiping it left or right.
I assume that you have a Custom Object and ArrayList of this object.
You need to have an adapter to Show this Arraylist into Listview
When you clicked the list item you need to pass your object from ListActivity to your DetailActivity
I think you are doing this fine until this part.
Then now, you can use ViewPager and its adapter in your DetailActivity.
When you click the list item, you need to pass your Arraylist and your object index to the DetailActivity.
In the DetailActivity, get your Arraylist, set your List into ViewPager adapter, find your object via index(that you clicked) and set ViewPager page index as your wanted item index.
If you manage this correctly, you can slide details of your content. You can ask ma anything to make this clear.
There is a tutorial of Using the ViewPager: http://architects.dzone.com/articles/android-tutorial-using
You could go with a ExpandableListView. No need to open a new screen or doing something which becomes hard to manage like Fragments. The user can show/hide detail just by clicking on the item.
A view that shows items in a vertically scrolling two-level list. This
differs from the ListView by allowing two levels: groups which can
individually be expanded to show its children. The items come from the
ExpandableListAdapter associated with this view.

Access to data that is shown by a list view in android

I have a list view in an android program. Each row of this list view has a button. When user clicks one of these buttons, an action must be performed on data that is shown by that row.
How can I get access to that data and manipulate it?
Call getItem() on your Adapter to retrieve whatever the data is that is at that position within the Adapter. So, for example, if your Adapter is an ArrayAdapter<Restaurant>, getItem() will return the Restaurant for a given position.

How can i stop executing getview() method in android?

I have two list view for displaying data from sqlite database.I displayed data without any problem.in second list view i have header with four columns.When i click on first list view , that related data displayed in second list it displayed based on position.By default i displayed first list view of the zero position related data in second list. the problem is , while scrolling the first list view it automatically calls the zero position values but i need the data of whatever position i clicked on first list view.I displayed data like below.How i can do?
Please can any on help?
first list view:
first........
second........
third........
I displayed data based on selection of first list view.
Second list view:
header
id Name Contact
1 ram 12345
Thanking in Advance.
easy bruteforce way is to use flag like this..
public boolean flag=false; // declare this as class variable outside both listview adapter class
then in 1st list view getview() function
if(!flag){
//display 2nd list view contents on 0th position of 1st list
flag=true;
}
in onItemClick() of 1st list view
//display 2nd list view contens based on position

Categories

Resources