Codename One:我想在连接请求中使用Hashtable向服务器发送JsonArray



我正在构建一个应用程序,该应用程序将向数据库提交兄弟姐妹的详细信息。我的想法是,由于我不知道你们孩子的数量,我只是有一个浮动按钮,用来调用一个类,该类添加了一个包含一些要填充的textFields的contaner。

所以我这里有一张表格。。。。

private Button btnSubmit;
private Container cnt_box;
public class ChildrenForm extends Form
{
private List<Child> listofchildren;

public ChildrenForm()
{
super("CHILDREN DETAILS",BoxLayout.y());
FloatingActionButton fab=FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
fab.bindFabToContainer(this);
fab.addActionListener((e) -> addNewChild());
getToolbar().addMaterialCommandToRightBar("", FontImage.MATERIAL_CLEAR_ALL, (e) -> 

clearAll());
btnSubmit=new Button("Submit");
cnt_box = new Container(new BoxLayout(BoxLayout.Y_AXIS));
cnt_box.add(btnSubmit);
add(cnt_box);
}
//....here i have some other methods...
}

我有一个方法可以在这里启用编辑。。。。

public void edit()
{
txtname.startEditingAsync();
txtname3.startEditingAsync();
txtbirth.startEditingAsync();
txtdbirth.startEditingAsync();
}

floatingAction按钮在此处调用此方法。。。。

public void addNewChild()
{
Childdetails td=new Childdetails("","","","",false);
add(td);
revalidate();
td.edit();
}

这个方法现在调用了这个类,我想获取显示这个容器的详细信息。。。。。

public class Childdetails extends Container
{
private TextField txtname;
private TextField txtname3;
private TextField txtbirth;
private TextField txtdbirth;
private CheckBox done=new CheckBox();
private Container cnt_child;
public Childdetails(String name,String name3,String birthcertno,String dateofbirth ,boolean checked)
{
super(new BorderLayout());
cnt_child=new Container();
cnt_child.addComponent(new Label("First Name"));
txtname = new TextField(name);
txtname.setHint("First Name");
cnt_child.addComponent(txtname);
cnt_child.addComponent(new Label("Surname"));
txtname3 = new TextField(name3);
txtname3.setHint("Surname");
cnt_child.addComponent(txtname3);
cnt_child.addComponent(new Label("Birth Certificate/Notification No"));
txtbirth = new TextField(birthcertno);
txtbirth.setHint("Birth Certificate No:");
cnt_child.addComponent(txtbirth);
cnt_child.addComponent(new Label("Date of Birth"));
txtdbirth = new TextField(dateofbirth);
txtdbirth.setHint("dd/MM/yyyy");
cnt_child.addComponent(txtdbirth);
add(CENTER,cnt_child);
add(LEFT,done);
done.setSelected(checked);
}
public void edit()
{
txtname.startEditingAsync();
txtname3.startEditingAsync();
txtbirth.startEditingAsync();
txtdbirth.startEditingAsync();
}
public boolean isChecked(){
return done.isSelected();
}
public String getText(){
return txtname.getText();
}
}

这是我用来延迟任何选定容器的方法。。。。但我理解这是因为那个保存方法。。。。。。

private void clearAll()
{
int cc=getContentPane().getComponentCount();
for(int i=cc-1; i>=0; i--)
{
Childdetails t=(Childdetails)getContentPane().getComponentAt(i);
if(t.isChecked())
{
t.remove();
}
}
save();
getContentPane().animateLayout(300);
}

保存方法。。。。在遵循了一些教程之后,我相信它可以保存所获取的数据。。。。此处

private void save()
{
listofchildren = new ArrayList<>();
Childdetails detail=new Childdetails("","","","",false);
Child child=new Child()
.name.set(detail.getText())
.name3.set(detail.getText())
.birthcertno.set(detail.getText())
.dateofbirth.set(detail.getText())
.checked.set(detail.isChecked());
listofchildren.add(child);

PropertyIndex.storeJSONList("child.json", listofchildren);
}

我还有一个我按照某个教程构建的类来保存数据。。。。。此处

public class Child implements PropertyBusinessObject
{
public final Property<String,Child> name=new Property<>("firstname","");
public final Property<String,Child> name3=new Property<>("Surname","");
public final Property<String,Child> birthcertno=new Property<>("BirthCertNo","");
public final Property<String,Child> dateofbirth=new Property<>("dateofbirth","");
public final BooleanProperty<Child> checked=new BooleanProperty<>("checked", false);
private final PropertyIndex idx=new PropertyIndex(this,"Todo", name, name3, birthcertno, dateofbirth, checked);
@Override
public PropertyIndex getPropertyIndex(){
return idx;
}

现在我的主要问题。。。我只想当按下提交按钮时,发送填写的详细信息。。。。。我试过这个,,

btnSubmit.addActionListener(new ActionListener() 
{
@Override
public void actionPerformed(ActionEvent evt) 
{
Log.p("Button pressed", 1);
save();
Log.p("data saved...", 1);
if(existsInStorage("child.json"))
{
Log.p("loading data ...", 1);

listofchildren=new Child().getPropertyIndex().loadJSONList("child.json");

String NationalID=Storage.getInstance().readObject("NationalID").toString();
String UserName=Storage.getInstance().readObject("UserName").toString();

Hashtable hash=new Hashtable();
hash.put("ChildDet", listofchildren);

hash.put("ReadIdCopy", NationalID);
hash.put("UserName",UserName);
final Result res=Result.fromContent(hash);
final String checkthis=res.toString();
//--------check url......
String myUrl="http://localhost:50111/AddChildren";
String Reply="";

requestclass c=new requestclass(); 
try {
Reply=c.checking(checkthis,myUrl);
} catch (IOException ex) {
//                            Logger.getLogger(AddChildren.class.getName()).log(Level.SEVERE, null, ex);
} catch (requestclass.JSONException ex) {
//                            Logger.getLogger(AddChildren.class.getName()).log(Level.SEVERE, null, ex);
}
if(Reply.equals("SuccesfullyRecieved"))
{
Dialog.show("SuccesfullyRecieved", "Details Succesfuly Recieved", "OK", null);
/*----redirect---*/
nextofkin nkin=new nextofkin();
nkin.nxtofkscreen();
}
else if(Reply.equals("sorry"))
{
Dialog.show("SORRY!!!", "Seems their is a problem updating Next of kin details... try again", "OK", null);
}
else
{
Dialog.show("Error", "Something went wrong, try checking your connection and try again later.", "OK", null);
} 
}
else
{
ToastBar.showErrorMessage("Sorry, no data to submit....");
}
}
});

我不知道怎么做,,,还有我的保存方法有一些错误。。。请帮我一下,提前谢谢

这是由以下行引起的:

Childdetails t=(Childdetails)getContentPane().getComponentAt(i);

您在这里所做的是在内容窗格中的所有组件上循环,并将它们向下转换为Childdetails

这太糟糕了。你没有检查instanceof,这会很有帮助。你可能还有其他问题,但这条线:

add(cnt_box);

具体地,将非Childdetails组件添加到内容窗格(在Form上不带上下文的情况下进行添加会隐式添加到内容面板)。

关于startEditingAsync。这是错误的。

这不是让它们可见的方法。

请注意,在显示表单并在这些实例上使用animateLayout之前,您的代码添加了许多组件。这可能就是为什么事情不可见的原因,因为你在还没有显示的CCD_;运行";没有任何效果。部件可能在错误的区域。

我建议删除startEditingAsync的整个块,并尝试:

if(getContentPane().isInitialized()) {
getContentPane().animateLayout(300);
}

最新更新