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
|
package dz.formation.sql;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class Main extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
DBAdaptater db;
private String titre;
private String produit;
private String numProduit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (this.getIntent().getExtras() != null) {
ArrayList<?> s1 = this.getIntent().getExtras().getCharSequenceArrayList("data1");
Log.i("[info1 Val A1:]", s1.get(0).toString()); this.titre =s1.get(0).toString();
Log.i("[info2 Val A2:]", s1.get(1).toString()); this.produit =s1.get(1).toString();
Log.i("[info3 Val A3:]", s1.get(2).toString()); this.numProduit =s1.get(2).toString();
//edit2.setText(s1);
}
getListView().setOnCreateContextMenuListener(this); // pour le click sur
// "menu"
((Button) findViewById(android.R.id.button1)).setOnClickListener(this);
db = new DBAdaptater(this);
db.open();
dataBind();
}
private void dataBind() {
Cursor c = db.recupererLaListeDesProduits();
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
new String[] { "titre", "description", "codebarre" },
new int[] { R.id.textTitre, R.id.TextDescription, R.id.TextCodeBarre });
setListAdapter(adapter);
}
@Override
protected void onDestroy() {
db.clos();
super.onDestroy();
}
@Override
public void onClick(View arg0) {
//long num = SystemClock.currentThreadTimeMillis();
db.insererUnProduit("titre: " + this.titre, "Nouvau produit: " + this.produit, "produit n°:" + this.numProduit);
dataBind(); // pour rafrechire la LiseViw
}
/**
* ajout de quelque fonctionnaliter + les gestion des evenement sur le menu
*/
@Override
// Création du menu principal
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 100, 0, "Tout effacer");
return true;
}
@Override
// Selection d'un item du menu
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 100:
db.truncate();
dataBind();
break;
}
return true;
}
@Override
// Selection d'un item de la liste
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = (Cursor) l.getAdapter().getItem(position);
String titre = cursor.getString(cursor.getColumnIndex("titre"));
Toast.makeText(this, "Item id " + id + " : " + titre,
Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
}
@Override
// Creation du menu contextuel
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0, 100, 0, "Supprimer");
menu.add(0, 200, 1, "Editer");
}
@Override
// Selection d'un item du menu contextuel
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case 100:
db.supprimerProduit(info.id);
dataBind();
break;
case 200:
Toast.makeText(this, "TODO", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
} |