我的数据类型:
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences prefs;
prefs = getSharedPreferences("pref", Context.MODE_PRIVATE);
int count = prefs.getInt("count", 0);
//Check your count here...
if(your condition)
{
//Code for showing `AlertDialog`
}
else
//Do nothing
我的填充列表框程序:
type
TObjectData = class
Id: Integer;
DataType: String;
end;
TProjektInfo = record
Id: Integer;
Nazev: String;
end;
TReportSelect = record
Count: Integer;
Zakazka_Id: Integer;
Singles: Array of TProjektInfo;
Multies: Array of TProjektInfo;
end;
点击我的按钮事件获取我的数据类型对象(这是错误的评论):
procedure TReportMain.VykresyFillProjectsList();
var
I,Id: Integer;
Nazev: String;
ItemData: TObjectData;
begin
VykresyProjectsListSections.Items.BeginUpdate;
VykresyProjectsListSections.Items.Clear;
for I := Low(ReportSelect.Singles) to High(ReportSelect.Singles) do
begin
Id := ReportSelect.Singles[I].Id;
Nazev := ReportSelect.Singles[I].Nazev;
ItemData := TObjectData.Create;
ItemData.Id := Id;
ItemData.DataType := 'single';
VykresyProjectsListSections.Items.AddObject(Nazev, TObject(ItemData));
ItemData.Free;
end;
for I := Low(ReportSelect.Multies) to High(ReportSelect.Multies) do
begin
Id := ReportSelect.Multies[I].Id;
Nazev := ReportSelect.Multies[I].Nazev;
ItemData := TObjectData.Create;
ItemData.Id := Id;
ItemData.DataType := 'multi';
VykresyProjectsListSections.Items.AddObject(Nazev, TObject(ItemData));
ItemData.Free;
end;
VykresyProjectsListSections.Items.EndUpdate;
end;
答案 0 :(得分:2)
您遇到的可能是访问冲突。
您尝试访问的变量未定义,因为您已经释放了变量所指向的对象。
在上面的代码中,ItemData
对象在添加到列表后总是被释放。
清除或释放列表时,必须编写一些代码来释放对象。
这可以在表单的OnDestroy
事件中完成:
procedure TReportMain.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := VykresyProjectsListSections.Items.Count-1 downto 0 do begin
VykresyProjectsListSections.Items.Objects[i].Free;
VykresyProjectsListSections.Delete(i);
end;
VykresyProjectsListSections.Free;//free the list if not owned by the application
end;
作为旁注,您可以测试是否检查TCheckListBox
元素:
var
i: Integer;
begin
for i := 0 to VykresyProjectsListSections.Items.Count-1 do begin
if VykresyProjectsListSections.State[i] = cbChecked then
//do your stuff
end;
end;
答案 1 :(得分:0)
您在ItemData.Free
之后致电AddObject()
。这是错误的,因为该对象将不再有效。
这将在以后访问CheckListBox
中的对象时导致错误。