-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_view.py
More file actions
76 lines (59 loc) · 2.39 KB
/
list_view.py
File metadata and controls
76 lines (59 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from abc import ABC, abstractmethod
from typing import Any
from .utils import IS_ANDROID
from .view import ViewBase
# ========================================
# Base class
# ========================================
class ListViewBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_data(self, data: list) -> "ListViewBase":
pass
@abstractmethod
def get_data(self) -> list:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/android/widget/ListView
# ========================================
from java import jclass
class ListView(ListViewBase, ViewBase):
def __init__(self, context: Any, data: list = []) -> None:
super().__init__()
self.context = context
self.native_class = jclass("android.widget.ListView")
self.native_instance = self.native_class(context)
self.set_data(data)
def set_data(self, data: list) -> "ListView":
adapter = jclass("android.widget.ArrayAdapter")(
self.context, jclass("android.R$layout").simple_list_item_1, data
)
self.native_instance.setAdapter(adapter)
return self
def get_data(self) -> list:
adapter = self.native_instance.getAdapter()
return [adapter.getItem(i) for i in range(adapter.getCount())]
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uitableview
# ========================================
from rubicon.objc import ObjCClass
class ListView(ListViewBase, ViewBase):
def __init__(self, data: list = []) -> None:
super().__init__()
self.native_class = ObjCClass("UITableView")
self.native_instance = self.native_class.alloc().init()
self.set_data(data)
def set_data(self, data: list) -> "ListView":
# Note: This is a simplified representation. Normally, you would need to create a UITableViewDataSource.
self.native_instance.reloadData()
return self
def get_data(self) -> list:
# Note: This is a simplified representation.
# Normally, you would need to get data from the UITableViewDataSource.
return []