Skip to content

Commit 5cf689e

Browse files
committed
Updated ListViewAdapter
1 parent 733de44 commit 5cf689e

File tree

2 files changed

+32
-111
lines changed

2 files changed

+32
-111
lines changed
Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
11
package com.pureswift.swiftandroid
22

3-
import android.database.DataSetObserver
3+
import android.R
4+
import android.content.Context
45
import android.view.View
56
import android.view.ViewGroup
6-
import android.widget.ListAdapter
7+
import android.widget.ArrayAdapter
78

8-
class ListViewAdapter(val context: SwiftObject): ListAdapter {
9+
class ListViewAdapter<T: Any>(context: Context, val swiftObject: SwiftObject, val objects: ArrayList<T>) :
10+
ArrayAdapter<T>(context, 0, objects) {
911

10-
external override fun getCount(): Int
12+
external override fun getView(position: Int, convertView: View?, parent: ViewGroup): View
1113

12-
external override fun isEmpty(): Boolean
13-
14-
external override fun getItem(position: Int): Any?
15-
16-
external override fun getItemId(position: Int): Long
17-
18-
external override fun getItemViewType(position: Int): Int
19-
20-
external override fun getViewTypeCount(): Int
21-
22-
external override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View?
23-
24-
external override fun areAllItemsEnabled(): Boolean
25-
26-
external override fun isEnabled(position: Int): Boolean
27-
28-
external override fun registerDataSetObserver(observer: DataSetObserver?)
29-
30-
external override fun unregisterDataSetObserver(observer: DataSetObserver?)
31-
32-
external override fun hasStableIds(): Boolean
33-
}
14+
fun getResourceId(): Int {
15+
return R.layout.simple_list_item_1
16+
}
17+
}

Demo/app/src/main/swift/ListViewAdapter.swift

Lines changed: 22 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,113 +12,50 @@ import AndroidKit
1212
open class ListViewAdapter: JavaObject {
1313

1414
@JavaMethod
15-
@_nonoverride public convenience init(swiftObject: SwiftObject!, environment: JNIEnvironment? = nil)
15+
@_nonoverride public convenience init(
16+
context: AndroidContent.Context?,
17+
swiftObject: SwiftObject?,
18+
objects: ArrayList<JavaObject>?,
19+
environment: JNIEnvironment? = nil
20+
)
1621

1722
@JavaMethod
18-
func getContext() -> SwiftObject!
23+
func getSwiftObject() -> SwiftObject!
24+
25+
@JavaMethod
26+
func getResourceId() -> Int32
1927
}
2028

2129
@JavaImplementation("com.pureswift.swiftandroid.ListViewAdapter")
2230
extension ListViewAdapter {
2331

24-
@JavaMethod
25-
func hasStableIds() -> Bool {
26-
log("\(self).\(#function)")
27-
return true
28-
}
29-
30-
@JavaMethod
31-
func isEmpty() -> Bool {
32-
log("\(self).\(#function)")
33-
return context.items.isEmpty
34-
}
35-
36-
@JavaMethod
37-
func getCount() -> Int32 {
38-
log("\(self).\(#function)")
39-
return Int32(context.items.count)
40-
}
41-
42-
@JavaMethod
43-
func getItem(position: Int32) -> JavaObject? {
44-
log("\(self).\(#function) \(position)")
45-
return JavaString(context.items[Int(position)])
46-
}
47-
48-
@JavaMethod
49-
func getItemId(position: Int32) -> Int64 {
50-
log("\(self).\(#function)")
51-
return Int64(position)
52-
}
53-
54-
@JavaMethod
55-
func getItemViewType(position: Int32) -> Int32 {
56-
log("\(self).\(#function)")
57-
return 0
58-
}
59-
60-
@JavaMethod
61-
func getViewTypeCount() -> Int32 {
62-
log("\(self).\(#function)")
63-
return 1
64-
}
65-
6632
@JavaMethod
6733
func getView(position: Int32, convertView: AndroidView.View?, parent: ViewGroup?) -> AndroidView.View? {
6834
log("\(self).\(#function) \(position)")
69-
guard let parent else {
70-
assertionFailure()
71-
return nil
72-
}
73-
let view = TextView(parent.getContext())
74-
let item = context.items[Int(position)]
75-
view.text = item
76-
return view
77-
}
78-
79-
@JavaMethod
80-
func areAllItemsEnabled() -> Bool {
81-
log("\(self).\(#function)")
82-
return true
83-
}
84-
85-
@JavaMethod
86-
func isEnabled(position: Int32) -> Bool {
87-
log("\(self).\(#function) \(position)")
88-
return true
89-
}
90-
91-
@JavaMethod
92-
func registerDataSetObserver(observer: JavaObject?) {
93-
log("\(self).\(#function)")
94-
95-
}
96-
97-
@JavaMethod
98-
func unregisterDataSetObserver(observer: JavaObject?) {
99-
log("\(self).\(#function)")
100-
35+
return getView(position, convertView, parent)
10136
}
10237
}
10338

10439
public extension ListViewAdapter {
10540

106-
struct Context {
107-
108-
let items: [String]
109-
}
41+
typealias GetView = (Int32, AndroidView.View?, ViewGroup?) -> AndroidView.View?
11042

111-
var context: Context {
43+
var getView: GetView {
11244
get {
113-
getContext().valueObject().value as! Context
45+
getSwiftObject().valueObject().value as! GetView
11446
}
11547
set {
116-
getContext().valueObject().value = newValue
48+
getSwiftObject().valueObject().value = newValue
11749
}
11850
}
11951

120-
convenience init(_ context: Context, environment: JNIEnvironment? = nil) {
121-
self.init(swiftObject: SwiftObject(context), environment: environment)
52+
convenience init(
53+
context: AndroidContent.Context,
54+
getView: @escaping (Int32, AndroidView.View?, ViewGroup?) -> AndroidView.View?,
55+
objects: ArrayList<JavaObject>,
56+
environment: JNIEnvironment? = nil
57+
) {
58+
self.init(context: context, swiftObject: SwiftObject(getView), objects: objects, environment: environment)
12259
}
12360
}
12461

0 commit comments

Comments
 (0)