Introduction
The examples in this article will create a listview and a custom view. Android listview often uses adapter such as "ArrayAdapter<String>(…)
". The syntax is Java generic, which may have no corresponding types in dynamic script languages, such as Python, Lua, etc. Therefore, we have to give specific class, such as StringArrayAdaper
, for it can be used in these dynamic languages. For StringArrayAdapter
, we can override function getView
to return little more complex view for listview
. For custom view, we can create instance of View, and override its function onDraw
. In function onDraw
, we may draw text or bitmaps using function of canvas.
List View
We first create an instance of StringArrayAdapterClass
, which is defined wrapandroid.jar package. And then create a list view.
Create StringArrayAdapter and Override Its Function getView
MyStringArrayAdapter = Service.StringArrayAdapterClass._New();
def MyStringArrayAdapter_getView(self,position, convertView, parent) :
global Service;
i = Service.LinearLayoutClass._New();
px = i.dp2px(5);
i.setPadding(px,px,px,px);
i.setAbsListViewLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);
imageView = Service.ImageViewClass._New(i);
imageView.setPadding(5,5,5,5);
imageView.setLinearLayoutParams(i.dp2px(24),i.dp2px(24));
itextview = Service.TextViewClass._New(i);
itextview.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);
itextview.setTextSize(i.sp2px(18))
if( position == 0 ) :
itextview.setText("Android");
imageView.setImageResource(StarActivity.getResource("drawable/android_logo"));
if( position == 1 ) :
itextview.setText("WindowsMobile");
imageView.setImageResource(StarActivity.getResource("drawable/windowsmobile_logo"));
if( position == 2 ) :
itextview.setText("iOS");
imageView.setImageResource(StarActivity.getResource("drawable/ios_logo"));
if( position == 3 ) :
itextview.setText("Blackberry");
imageView.setImageResource(StarActivity.getResource("drawable/blackberry_logo"));
i._LockGC();
return i;
MyStringArrayAdapter.getView = MyStringArrayAdapter_getView;
#add string values to StringArrayAdapter.
MyStringArrayAdapter.add("Android");
MyStringArrayAdapter.add("WindowsMobile");
MyStringArrayAdapter.add("iOS");
MyStringArrayAdapter.add("Blackberry");
Create ListView and Set Its Adapter
MyListView = Service.ListViewClass._New(MyLayout);
def MyListView_onItemClick(self, Ev,objid,position,id) :
Service.ToastClass._New().makeText("[MyListView] event on click is onItemClick "+objid,0).show();
MyListView.onItemClick = MyListView_onItemClick;
MyListView.setLinearLayoutParams(Service.FILL_PARENT,150);
MyListView.setAdapter(MyStringArrayAdapter);
Custom View
For custom view, we can create instance of View, and override its function onDraw
. In function onDraw
, we may draw text or bitmaps using function of canvas.
Create a Paint and BitmapFactory for Using in Next Step
MyPaint = Service.PaintClass._New();
MyBitmapFactory = Service.BitmapFactoryClass._New();
Create View and Override Its Function onDraw
def myView_onDraw(self,canvas) :
global MyBitmapFactory,MyPaint
self.onSuperDraw(canvas);
MyPaint.setColor(0xFFFF0000);
canvas.drawRect(10, 20, 100, 100, MyPaint);
MyBitmap = MyBitmapFactory.decodeResource
(StarActivity.getResource("drawable/aqua02"));
canvas.drawBitmap(MyBitmap, 100, 100, None);
matrix=Service.MatrixClass._New();
matrix.postScale(0.8, 0.8);
matrix.postRotate(45);
dstbmp=Service.BitmapClass._New();
dstbmp.createBitmap0
(MyBitmap,0,0,MyBitmap.getWidth(),MyBitmap.getHeight(),matrix,True);
canvas.drawBitmap(dstbmp, 300, 100, None);
matrix._Free();
dstbmp._Free();
MyBitmap._Free();
myView.onDraw = myView_onDraw;
myView.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);
Screenshot