AjoutMedocActivity.java 2.95 KB
package com.example.martin.projetv5;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by martin on 28/02/2017.
 */

public class AjoutMedocActivity extends AppCompatActivity {

    StringBDD eventBDD = null;
    ArrayList<String> arrayList = new ArrayList<>();
    AutoCompleteTextView editNom = null;


    public void onClickAjout(View view){
        editNom = (AutoCompleteTextView) findViewById(R.id.textNom);
        String s1 = editNom.getText().toString();

        // on regarde si le nom rentré existe
        boolean existe = false;
        int w=1;
        while(w <= eventBDD.getIndiceMaxMedoc() && !existe) {
            Medicament m = eventBDD.getMedocWithId(w);
            if (m.getNom1().equals(s1)) existe = true;
            w++;
        }

        if(existe) {
            Medicament m = eventBDD.getMedocWithNom1(s1);

            // s'il existe on regarde ensuite si ce medicament n'est pas déjà en train d'être pris
            boolean ok = true;
            int x=1;
            while(x <= eventBDD.getIndiceMaxPrise() && ok){
                Prise2 p = eventBDD.getPriseWithId(x);
                if(p.getIdMedoc().equals(m.getIdM()))ok = false;
                x++;
            }

            if(ok) {
                // si tout est bon, on poursuit
                Intent i = new Intent(this, ModifMedocActivity.class);
                boolean nouveau = true;
                i.putExtra("id", m.getIdM());
                i.putExtra("new", nouveau);
                startActivity(i);
            }
            else Toast.makeText(this,R.string.ajout3,Toast.LENGTH_LONG).show();
        }
        else Toast.makeText(this,R.string.ajout2,Toast.LENGTH_LONG).show();
    }

    void recupNom1Medoc(ArrayList<String> aList){
        for(int w=0; w<eventBDD.getIndiceMaxMedoc(); w++){
            String s = eventBDD.getMedocWithId(w+1).getNom1();
            aList.add(w,s);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajout_layout);

        eventBDD = new StringBDD(this);
        eventBDD.open();
    }

    @Override
    protected void onResume() {
        eventBDD.open();
        super.onResume();

        editNom = (AutoCompleteTextView) findViewById(R.id.textNom);
        recupNom1Medoc(arrayList);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, arrayList);
        editNom.setAdapter(adapter);
        editNom.setThreshold(0);
    }

    @Override
    protected void onPause() {
        eventBDD.close();
        super.onPause();
        arrayList.clear();
    }
}