Hallo,
ich hab eine ListActivity gebastelt welche Einstellungen also name,value aus einer sqlite DB anzeigt.
diese möchte ich gerne mit einem Dialog bearbeiten können.
bei meinem ansatz ist es leider so dass etwas schief läuft nach dem 2. mal einen gleichen dialog zu öffnen .. könnte mir vorstellen dass meine idee das ganze umzusetzen falsch ist...
hier mein Ansatz:
Java
public class hvwsettings extends ListActivity {
private DbAdapter DBHelper;
private Dialog dialog;
private String setting_name;
private Cursor settings_cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
DBHelper = new DbAdapter(this);
DBHelper.open();
setContentView(R.layout.settings_list);
displayData();
registerForContextMenu(getListView());
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
TextView tv;
LinearLayout itemlayout;
itemlayout = (LinearLayout) lv.getChildAt(position);
tv = (TextView) itemlayout
.findViewById(R.id.setting_displayname);
setting_name = (String) tv.getText();
tv = (TextView) itemlayout.findViewById(R.id.setting_name);
switch (Integer.parseInt((String) tv.getText())) {
case Constants.PHONE:
showDialog(Constants.SETTINGS_PHONE_DIALOG);
break;
case Constants.USERNAME:
showDialog(Constants.SETTINGS_VALUE_DIALOG);
break;
case Constants.PASSWORD:
showDialog(Constants.SETTINGS_PASSWORD_DIALOG);
break;
}
}
});
}
@Override
public Dialog onCreateDialog(int dialogId) {
EditText et;
Button btn_save;
dialog = new Dialog(hvwsettings.this);
dialog.setContentView(R.layout.dia_change_value);
dialog.setTitle(setting_name);
et = (EditText) dialog.findViewById(R.id.dia_setting_new_value);
et.setText("");
btn_save = (Button) dialog.findViewById(R.id.dia_setting_save);
switch (dialogId) {
case Constants.SETTINGS_PHONE_DIALOG:
et.setHint("Telefonnummer");
et.setInputType(InputType.TYPE_CLASS_PHONE);
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText dia_et = (EditText) dialog
.findViewById(R.id.dia_setting_new_value);
DBHelper.setSetting(Constants.PHONE, dia_et.getText()
.toString());
dismissDialog(Constants.SETTINGS_PHONE_DIALOG);
displayData();
}
});
break;
case Constants.SETTINGS_VALUE_DIALOG:
et.setHint("Neuer Wert");
et.setInputType(InputType.TYPE_CLASS_TEXT);
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText dia_et = (EditText) dialog
.findViewById(R.id.dia_setting_new_value);
DBHelper.setSetting(Constants.USERNAME, dia_et.getText()
.toString());
dismissDialog(Constants.SETTINGS_VALUE_DIALOG);
displayData();
}
});
break;
case Constants.SETTINGS_PASSWORD_DIALOG:
et.setHint("Passwort");
et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText dia_et = (EditText) dialog
.findViewById(R.id.dia_setting_new_value);
DBHelper.setSetting(Constants.PASSWORD, dia_et.getText()
.toString());
dismissDialog(Constants.SETTINGS_PASSWORD_DIALOG);
displayData();
}
});
break;
}
return dialog;
}
private void displayData() {
settings_cursor = DBHelper.fetchAllSettings();
startManagingCursor(settings_cursor);
String[] header = { DbAdapter.KEY_SETTING_ID, DbAdapter.KEY_VALUE,
DbAdapter.KEY_COMMENT };
int[] to = new int[] { R.id.setting_name, R.id.setting_value,
R.id.setting_displayname };
setListAdapter(new SimpleCursorAdapter(this, R.layout.settings_row,
settings_cursor, header, to));
}
}
Alles anzeigen