我在Android中使用相当简单的布局时遇到了问题。基本上,我有一个微调器,包含sqlite数据库中的几个项目。我还有一些EditText字段,用于接收和显示所选项目的信息。我现在要做的是实现一个允许用户添加新条目并更改其数据的按钮 - 所有这些都在同一个视图中(EditText字段已经编程完成)。这是我的代码(我尝试做的是使用新项目中的默认数据更新数据库并重新绘制类,但我无法将微调器设置为新位置)。你们对我如何做到这一点有什么想法吗?
public class Activity_MaterialTab extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_materialtab);
//Opens database
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estutura.db", Context.MODE_PRIVATE, null);
final Cursor cursor = Estrutura.rawQuery("SELECT * FROM MATERIAIS ORDER BY [_id] ASC", null);
final Spinner spnMaterial = (Spinner) findViewById(R.id.mat_spn_material);
final EditText dVol = (EditText) findViewById(R.id.mat_frm_densidade);
final EditText modE = (EditText) findViewById(R.id.mat_frm_elasticidade);
final EditText nome = (EditText) findViewById(R.id.mat_frm_nome);
final ImageButton addMaterial = (ImageButton) findViewById(R.id.mat_btm_add);
final ImageButton excMaterial = (ImageButton) findViewById(R.id.mat_btm_exc);
final Bundle sis = savedInstanceState;
//Spinner
String from[] = {"_id","nome","modE","dVol"};
int to[] = {R.id.txvid,R.id.txvnome,R.id.txvmodE,R.id.txvdVol};
SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.sp_material, cursor, from, to, 0);
spnMaterial.setAdapter(ad);
try {
spnMaterial.setSelection(sis.getInt("pos"));
} catch (Exception e) {}
//Updates Edittext fields
modE.setText(String.valueOf(cursor.getDouble(2)));
dVol.setText(String.valueOf(cursor.getDouble(3)));
spnMaterial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
Cursor cursor2 = Estrutura.rawQuery("SELECT * FROM MATERIAIS ORDER BY [_id] ASC", null);
cursor2.moveToPosition(cursor.getPosition());
nome.setText(String.valueOf(cursor2.getDouble(1)));
modE.setText(String.valueOf(cursor2.getDouble(2)));
dVol.setText(String.valueOf(cursor2.getDouble(3)));
cursor.moveToPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
modE.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {}
@Override
public void afterTextChanged(Editable s) {
if (modE.length() != 0) {
String sql = "UPDATE MATERIAIS SET modE="+modE.getText().toString()+" WHERE [_id]="+String.valueOf(cursor.getInt(0));
Estrutura.execSQL(sql);
}
}
});
dVol.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {}
@Override
public void afterTextChanged(Editable s) {
if (dVol.length() != 0) {
String sql = "UPDATE MATERIAIS SET dVol="+dVol.getText().toString()+" WHERE [_id]="+String.valueOf(cursor.getInt(0));
Estrutura.execSQL(sql);
}
}
});
addMaterial.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
String sql = "INSERT INTO MATERIAIS(nome,modE,dVol) VALUES('Nome',0,0)";
Estrutura.execSQL(sql);
sis.putInt("pos", cursor.getPosition());
onCreate(sis);
spnMaterial.setSelection(3);
}
});
}
}