Android Development – ListView v/s TableLayout with CheckBoxes

What’s this is all about?

Do you have CheckBox in your ListView’s each row?? Then you’ll know what I mean here. Those who have experience with CheckBox in ListView knows the recycling of objects in ListView more than anyone else.

So what’s this recycle thing going on here??

ListView in general have multile rows in them, sometimes more than what a normal screen can show at one time. As anyone with enough experience with Layouts know that more the Layouts in your activity, more time will it take to load. So to fasten up loading time in ListView, android developers added a recycle feature in it. The feature recycles every row which is not currently displayed on the screen and when the user scrolls or flings loads them back. This feature load the ListView faster and make it look smoother.

So why do I care about what a ListView does, only when I have CheckBox in a row??

You will have to worry about the recycle feature cause you would wanted to persist the state of the CheckBoxes, i.e. when a user selects a CheckBox and scrolls down and up, they should see the CheckBox as selected, but in reality it doesn’t happen. Why? Dude, I just told you, it’s RECYCLED and created again, so there’re new object of Checkbox everytime you scroll or fling.

I bet there must be methods or XML tags in ListView to disable the recycling. 

Go ahead! Buy a magnifier and try to find it, and when you get that, please tell me about that too. I guess you know what I mean here, there’s no method in ListView to disable recycling. No,no the ITEM_VIEW_TYPE_IGNORE constant won’t help you too.

OMG!!! I am doomed now. I had a ListView with CheckBoxes. All the codes ready, what should I do?

No worries! There’re many tricks available to help you, like saving the checkbox data into a boolean array or in SharedPreferences. But the thing is it’s hard to persist the state, as you have to work hard with RecyclerListener in ListView to save and retrieve the CheckBox state.

Then I must suicide now, I have to submit my project tomorrow and I won’t be able to do it. I am gonna die…. (Sorry for being so dramatic 😉 )

Well, you don’t need to die, not at least for this. If you have read the Title of this blog properly, you’ll come to know what I mean. There’s a rescue for you. TableLayout!!!!

Erm… How will I be able to Use it??

It’s easy. If you have even a little bit experience with dynamic Layout creation, you’ll be able to handle it easily, don’t worry if you don’t know what it means. I’ll give you a sample code to work on too.

I heard sample code…

Here you go –

XML:-

<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TableLayout  
android:id="@+id/table"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"> 
</TableLayout> 
</ScrollView> 

Code excerpt in Java:-

TableLayout table=(TableLayout)findViewByid(R.id.table);
ArrayList<CheckBox> cbData=new ArrayList<CheckBox>(); 
for(int i=0;i<rowCount;i++){
 TextView t1=new TextView(this); 
 t1.setText("Text1");
 TextView t2=new TextView(this);
 t2.setText("Text2");  
 CheckBox cb=new CheckBox(this); 
 TableRow row=new TableRow(this); 
 row.addView(t1); 
 row.addView(t2); 
 row.addView(cb); 
 cbData.add(cb); 
 table.addView(row);
}

I didn’t got it :/

Well I’ve created XML layout which you need to set in the setContentView(HERE). Although it’s just part of it, as you still need to put it into some Layout for proper formatting. The Tablelayout is been put into a vertical ScrollView, to have a scrolling effect on it. In the java file, we run a loop for every row we need. We created 2 textviews and one checkbox per row, set value in them, created a TableRow, added our components to the row and added the row to the TableLayout. Interesting thing to note here is, we used ArrayList to have a list of objects for the CheckBoxes being used. You can get data,set clicks, listens to the event, etc. using it and you could use ArrayList for any component you need to access later in the activity.

What benefit I got using TableLayout instead of ListView?

GOSH!! How many times I have to tell you. It’s recycling thing we have discussed earlier :p . However the real thing here is, the code you have seen above may be all you need to have a working scolling list. That’s it, no Custom Adapter, no different XML for each component, and no caching code at all. The only thing defers our own list from the original list is, we don’t have that yellow highlighting effect on touch for each row yet, but I am sure you’ll be able to find a better work around (Don’t expect me to do your homework or you too, how will you learn then 😉 )

Is there any drawbacks of using it? I mean they always creep in to good hacks.

You are a smart fellow, ain’t you? Well yeah, there is one drawaback, it doesn’t recycle your rows xD .

Err… what do you mean? I thought I wanted to disable recycling.

yes you wanted to, but think about it, why would Google put recycle feature in it? To save the initial loading time and free up unsued resources. Thus, as the Layout theory says, “More the Layouts you have, More you’ll regret it having”. Which means, every increase in Layout count, makes your activity to wait more for loading. There’re work arounds available, such as Lazy-Loading (Don’t worry I’ll cover it in next blog) or Delayed-Loading. However untill you have more than 30 rows, you won’t care for it.

Thank you so much, you saved my life. How can I contribute back?

Click on any of the share button available and share this post with your friends, collegues or anyone who can get benefit with it. You can post your questions and comments below and, can also vote up this post.

I want to hear more from you.

You can check my stackoverflow profile and look for answers I posted there (probably vote on them too 😉 ). You can also ask your questions there and provide me a link to answer them. If you want a complete blog post on them, leave me a comment, and I’ll get back to you soon.

Thanks for listening,

Abhi,

The Creator

profile for Creator at Stack Overflow, Q&A for professional and enthusiast programmers

Advertisements

3 comments on “Android Development – ListView v/s TableLayout with CheckBoxes

    • That many TableLayouts and TableRows wil increase the loading time of the activity significantly. Also If you’ve invalidated the Layouts in activity, it’ll slow down the FPS. It’s better to use Custom Canvas for better performance or if the performance slowdown is acceptable to you, you can use Layouts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s