Get the view of a row in a listview - android

Is there any way to find the view that represents a specific row in a listview.
When the user clicks on a row in my listview I expand it to show more details (using the ExpandAnimation class).
Thing is that when I click on another row I want the previously opened row to close.
So, I need to be able to get at the view (I have the row number within the dataset).
If the row is not on the screen at present then it is not a problem because it will be shrunk down when getView is called.
So, is there any way to get the view by index in list adapter.

Related

Difference between setting onClickListener in adapter and in fragment

In my code I have fragment and gridview in it. Also I have ArrayAdapter for this Gridview. Now I need to change background color of grid cell on click. I do this by setting onClickListener.
Question is what is difference between setting onClickLister for GridView cell in adapter and in fragment?
I'm gonna try to clarify the different android entities involved in your question a little.
A GridView is a View.
You can assign Click Listeners to Views so they react to a user click. Any view has a generic clicklistener (View.setOnClickListener) that gets called whenever the user clicks on any part of the view
Complex views can have several other more specialized clicklisteners, for example, menu-like views (ListViews, GridViews, etc) will have also a setOnItemClickListener / setOnItemLongClickListener that gets called whenever the user clicks on an item (vs. the whole view)
An Adapter is just a class whose purpose is to build views with data to data-consuming views. For example, your GridViewAdapter: It will get called once for every row and it will construct each Row View (in the getView method). Every Row View will be (probably) a ViewGroup (FrameLayout/RelativeLayout...) with some other views inside (ie. Icon ImageView, name TextView, address...)
So the adapter itself doesn't accept clicklisteners. But the Views created by the adapter can! For example, let's assume your GridView is a Phone List:
Your GridView has an ItemClickListener to react to the selected phone list entry and show info about the contact
Your GridView adapter builds views for every row. Imagine your 'contact' rows have 3 views: A title, an Icon, and a button to delete the contact
Inside your adapter, you will assign an onClickListener to the "delete contact" button View. Mind you always assign onClickListeners to Views, not to the adapter itself ("you can't click an adapter!")
About Fragments, think of them as "sub-activities". A fragment contains a root layout with several views. Again, it will be in those views where you assign the clicklisteners, not to the fragment itself.

How to get Expanded child view in ExpandableListView?

I have 2 Button and one ExpandableListView in my activity.On button click i need to get Child views which is expanded in expandable List view. In other way i need to get all the child views which is currently showing in expandable List. For your reference i have added some sample image. in that i have get count button. on click of getcount button i need to get which is expanded(Dell) and its child and that count also.
I would recommend you do not add extra buttons. Without code it is hard to give you an example of what to change in your code.
However, I would suggest you take a look at ExpandableListView#OnChildClickListener. The method should offer the functionality you are looking for. I quote the documentation:
Parameters
parent The ExpandableListView where the click happened
v The view within the expandable list/ListView that was
clicked
groupPosition The group position that contains the child that was
clicked
childPosition The child position within the group
id The row id of the child that was clicked
This should be sufficient to serve your needs!

Can I clear all views in a ListView so they won't be recycled via convertView?

I reuse the same ListView for navigating through data, while updating the ListView data based on the user clicking a row. Up until now I had been using the same row View throughout the activity, but after the user clicks a row, I would like to change to a different row layout for all of the ListView's rows.
To be more concrete, say the ListView contains rows using layout1, and the user clicks on a row. The ListView's array adapter subsequently gets a new set of data and all the rows are now using layout2.
Rather than create a new ListView after the click or mess with setTag ugliness, I'd just like to clear all of the the cached row views that are sent via getView's convertView. I still want to subsequently recycle the layout2 views.
Can I do this, or do I have to "bite the bullet?"
It looks like ListView.invalidateViews() will do this:
public void invalidateViews ()
Causes all the views to be rebuilt and redrawn.

Dynamically add a row in list view and row contains the progress bar and button in android

i am developing an app in which there is a layout which contains 4 rows in a list view and each row contains a progress bar and a button. Below the list view there is a button when i click on this button, a new row should be add in the list view and when i will add multiple rows then there should be no problem in scrolling up and down. I don't know how to add dynamically rows in list view and how to handle scrolling problem. please help me.
Thanks in advance.
List view can contain any number of row it depends upon the array of valuew that you provide in its adapter. if you want to add one more value to list add it to its array which you are supplying to its adapter and just call this methods
adapter.notifyDataSetChanged();
You can see one more row is added to your list view

Not getting focus on list rows in android

I have a list with some rows which define some transaction. When i select the row, particular transaction details is showing in another list. Now when i am selecting a row in the first list, corresponding details is showing in the second list but the first list's selected row is not getting the focus. How can i get the focus?? I am doing this for android tablet and OS is 2.2.
I guess you can use listview.setSelection(int postion)
It Changes the selection state of this view. A view can be selected or not. Note that selection is not the same as focus. Views are typically selected in the context of an AdapterView like ListView or GridView; the selected view is the view that is highlighted.

Categories

Resources