I would like to create List Activity in Android where each row will contain different layout. For e.g. first row contains an edit text and a text view. Second row contains a button. Third row contains only an image.Thus, each row element as different layout.Please help me with this.
You can create custom adapter, and use a array of layout ids.
In adapter getView method:
public View getView(final int position, View convertView, ViewGroup parent)
you can inflate different layout for different position value.
Implement a custom ListView. In the getView() method, depending on the data being processed, inflate a different layout as required.
If you are using the convertView, I guess you might also want to set a tag to the views being returned from getView() to mark what type of layout it is using. When reusing, you can check the tag, if its not the required type, then inflate a new one, etc.
Related
I am trying to determine the best way to have a single Listview containing different rows styles.
I searched alot and found some tricks using getViewTypeCount() and getItemViewType(int position), to let the listview layout changes depending on position and that was not what I'm looking for.
What I'm trying to do is to change the listview layout depeding on a textview value on the row itself.
I don't understand why you cant achieve this with the methods listed above, if you know how many views you have, then create an .xml for each one of them and then in the getView() inflate the corresponding layout depending on the value of your textview. You then return this row and it should then work.
well, this is exactly what you need to do.
override the getViewTypeCout to return the amount of types you have.override the getItemViewType and determine the type according to the position (or item to show)
after you do that, just use this in your getView like this:
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType(position) == FIRST_VIEW_TYPE) {
// inflate first view
} else {
// inflate seconde view
}
}
I searched alot and found some tricks using getViewTypeCount() and getItemViewType(int position), to let the listview layout changes depending on position and that was not what I'm looking for.
Yes, it is.
What I'm trying to do is to change the listview layout depeding on a textview value on the row itself.
No, you are not. You are trying to "change the listview layout depeding [sic] on" some text that you plan on putting into "a textview value on the row itself". As mr_archano points out, this TextView is not magically getting a value -- it is getting a value based on something you put there by one means or another.
Hence:
Override getViewTypeCount() to return the number of distinct possible row types
Override getItemViewType() to return a value from 0 to getViewTypeCount()-1 based upon the text that you plan on pouring into the TextView
Ensure that your getView() or bindView() implementation is aware of the different view types and handles them accordingly
The contents of the TextView inside the ListView Row must come from some data you have defined (probably an ArrayList or similar I'm assuming), so what you have to do is check what kind of view you want to draw based on that data before drawing the ROW, this is done on the getView() method.
getView() is called when the ListView wants to render a given element, you're thinking it doesn't have to do with position but it does. You know the TextView contents before drawing them, so parse that value and use the type of layout you want in getView dynamically ;).
Read this if it is still not clear: Android custom listview row
Inside my getView method I want to get a specific child view. I know that my adapter holds several items, by doing calling getCount:
getCount();
According to the JavaDoc:
public abstract int getCount ()
Added in API level 1
How many items are in the data set represented by this Adapter.
Now in my getView method:
public View getView(final int position, View convertView, ViewGroup parent) {
View lastAddedItem = parent.getChildAt(getCount()-1);
if(lastAddedItem == null) {
Log.w("ListAdapter", "View is null");
}
}
This message always appears in the LogCat, which means that the View I try to grab is Null.
I've also tried this:
View lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
So how can I grab a specific view from my ViewGroup object?
I know that my adapter holds several items, by doing calling
getCount:
Yes it does, but the getCount()(returning the items in the Adapter) method of an adapter has nothing to do with getting the children of a ListView with getChildAt()(where you should use getChildCount() as this will return the Views present in the ViewGroup).
lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
This should return a non-null view, the last view returned by the getView() method.
I just want to animate a specific View in my ViewGroup
If you are going to animate a list row(or part of that row) when it becomes visible than register a scroll listener for the ListView and start the animation from there when the target view is appearing on the screen.
I don't how are you implemented the rest of the adapter, but if you are doing it in the right way, and I am right, the parent only contains the Views being displayed on the device's screen, and each time you pull down or pull up the list, it modifies the content of those views, that is why you get Null.
What you want to achieve (animate a view), should be done in runtime, checking if the view currently being displayed in the screen should be animated or not.
Hope it helps.
I woud like to create a listview that alternates background images. For example, the first item would have background image a and the second item would have background image b and the third backgroud a. In basic terms I would like help on creating a listview that for every odd item (egg first, third, fith) has a certian background image different to those listview items which are even (egg second, fourth, and sixth listview item). Here's an exampe.
http://www.gadgetreview.com/wp-content/uploads/2011/10/SIRI-Reminders.jpg
In this example the speech bubbles are background image and each different background image is a different listview item.
In your list adapter in the getView method divide the position attribute that gets sent into the method by 2. If the remaining number is 0 than you are in the even row of your listview. Depending on that you can change the layout of your list view item.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position % 2 = 0)
//set layout for even row
}else{
//set layout for odd row
}
Last time that I tried I didn't find an xml parameter to do that, but you can try to use the same workaround used in this question:
Stack Overflow: How do I alternate colors in between Listviews?
you have to make your own custom adapter and then in the following method:
public View getView(int position, View convertView, ViewGroup parent) {
}
you can use position to change the background if position is odd
There is SetEmptyView method in list view use it
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
I've got a Gridview, which gets populated with data from internet. One of the parameters is an id of an element - my goal is to store this id somehow in order to use it in onItemClickListener method.
At first in the activity I pull data with custom components, afterwards I use it adapter's method:
public View getView(int position, View convertView, ViewGroup parent)
Where and how should I keep relation between the grid's elements and actual data ids, so when I click on an grid element, for instance, activity with appropriate id parameter gets launched.
p.s. ID isn't showed in the gridview.
Make a custom object (class) and store all values inside the ArrayList, pass it to the custom adapter to display data inside the getView() method.
And on click method, you can easily have that clicked position object from the ArrayList.
I want to change the background color of one of my views in my listview
myListView.getView(int position, View convertView, ViewGroup parent).setBackgroundColor(0x00FFE303);
I know what position is - this will be my index of the arrayadapter, but I don't know what view and viewgroup are. I have declared them above this line, but I don't know what to initialize them to
help?
I'm not sure you want to be calling getView() manually like that. getView is called automatically by Android when a item in a listview is drawn to the screen. What would be most common to do, would be to extend an Adapter class (like ArrayAdapter) and #Override the getView() method.