Skip to content

Commit 1a03668

Browse files
committed
ListView + BaseAdapter + Data Object
First sample of android ListView with custom BaseAdapter class and a custom model (POJO in Java)
1 parent 8dd4cd8 commit 1a03668

File tree

29 files changed

+711
-0
lines changed

29 files changed

+711
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Built application files
2+
*.apk
3+
*.ap_
4+
5+
# Files for the Dalvik VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
10+
11+
# Generated files
12+
bin/
13+
gen/
14+
15+
# Gradle files
16+
.gradle/
17+
build/
18+
/*/build/
19+
20+
# Local configuration file (sdk path, etc)
21+
local.properties
22+
23+
# Proguard folder generated by Eclipse
24+
proguard/
25+
26+
# Log Files
27+
*.log
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="java-gradle" name="Java-Gradle">
5+
<configuration>
6+
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
7+
</configuration>
8+
</facet>
9+
</component>
10+
<component name="NewModuleRootManager" inherit-compiler-output="true">
11+
<exclude-output />
12+
<content url="file://$MODULE_DIR$">
13+
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
14+
</content>
15+
<orderEntry type="inheritedJdk" />
16+
<orderEntry type="sourceFolder" forTests="false" />
17+
</component>
18+
</module>
19+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="MyApplication" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="android-gradle" name="Android-Gradle">
5+
<configuration>
6+
<option name="GRADLE_PROJECT_PATH" value=":app" />
7+
</configuration>
8+
</facet>
9+
<facet type="android" name="Android">
10+
<configuration>
11+
<option name="SELECTED_BUILD_VARIANT" value="debug" />
12+
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
13+
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
14+
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
15+
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
16+
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
17+
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
18+
<option name="ALLOW_USER_CONFIGURATION" value="false" />
19+
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
20+
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
21+
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
22+
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
23+
</configuration>
24+
</facet>
25+
</component>
26+
<component name="NewModuleRootManager" inherit-compiler-output="false">
27+
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
28+
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" />
29+
<exclude-output />
30+
<content url="file://$MODULE_DIR$">
31+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
32+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/debug" isTestSource="false" generated="true" />
33+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/debug" isTestSource="false" generated="true" />
34+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
35+
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" />
36+
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/generated/debug" type="java-resource" />
37+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/debug" isTestSource="true" generated="true" />
38+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
39+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
40+
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
41+
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" />
42+
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/generated/androidTest/debug" type="java-test-resource" />
43+
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
44+
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
45+
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
46+
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
47+
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
48+
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
49+
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
50+
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
51+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
52+
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
53+
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
54+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
55+
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
56+
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
57+
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
58+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
59+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
60+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
61+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
62+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
63+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
64+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
65+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
66+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
67+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
68+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/coverage-instrumented-classes" />
69+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
70+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
71+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
72+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
73+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
74+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
75+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/libs" />
76+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
77+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
78+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/ndk" />
79+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
80+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard" />
81+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
82+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
83+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
84+
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
85+
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
86+
</content>
87+
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
88+
<orderEntry type="sourceFolder" forTests="false" />
89+
<orderEntry type="library" exported="" name="support-v4-22.0.0" level="project" />
90+
<orderEntry type="library" exported="" name="kotlin-runtime-0.11.91.2" level="project" />
91+
<orderEntry type="library" exported="" name="support-annotations-22.0.0" level="project" />
92+
<orderEntry type="library" exported="" name="appcompat-v7-22.0.0" level="project" />
93+
<orderEntry type="library" exported="" name="kotlin-stdlib-0.11.91.2" level="project" />
94+
</component>
95+
</module>
96+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion 22
6+
buildToolsVersion "22.0.1"
7+
8+
defaultConfig {
9+
applicationId "com.irontec.kotlintest"
10+
minSdkVersion 16
11+
targetSdkVersion 22
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
sourceSets {
22+
main.java.srcDirs += 'src/main/kotlin'
23+
}
24+
}
25+
26+
dependencies {
27+
compile fileTree(dir: 'libs', include: ['*.jar'])
28+
compile 'com.android.support:appcompat-v7:22.0.0'
29+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
}
31+
buildscript {
32+
ext.kotlin_version = '0.11.91.2'
33+
repositories {
34+
mavenCentral()
35+
}
36+
dependencies {
37+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
38+
}
39+
}
40+
repositories {
41+
mavenCentral()
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/axier/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.irontec.kotlintest;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.irontec.kotlintest" >
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:theme="@style/AppTheme" >
12+
<activity
13+
android:name=".MainActivity"
14+
android:label="@string/app_name" >
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.irontec.kotlintest
2+
3+
import android.support.v7.app.ActionBarActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import android.view.Menu
7+
import android.view.MenuItem
8+
import android.webkit.WebView
9+
import android.widget.ListView
10+
import com.irontec.kotlintest.adapters.CustomListAdapter
11+
import com.irontec.kotlintest.models.CustomPojo
12+
13+
14+
public class MainActivity : ActionBarActivity() {
15+
16+
val mListView: ListView
17+
get() {
18+
return findViewById(R.id.list) as ListView
19+
}
20+
21+
22+
override fun onCreate(savedInstanceState: Bundle?) {
23+
super.onCreate(savedInstanceState)
24+
setContentView(R.layout.activity_main)
25+
26+
val listdata = Array<CustomPojo>(90, {i -> CustomPojo(name = i.toString())})
27+
28+
val adapter = CustomListAdapter(data = listdata, context = this)
29+
30+
mListView.setAdapter(adapter)
31+
}
32+
33+
34+
override fun onCreateOptionsMenu(menu: Menu): Boolean {
35+
getMenuInflater().inflate(R.menu.menu_main, menu)
36+
return true
37+
}
38+
39+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
40+
val id = item.getItemId()
41+
if (id == R.id.action_settings) {
42+
return true
43+
}
44+
return super.onOptionsItemSelected(item)
45+
}
46+
47+
fun getLastNumber(dataset: Array<Int>) : Int {
48+
return dataset.last();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.irontec.kotlintest.adapters
2+
3+
import android.content.Context
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.BaseAdapter
8+
import android.widget.TextView
9+
import com.irontec.kotlintest.R
10+
import com.irontec.kotlintest.models.CustomPojo
11+
12+
/**
13+
* Created by axier on 22/4/15.
14+
*/
15+
open class CustomListAdapter(data: Array<CustomPojo>, context: Context) : BaseAdapter() {
16+
17+
var localData = data;
18+
19+
override fun getCount(): Int {
20+
return localData.size()
21+
}
22+
23+
override fun getItem(position: Int): Any {
24+
return localData.get(position)
25+
}
26+
27+
override fun getItemId(position: Int): Long {
28+
return position.toLong()
29+
}
30+
31+
private val mInflater: LayoutInflater?
32+
{
33+
mInflater = LayoutInflater.from(context)
34+
}
35+
36+
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
37+
38+
val view: View?
39+
val vh: ListRowHolder
40+
if (convertView == null) {
41+
view = mInflater?.inflate(R.layout.row_item, parent, false)
42+
vh = ListRowHolder(view)
43+
view?.setTag(vh)
44+
} else {
45+
view = convertView
46+
vh = view.getTag() as ListRowHolder
47+
}
48+
vh.label.setText(localData.get(index = position).toString())
49+
return view;
50+
}
51+
52+
private class ListRowHolder(row: View?) {
53+
public val label: TextView
54+
{
55+
this.label = row?.findViewById(R.id.label) as TextView
56+
}
57+
}
58+
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.irontec.kotlintest.models
2+
3+
/**
4+
* Created by axier on 21/4/15.
5+
*/
6+
7+
open class CustomPojo (var name: String) {
8+
9+
init {
10+
println("Customer initialized with value ${name}")
11+
}
12+
13+
override fun toString() = "$name"
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3+
android:layout_height="match_parent" tools:context=".MainActivity">
4+
5+
<ListView
6+
android:id="@+id/list"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"/>
9+
10+
</RelativeLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<TextView
7+
android:id="@+id/label"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content" />
10+
11+
</LinearLayout>

0 commit comments

Comments
 (0)