Android ListView with custom adapter aligning specific rows - android

I am already familiar with the basics and am pretty close to what I want to achieve. I want to have two different layouts in total for the rows but don't want it to be a set pattern of alternation. There could be any pattern for the two row layouts.
My question is how do I use two different xml layouts depending on the information that is going to be displayed? Or if possible, use a single layout and work with visibility for simplicity (current implementation).
Is it possible to send another parameter into getViewItemType()?

There is no way you can set another parameter into getItemViewType (int position) as it only takes one parameter and returns the view that is created by getView(int position, View convertView, ViewGroup parent) on a specific item.
I wolud recommend you using the setVisibility() on a specofic xml Layout which you don't want to display.
Or you can just maintain a small SQliteDatabase for storing what all the items you want to make it visible and call the adapter according to that value in the database.
Hope this helps.

In android adapter any time you can call "getItem()" and with help of that you can decide which layout to show.
Don't forget to override getItem() in your adapter class.

Related

Android RecycleView: Adding dynamic views

I have decided to upgrade my code to use RecyclerView now instead of the list I am using now.
As I understand the following is how it normally goes:
onCreateViewHolder - this inflates a view, and does the findviewbyid and returns it as a ViewHolder object for the view
onBindViewHolder - this assigns the view holder values to the position view (which is being recycled).
This is all fine and dandy... If the views in the list contain the same fields...
My views however in my list are different, before I programmatically added views depending on the adapter List (in the getView method).
Is there a "good practice" way to handle this, I can't think of a good way to get around this.
getViewItemType does not work as the views are unique and that they are not predefined.
This is what you need to use when you different types of Views inside a ListView or RecyclerView :-
getItemViewType() and getViewTypeCount()
First you need to use getViewTypeCount() and return the number of unique views you need inside your List. Then override getItemViewType() and return the View type you want to inflate inside the List row.
Hope it will help.
If you still have any issue and need a working example, let me know, i'll update my answer.

Android Viewholder implementation

I understand the idea and usage of Viewholder pattern, but still I have one question:
Assume we have one TextView in the viewholder, and 10 items to display ("item0, item1....").
If I call findViewById once, as I understand I have one object of that TextView.
So at first call to getView I inflate the view, find the reference and set text "item0".
At second call I get same TextView and set text "item1" to the same TextView.
Why item 0 text doesn't change?
Is there any cloning in the background?
Is there any cloning in the background?
Android preallocate a number of views that are enough to fill the screen of the device where you are running the app ( a pool of views ), identical from the content perspective but differently from the reference perspective
Assuming that you implement your ViewHolder inside an adapter class and you use the holder in the getView() method, the only thing that is for sure, is that the TextView in your case , describe a slot of the parent structure (e.g. ListView). Once you have defined the slot in an xml, that is inflated from your adapter, there is no cloning or something like that.
According to Google Documentation the holder idea is described as :
Your code might call findViewById() frequently during the scrolling of
ListView, which can slow down performance. Even when the Adapter
returns an inflated view for recycling, you still need to look up the
elements and update them. A way around repeated use of findViewById()
is to use the "view holder" design pattern.
A ViewHolder object stores each of the component views inside the tag
field of the Layout, so you can immediately access them without the
need to look them up repeatedly. First, you need to create a class to
hold your exact set of views.
There is not cloning , only reusability of the view

Which is better for list View in getView(), Inflating view or Creating View by coding

I have one question for list view. At the time of creating list item in getView() method, which is a good option for list view. Creating views through coding or inflating view through xml. I am thinking about memory utilization & performance of list view.
Normally list item contain one product image with their name & 3 line description. Means one Image View & two text view.
Creating Views through code is usually not recommended and is justified only in cases when you can't deal without it. Using XML is always best practice so you should use this approach no matter where you're using your Views. Hope this helps.
You need to recycle views. Are you using a VieHolder?. My suggestion is use a view holder inflate using xml. ViewHolder will increase performance of ListView.
http://www.youtube.com/watch?v=wDBM6wVEO70. Check this link.

Adding multiple views to a listview

I tried to add these views to list view using this kind of factory but everytime I try and add the view to a ListActivity, it comes up with nothing. What am I doing wrong? I set my list views like so:
List<View> views = new ArrayList<View>();
for(int x =0;x<tagg_views.size();x++){
lv.addHeaderView(views.get(x));
}
It looks like you are trying to add x number of headers to your ListView. That doesn't make sense.
A ListView should contain x number of copies of the same view, with different information on each line.
Hello ListView gives a good example of the correct usage of a ListView.
Why are you adding the Views to the list yourself? I would highly recommend using any kind of apropriate Adapter for the List. The adapter will handle the creating and recycling of views while the user is scrolling etc. If you use an Adapter it is discouraged to save references to the view yourself like you are doing it in the views list.
The addHeaderView method you are using is made to one single header to the list that always will appear on the top of the list. This means calling it in a loop will not have a reasonable result.
Look into the helloListView example Mayra mentions to understand how a list in android is working. To see how a custom listadapter works have a look at this tutorial looks promising despite the bad code formatting.
A ListView is linked with and Adapter. The Adapter is responsible for the data displayed in the ListView. Take into account that internally ListView creates a pool of itmes (or a pool for each type of item that can be displayed in your case).
For this purpose your adapter needs to implement the following methods:
int getItemViewType(int position): Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item. So you need to identify you types.
int getViewTypeCount(): Returns the number of types of Views that will be created by getView(int, View, ViewGroup). This is used to create a pool for each type of item.

Creating multiple objects of a view defined in the xml

I have to dynamically add a list of views (the views use RelativeLayout). Can I do this by specifying the view definition in xml and instantiate multiple objects off it? This gives the usual benefits of separating the view part from the code (for e.g., making it easy for the UI guys to alter things) or Is going the ArrayAdapter the suggested/only route?
are you saying that you want to do this?
View v1 = (View) findViewById(R.id.someView);
View v2 = (View) findViewById(R.id.someView);
If you do this, you will merely have 2 references to the same view; it does not create two separate View objects. However, if you want to make a vertical list of views, look into ListActivity. in this case you will make a layout xml that will be used for every item in the list. you will need to implement a ListAdapter, or use a SimpleArrayAdapter.
does that help?

Categories

Resources