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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class SaisieNotes3 extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
//ajout pour modification par EditText
private EditText EditTextModificationNote;
private Long mRowId;
//fin
private ElevesDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_avec_edittext);
setTitle(R.string.titre_saisie_notes);
//ajout pour modification par EditText
EditTextModificationNote = (EditText) findViewById(R.id.EditTextModificationNote);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(ElevesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(ElevesDbAdapter.KEY_ROWID)
: null;
}
//fin
mDbHelper = new ElevesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor notesCursor = mDbHelper.RemplissageListView();
startManagingCursor(notesCursor);
SimpleCursorAdapter classe = new SimpleCursorAdapter (this,
R.layout.cursor_saisies_notes,notesCursor,new String[]{"note","nom"},
new int[]{R.id.note,R.id.nom}
);
setListAdapter(classe);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createEleves();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteEleves(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createEleves() {
Intent i = new Intent(this, CreationFichesEleves.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
//ajout pour modification par EditText
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (mRowId != null) {
Cursor classe8 = mDbHelper.ModifNotesEleves(mRowId);
startManagingCursor(classe8);
EditTextModificationNote.setText(classe8.getString(
classe8.getColumnIndexOrThrow(ElevesDbAdapter.KEY_NOTE)));
}
}
private void populateFields3() {
if (mRowId != null) {
Cursor classe8 = mDbHelper.ModifNotesEleves(mRowId);
startManagingCursor(classe8);
EditTextModificationNote.setText(classe8.getString(
classe8.getColumnIndexOrThrow(ElevesDbAdapter.KEY_NOTE)));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(ElevesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields3();
}
private void saveState() {
String note = EditTextModificationNote.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createEleves2(note);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateEleves2(mRowId, note);
}
}
//FIN
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
} |
Partager