mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-13 12:59:19 +02:00
LCL-Android: Adds a new JNI routine which allows to show the native ListView in a dialog. It is very useful when we need the user to select an item from a list which has more info then the basic list for the combobox.
git-svn-id: trunk@37692 -
This commit is contained in:
parent
ece462c2a0
commit
fad008f0a0
@ -659,6 +659,111 @@ public class LCLActivity extends Activity implements SensorEventListener, Locati
|
||||
});
|
||||
}
|
||||
|
||||
// This method allows us to use the native Android ListView in a dialog
|
||||
// It is nice for choosing a line in a table with multiple columns of information
|
||||
// The extra columns appear as sub-info in the Android ListView
|
||||
//
|
||||
// output: Calls LCLOnMessageBoxFinished which will call LCLIntf.OnListViewDialogResult
|
||||
//
|
||||
public void LCLDoShowListViewDialog(String ATitle, String[] AItems, String[] ASubItems)
|
||||
{
|
||||
Dialog dialog = new Dialog(this);
|
||||
|
||||
ListView lListView = new ListView(this);
|
||||
List<LCL_ListViewItem> listItems = new ArrayList<LCL_ListViewItem>();
|
||||
for (int i = 0; i < AItems.length; i++)
|
||||
{
|
||||
listItems.add(new LCL_ListViewItem(AItems[i], ASubItems[i]));
|
||||
};
|
||||
LCL_ListViewAdapter listAdapter = new LCL_ListViewAdapter(
|
||||
this,
|
||||
listItems,
|
||||
android.R.layout.simple_list_item_2,
|
||||
new String[] { "title", "description" },
|
||||
new int[] { android.R.id.text1, android.R.id.text2 });
|
||||
lListView.setAdapter(listAdapter);
|
||||
lListView.setClickable(true);
|
||||
AdapterView.OnItemClickListener listviewClickListener = new AdapterView.OnItemClickListener()
|
||||
{
|
||||
@Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
|
||||
{
|
||||
//Log.i("lclapp", "LCLDoPrepareSelectItemDialog.onClick");
|
||||
LCLOnMessageBoxFinished(position, 2);
|
||||
dialog.dismiss();
|
||||
}
|
||||
};
|
||||
lListView.setOnItemClickListener(listviewClickListener);
|
||||
|
||||
DialogInterface.OnCancelListener dialogCancelListener = new DialogInterface.OnCancelListener()
|
||||
{
|
||||
@Override public void onCancel(DialogInterface dialog)
|
||||
{
|
||||
LCLOnMessageBoxFinished(-1, 2);
|
||||
}
|
||||
};
|
||||
dialog.setOnCancelListener(dialogCancelListener);
|
||||
dialog.setTitle(ATitle);
|
||||
dialog.setContentView(lListView);
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
//
|
||||
// Classes for the ListView
|
||||
//
|
||||
|
||||
//
|
||||
// ListView item
|
||||
//
|
||||
public class LCL_ListViewItem extends HashMap<String, String>
|
||||
{
|
||||
public String Title;
|
||||
public String Description;
|
||||
|
||||
public LCL_ListViewItem(String ATitle, String ADescription)
|
||||
{
|
||||
this.Title = ATitle;
|
||||
this.Description = ADescription;
|
||||
}
|
||||
|
||||
@Override public String get(Object k)
|
||||
{
|
||||
String key = (String) k;
|
||||
if (key.equals("title")) return Title;
|
||||
else if (key.equals("description")) return Description;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Adapter class for the ListView
|
||||
//
|
||||
public class LCL_ListViewAdapter extends SimpleAdapter
|
||||
{
|
||||
private List<LCL_ListViewItem> Items;
|
||||
// Colors to alternate
|
||||
private int[] colors = new int[] { 0xffffffff, 0xff808080 };
|
||||
|
||||
@SuppressWarnings("unchecked") public LCL_ListViewAdapter(
|
||||
Context context,
|
||||
List<? extends Map<String, String>> AItems,
|
||||
int resource,
|
||||
String[] from,
|
||||
int[] to)
|
||||
{
|
||||
super(context, AItems, resource, from, to);
|
||||
this.Items = (List<LCL_ListViewItem>) Items;
|
||||
}
|
||||
|
||||
@Override public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
View view = super.getView(position, convertView, parent);
|
||||
|
||||
//int colorPos = position % colors.length;
|
||||
//view.setBackgroundColor(colors[colorPos]);
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------
|
||||
// Fields exported to the Pascal side for easier data communication
|
||||
// -------------------------------------------
|
||||
|
@ -119,7 +119,7 @@
|
||||
<PackageName Value="LCL"/>
|
||||
</Item1>
|
||||
</RequiredPackages>
|
||||
<Units Count="4">
|
||||
<Units Count="5">
|
||||
<Unit0>
|
||||
<Filename Value="androidlcltest.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
@ -145,6 +145,10 @@
|
||||
<ResourceBaseClass Value="Form"/>
|
||||
<UnitName Value="secondform"/>
|
||||
</Unit3>
|
||||
<Unit4>
|
||||
<Filename Value="android/AndroidManifest.xml"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit4>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
@ -58,6 +58,8 @@ type
|
||||
procedure HandleMessageDialogFinished(Sender: TObject; AResult: Integer);
|
||||
procedure SocketProc;
|
||||
function LoadHTMLPageViaJNI(AURL: string): string;
|
||||
procedure ShowListViewDialog(ATitle: string; ATitles, ADescriptions: array of string);
|
||||
procedure MyOnListViewDialogResult(ASelectedItem: Integer);
|
||||
end;
|
||||
|
||||
{ TSubControl }
|
||||
@ -230,6 +232,8 @@ begin
|
||||
SubControl.Width := 50;
|
||||
SubControl.Height := 50;
|
||||
SubControl.Parent := Self;
|
||||
|
||||
OnListViewDialogResult := @MyOnListViewDialogResult;
|
||||
end;
|
||||
|
||||
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
|
||||
@ -463,5 +467,56 @@ begin
|
||||
{$endif}
|
||||
end;
|
||||
|
||||
procedure TForm1.ShowListViewDialog(ATitle: string; ATitles,
|
||||
ADescriptions: array of string);
|
||||
{$ifdef Android}
|
||||
var
|
||||
javaClass_LCLActivity, javaClass_String: jclass;
|
||||
javaMethod_LCLActivity_LCLDoShowListViewDialog: jmethodid;
|
||||
javaString: jstring;
|
||||
lParams: array[0..2] of JValue;
|
||||
lNativeString: PChar;
|
||||
i: Integer;
|
||||
{$endif}
|
||||
begin
|
||||
{$ifdef Android}
|
||||
// Here we call this routine:
|
||||
// public void LCLDoShowListViewDialog(String ATitle, String[] AItems, String[] ASubItems)
|
||||
DebugLn(':>ShowListViewDialog');
|
||||
// First call FindClass for all required classes
|
||||
javaClass_LCLActivity := javaEnvRef^^.FindClass(javaEnvRef, 'com/pascal/lcltest/LCLActivity');
|
||||
javaClass_String := javaEnvRef^^.FindClass(javaEnvRef, 'java/lang/String');
|
||||
|
||||
// Now all Method IDs
|
||||
DebugLn(':ShowListViewDialog 1');
|
||||
javaMethod_LCLActivity_LCLDoShowListViewDialog := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_LCLACtivity,
|
||||
'LCLDoShowListViewDialog',
|
||||
'(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V');
|
||||
|
||||
DebugLn(':ShowListViewDialog 2');
|
||||
// Create a new instance for the HTTP request object
|
||||
// HttpGet javaRequest = new HttpGet();
|
||||
lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, PChar(ATitle));
|
||||
lParams[1].l := javaEnvRef^^.NewObjectArray(javaEnvRef, Length(ATitles), javaClass_String, javaEnvRef^^.NewStringUTF(javaEnvRef, ''));
|
||||
for i := 0 to Length(ATitles)-1 do
|
||||
javaEnvRef^^.SetObjectArrayElement(javaEnvRef, lParams[1].l, i, javaEnvRef^^.NewStringUTF(javaEnvRef, PChar(ATitles[i])));
|
||||
lParams[2].l := javaEnvRef^^.NewObjectArray(javaEnvRef, Length(ADescriptions), javaClass_String, javaEnvRef^^.NewStringUTF(javaEnvRef, ''));
|
||||
for i := 0 to Length(ADescriptions)-1 do
|
||||
javaEnvRef^^.SetObjectArrayElement(javaEnvRef, lParams[2].l, i, javaEnvRef^^.NewStringUTF(javaEnvRef, PChar(ADescriptions[i])));
|
||||
//
|
||||
javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaActivityObject,
|
||||
javaMethod_LCLActivity_LCLDoShowListViewDialog, @lParams[0]);
|
||||
javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
|
||||
javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[1].l);
|
||||
javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[2].l);
|
||||
DebugLn(':ShowListViewDialog 3');// javaRequest='+IntToHex(PtrInt(javaRequest), 8));
|
||||
{$endif}
|
||||
end;
|
||||
|
||||
procedure TForm1.MyOnListViewDialogResult(ASelectedItem: Integer);
|
||||
begin
|
||||
DebugLn(Format('[MyOnListViewDialogResult] ASelectedItem=%d', [ASelectedItem]));
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
object Form2: TForm2
|
||||
Left = 138
|
||||
Height = 259
|
||||
Height = 266
|
||||
Top = 247
|
||||
Width = 320
|
||||
Caption = 'Form2'
|
||||
ClientHeight = 259
|
||||
ClientHeight = 266
|
||||
ClientWidth = 320
|
||||
LCLVersion = '1.1'
|
||||
object Button1: TButton
|
||||
@ -31,7 +31,7 @@ object Form2: TForm2
|
||||
object Image1: TImage
|
||||
Left = 102
|
||||
Height = 138
|
||||
Top = 64
|
||||
Top = 96
|
||||
Width = 210
|
||||
Picture.Data = {
|
||||
07545069786D617044DA00002F2A2058504D202A2F0A73746174696320636861
|
||||
@ -1786,7 +1786,7 @@ object Form2: TForm2
|
||||
object btnStartAccel: TButton
|
||||
Left = 8
|
||||
Height = 25
|
||||
Top = 216
|
||||
Top = 232
|
||||
Width = 112
|
||||
Caption = 'Read Accel.'
|
||||
OnClick = btnStartAccelClick
|
||||
@ -1803,7 +1803,7 @@ object Form2: TForm2
|
||||
object btnStopAccel: TButton
|
||||
Left = 128
|
||||
Height = 25
|
||||
Top = 216
|
||||
Top = 232
|
||||
Width = 91
|
||||
Caption = 'Stop Accel.'
|
||||
OnClick = btnStopAccelClick
|
||||
@ -1812,7 +1812,7 @@ object Form2: TForm2
|
||||
object btnGetPos: TButton
|
||||
Left = 224
|
||||
Height = 25
|
||||
Top = 216
|
||||
Top = 232
|
||||
Width = 88
|
||||
Caption = 'Get Position'
|
||||
OnClick = btnGetPosClick
|
||||
@ -1860,4 +1860,13 @@ object Form2: TForm2
|
||||
OnClick = Button2Click
|
||||
TabOrder = 7
|
||||
end
|
||||
object Button3: TButton
|
||||
Left = 102
|
||||
Height = 25
|
||||
Top = 64
|
||||
Width = 203
|
||||
Caption = 'Show native ListView'
|
||||
OnClick = Button3Click
|
||||
TabOrder = 8
|
||||
end
|
||||
end
|
||||
|
@ -19,6 +19,7 @@ type
|
||||
btnGetPos: TButton;
|
||||
btnSendSMS: TButton;
|
||||
Button2: TButton;
|
||||
Button3: TButton;
|
||||
textDest: TEdit;
|
||||
textBody: TEdit;
|
||||
Image1: TImage;
|
||||
@ -31,6 +32,7 @@ type
|
||||
procedure btnStopAccelClick(Sender: TObject);
|
||||
procedure btnGetPosClick(Sender: TObject);
|
||||
procedure Button2Click(Sender: TObject);
|
||||
procedure Button3Click(Sender: TObject);
|
||||
procedure textDestExit(Sender: TObject);
|
||||
procedure textDestKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
|
||||
procedure textDestKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
|
||||
@ -97,6 +99,13 @@ begin
|
||||
lStr := Form1.LoadHTMLPageViaJNI('http://magnifier.sourceforge.net/');
|
||||
end;
|
||||
|
||||
procedure TForm2.Button3Click(Sender: TObject);
|
||||
begin
|
||||
Form1.ShowListViewDialog('Dialog Title',
|
||||
['Title1', 'Title2', 'Title3', 'Title4', 'Title5', 'Title6'],
|
||||
['Descr1', 'Descr2', 'Descr3', 'Descr4', 'Descr5', 'Descr6']);
|
||||
end;
|
||||
|
||||
procedure TForm2.textDestExit(Sender: TObject);
|
||||
begin
|
||||
DebugLn('[Edit1Exit]');
|
||||
|
@ -126,6 +126,7 @@ begin
|
||||
0: if Assigned(Application.OnMessageDialogFinished) then
|
||||
Application.OnMessageDialogFinished(Application, AResult);
|
||||
1: if Assigned(OnShowSelectItemDialogResult) then OnShowSelectItemDialogResult(AResult);
|
||||
2: if Assigned(OnListViewDialogResult) then OnListViewDialogResult(AResult);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
@ -88,6 +88,7 @@ type
|
||||
|
||||
var
|
||||
OnShowSelectItemDialogResult: TOnShowSelectItemDialogResult = nil;
|
||||
OnListViewDialogResult: TOnShowSelectItemDialogResult = nil; // -1 in the position indicates the dialog was cancelled
|
||||
|
||||
OpenURLWidgetsetImplementation: TOpenParamStringProc = nil;
|
||||
OpenDocumentWidgetsetImplementation: TOpenParamStringProc = nil;
|
||||
|
Loading…
Reference in New Issue
Block a user