The importance of ListViews

Deepa Iyer
Bitscademy
Published in
2 min readSep 24, 2016

Facebook has posts in a ListView, Twitter has tweets in a ListView, even your emails are displayed inside a ListView. Irrespective of device, OS, lists are indispensable to mobile programmers. The reason for this is because listView is the only real scalable presentation framework available to us to work with large quantities of data.

In Android, we use Simple ListViews and ArrayAdapters to display data by a few lines of code. For example in the Activity, we use ListView object myListView. Our data will reside in an ArrayList called listItems. And finally an ArrayAdapter will manage the interactions between our data and the UI. This is why we use ArrayAdapters. Check the 4 comment steps to do this in Android.

public class MainActivity extends Activity {

ListView myListView;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

//1.Datastructure to hold our list data
final ArrayList<String> listItems = new ArrayList<String>();

//2.Adapter to make life easier for us to manage the changes
//in the data and the UI together
final ArrayAdapter<String> aa;

//3.This is how we tie the Array Adapter and the data

aa = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems);

//4.Bind the array adapter to the listview.
myListView.setAdapter(aa);

}

In iOS things are slightly different by default, we use TableViews to make lists. Whenever we work with TableViews we have to help iOS by overriding 2 methods. I find this a very reasonable expectation from the developer. We have to teach the table 2 things: 1. how many rows of data there are and 2. whats inside each cell.

ViewController.h looks like this. We have to implement 2 protocols UITableViewDelegate and UITableViewDataSoure.

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *listItems;
@end

ViewController.m should have the following methods implemented

How many cells are in the table

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{   
return [self.listItems count];
}

What should each cell have. This method is called for each cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier=@"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text=self.listItems[indexPath.row];

return cell;

}

--

--