1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
package com.trebbe.scanner;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.trebbe.scanner.provider.SqliteProvider;
import com.trebbe.scanner.renderer.ProduitsListAdapter;
public class ActivityAjoutProduits extends Activity {
private static final int CODE_SCANNER = 10;
private static final int CODE_DESCRIPTION_PRODUIT = 11;
private TextView tv_client;
private ListView lv_produits;
private ProduitsListAdapter pla;
private Devis devis;
private ArrayList<ProduitDevis> produits;
private SqliteProvider sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajout_produit);
sp = new SqliteProvider(getApplicationContext());
//On récupère l'objet Bundle envoyé par l'autre Activity
Bundle objetbunble = this.getIntent().getExtras();
//On récupère les données du Bundle
long idDevis = 0;
if (objetbunble != null && objetbunble.containsKey("idDevis")) {
idDevis = objetbunble.getLong("idDevis");
}
else
finish();
Devis d = new Devis();
try {
devis = sp.getDevis(idDevis);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
tv_client = (TextView)findViewById(R.id.tv_client);
tv_client.setText(devis.getClient().getNom()+" "+devis.getClient().getPrenom());
lv_produits = (ListView)findViewById(R.id.lv_produits);
findViewById(R.id.bt_scan).setOnClickListener(bt_scanClick);
if(lv_produits.getAdapter()!=null && lv_produits.getAdapter().getCount()==0)
return;
produits = sp.getListeProduits(devis.getId());
try {
pla = new ProduitsListAdapter(getApplicationContext(), produits);
lv_produits.setAdapter(pla);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Button.OnClickListener bt_scanClick = new Button.OnClickListener(){
@Override
public void onClick(View v) {
lancerScan();
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == CODE_SCANNER) {
if (resultCode == RESULT_OK) {
String idProduit = intent.getStringExtra("SCAN_RESULT");
if(idProduit!="")
lancerDescriptionProduit(idProduit);
}
else
if (resultCode == RESULT_CANCELED) {
}
}
if (requestCode == CODE_DESCRIPTION_PRODUIT) {
if(resultCode == RESULT_OK){
// Si le produit correspond, on l'ajoute a la base
produits.add(new ProduitDevis(sp.addProduct(devis.getId(), intent.getLongExtra("idProduit", 0), intent.getIntExtra("qte_produit", 0))));
// Ne fonctionne pas pour l'instant
pla.notifyDataSetChanged();
}
}
}
private void updateDataProduits() throws JSONException, IOException{
/*
// Fonctionne avec ce codes
produits = sp.getListeProduits(devis.getId());
try {
pla = new ProduitsListAdapter(getApplicationContext(), produits);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lv_produits.setAdapter(pla);
*/
}
private void lancerScan(){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, CODE_SCANNER);
}
private void lancerDescriptionProduit(String idProduit){
Bundle objetbunble = new Bundle();
objetbunble.putString("idProd", idProduit);
Intent intentDesc = new Intent(getApplicationContext(), DescriptionProduit.class);
//On affecte à l'Intent le Bundle que l'on a créé
intentDesc.putExtras(objetbunble);
//On démarre l'autre Activity
startActivityForResult(intentDesc, CODE_DESCRIPTION_PRODUIT);
}
} |
Partager