Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Revision Project 1
- // https://imgur.com/a/ESIghSF
- //MyApplication.java
- package com.mycompany.myapp;
- import com.codename1.components.ImageViewer;
- import com.codename1.db.Cursor;
- import com.codename1.db.Database;
- import com.codename1.db.Row;
- import com.codename1.io.ConnectionRequest;
- import com.codename1.ui.Display;
- import com.codename1.ui.Form;
- import com.codename1.ui.Dialog;
- import com.codename1.ui.Label;
- import com.codename1.ui.plaf.UIManager;
- import com.codename1.ui.util.Resources;
- import com.codename1.io.Log;
- import com.codename1.io.NetworkEvent;
- import com.codename1.io.NetworkManager;
- import com.codename1.ui.Button;
- import com.codename1.ui.Component;
- import com.codename1.ui.Container;
- import com.codename1.ui.EncodedImage;
- import com.codename1.ui.TextField;
- import com.codename1.ui.Toolbar;
- import com.codename1.ui.URLImage;
- import com.codename1.ui.events.ActionEvent;
- import com.codename1.ui.events.ActionListener;
- import com.codename1.ui.layouts.BoxLayout;
- import com.codename1.ui.layouts.FlowLayout;
- import com.codename1.ui.util.UIBuilder;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- /**
- * This file was generated by <a href="https://www.codenameone.com/">Codename
- * One</a> for the purpose of building native mobile applications using Java.
- */
- public class MyApplication {
- private Form current;
- private Resources theme;
- ArrayList<Contact> contacts;
- String ch;
- Database db;
- boolean created = false;
- public void init(Object context) {
- theme = UIManager.initFirstTheme("/theme");
- // Enable Toolbar on all Forms by default
- Toolbar.setGlobalToolbar(true);
- contacts = new ArrayList<>();
- contacts.add(new Contact("1", "description nader", "contact_icon.png"));
- contacts.add(new Contact("2", "description hamza", "contact_icon.png"));
- contacts.add(new Contact("3", "description stoura", "contact_icon.png"));
- contacts.add(new Contact("4", "description ahmed", "contact_icon.png"));
- created = Database.exists("revisionCN1");
- try {
- db = Database.openOrCreate("revisionCN1");
- if (created == false) {
- System.out.println("CREATE");
- db.execute("CREATE TABLE user(id INTEGER, nom TEXT, decription TEXT, CONSTRAINT name_unique UNIQUE (nom));");
- }
- } catch (IOException ex) {
- System.err.println(ex.getMessage());
- }
- // Pro only feature, uncomment if you have a pro subscription
- // Log.bindCrashProtection(true);
- }
- Form f2;
- public void start() {
- UIBuilder ui = new UIBuilder();
- FlowLayout layout = new FlowLayout(Component.CENTER);
- Form f = new Form(layout);
- TextField tfNom = new TextField("", "Nom");
- TextField tfPrenom = new TextField("", "Prénom");
- f.add(tfNom);
- f.add(tfPrenom);
- Button btn = new Button("Valider");
- f.add(btn);
- f2 = new Form(BoxLayout.y());
- btn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- f2.setTitle(tfNom.getText() + " " + tfPrenom.getText());
- for (Contact c : contacts) {
- addItem(c);
- f2.refreshTheme();
- }
- f2.show();
- }
- });
- f.show();
- }
- public void addItem(Contact contact) {
- Container C1 = new Container(new BoxLayout(BoxLayout.X_AXIS));
- ImageViewer img = new ImageViewer(theme.getImage("round.png"));
- Container C2 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
- Label l = new Label(contact.getNom());
- Label tel = new Label(contact.getDescription());
- l.addPointerPressedListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- Form f3 = new Form(BoxLayout.y());
- EncodedImage encImg
- = EncodedImage.createFromImage(theme.getImage("round.png"), false);
- URLImage imgUrl
- = URLImage.createToStorage(encImg, "cache", "file:///C:/wamp64/www/serveur/cn1revision/" + contact.getImg());
- imgUrl.fetch();
- ImageViewer img = new ImageViewer(imgUrl);
- f3.add(img);
- Label lNom = new Label();
- Label lDesc = new Label();
- ConnectionRequest req = new ConnectionRequest();
- //C:\wamp64\www\serveur\cn1revision\getDetails.php
- req.setUrl("http://localhost/serveur/cn1revision/getDetails.php?id=" + contact.getNom());
- req.addResponseListener(new ActionListener<NetworkEvent>() {
- @Override
- public void actionPerformed(NetworkEvent evt) {
- byte data[] = (byte[]) evt.getMetaData();
- try {
- ch = new String(data, "utf-8");
- } catch (UnsupportedEncodingException ex) {
- }
- System.out.println(ch);
- lDesc.setText(ch);
- f3.add(lDesc);
- f3.refreshTheme();
- }
- });
- NetworkManager.getInstance().addToQueue(req);
- f3.getToolbar().addCommandToOverflowMenu("Add", null, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- try {
- db.execute("insert into user (nom, decription) values ('" + contact.getNom() + "', '" + lDesc.getText() + "' );");
- } catch (IOException ex) {
- System.err.println(ex.getMessage());
- }
- }
- });
- f3.getToolbar().addCommandToOverflowMenu("Select", null, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- Form f4 = new Form(BoxLayout.y());
- try {
- Cursor c = db.executeQuery("select * from user");
- while (c.next()) {
- Row r = c.getRow();
- String nom = r.getString(1);
- String prenom = r.getString(2);
- Label l = new Label(nom + " " + prenom);
- f4.add(l);
- }
- } catch (IOException ex) {
- System.err.println(ex.getMessage());
- }
- f4.getToolbar().addCommandToRightBar("Back to home", theme.getImage("back-command.png"),
- e -> f2.showBack());
- f4.show();
- }
- });
- f3.show();
- }
- });
- C2.add(l);
- C2.add(tel);
- C1.add(img);
- C1.add(C2);
- C1.setLeadComponent(l);
- f2.add(C1);
- f2.refreshTheme();
- }
- public void stop() {
- current = Display.getInstance().getCurrent();
- if (current instanceof Dialog) {
- ((Dialog) current).dispose();
- current = Display.getInstance().getCurrent();
- }
- }
- public void destroy() { }
- }
Advertisement
Add Comment
Please, Sign In to add comment