teaching machines

CS 491 Lecture 7 – List Views

September 24, 2013 by . Filed under cs491 mobile, fall 2013, lectures.

Agenda

TODO

Mobile User Interface

Take a moment to think about what your body does when working on a mobile device. How’s interacting with a mobile device different from interacting with a desktop user interface?

Code

ViewController.m

//
//  ViewController.m
//  Tlink
//
//  Created by Johnson, Christopher R on 9/24/13.
//  Copyright (c) 2013 Johnson, Christopher R. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic, readonly) IBOutlet UITableView *tableView;
@end

@implementation ViewController

@synthesize tableView = _tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 26 * 26;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mosherCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mosherCell"];
    }
    
    char a = indexPath.row / 26 + 'a';
    char b = indexPath.row % 26 + 'a';
    cell.textLabel.text = [self urlForLetters:a second:b];
    
    return cell;
}

- (NSString *)urlForLetters:(char)a second:(char)b {
    return [NSString stringWithFormat:@"http://www.%c%c.com", a, b];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    char a = indexPath.row / 26 + 'a';
    char b = indexPath.row % 26 + 'a';
    NSString *urlAsString = [self urlForLetters:a second:b];
    NSURL *url = [NSURL URLWithString:urlAsString];
    [[UIApplication sharedApplication] openURL:url];
    
}

@end

activity_main.xml

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
  <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"></ListView>
</RelativeLayout>

MainActivity.java

package org.twodee.tlink;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String[] urls = new String[26 * 26];
    int i = 0;
    for (char a = 'a'; a <= 'z'; ++a) {
      for (char b = 'a'; b <= 'z'; ++b) {
        urls[i] = String.format("http://www.%c%c.com", a, b);
        ++i;
      }
    }

    ListView listView = (ListView) findViewById(R.id.listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, urls);
    listView.setAdapter(adapter);
    
    listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> collectionView,
                              View itemView,
                              int position,
                              long id) {
        // open up a browser
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urls[position]));
        startActivity(intent);
      }
    });
  }
}

Haiku

Rough morning commute
I had to scroll through ten things
Not feeling lucky