簡介應用程序:添加聯繫人/編輯聯繫人試圖DATABSE.update顯示錯誤
- Contato.java
//顯示聯繫人的一個ListView時itemClicked顯示信息(姓名/電話)和3Buttons的對話框(OK /更改/刪除)Alter
按鈕將用戶:
- Adicionarcontato.java
與信息的編輯,但是當我編輯並按下按鈕「Salvar」(保存)錯誤:The application Mensagem(process com.example.mensagem) has stopped unexpectedly. Please try again.
下面是代碼ListView的Contato.java。
private void ListaContatos(){
ListView user = (ListView) findViewById(R.id.lvShowContatos);
//String = simple value ||| String[] = multiple values/columns
String[] campos = new String[] {"nome", "telefone"};
list = new ArrayList<String>();
c = db.query("contatos", campos, null, null, null, null, null);
c.moveToFirst();
if(c.getCount() > 0) {
while(true) {
list.add(c.getString(c.getColumnIndex("nome")).toString());
if(!c.moveToNext()) break;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
user.setAdapter(adapter);
user.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
reg = position;
c.moveToPosition(reg);
String nome = c.getString(c.getColumnIndex("nome"));
String telefone = c.getString(c.getColumnIndex("telefone"));
ShowMessage(nome, telefone);
}
});
}
這裏是在Adicionarcontato.java代碼:
public SQLiteDatabase db;
private String mIndex = "";
private String nomeant,foneant;
static final String userTable = "contatos";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.adicionarcontato);
if(getIntent().getExtras() != null) {
if(getIntent().getExtras().containsKey("reg")) mIndex = getIntent().getExtras().getString("reg");
}
db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);
if(!mIndex.equals("")) {
Cursor c = db.query(false, "contatos", (new String[] {"nome", "telefone"}), null, null, null, null, null, null);
c.moveToPosition(Integer.parseInt(mIndex));
nomeant = c.getString(0);
foneant = c.getString(1);
EditText nome1 = (EditText) findViewById(R.id.etNome);
EditText telefone1 = (EditText) findViewById(R.id.etTelefone);
nome1.setText(nomeant);
telefone1.setText(foneant);
}
AdicionarContato();
ResetarInfo();
}
而且按鈕 「Salvar」 的代碼,點擊時:
public void AdicionarContato() {
// TODO Auto-generated method stub
final EditText nm = (EditText) findViewById(R.id.etNome);
final EditText tlf = (EditText) findViewById(R.id.etTelefone);
Button add = (Button) findViewById(R.id.bSalvarContato);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final String nome = nm.getText().toString();
final String telefone = tlf.getText().toString();
if(nome.length() != 0 && telefone.length() != 0){
if(mIndex.equals("")) {
ContentValues valor = new ContentValues();
valor.put("nome", nome);
valor.put("telefone", telefone);
db.insert("contatos", null, valor);
ShowMessage("Sucesso","O Contato " + nome + " foi salvo com sucesso");
}
else {
String[] whereArgs = {"nome", "telefone"};
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("nome", nome);
dataToInsert.put("telefone", telefone);
db.update("contatos", dataToInsert, "nome='"+nomeant+" and telefone='"+foneant+"'", whereArgs);
ShowMessage("Sucesso","O Contato " + nome + " foi salvo com sucesso");
}
}
}
});
}
的logcat的錯誤它表明: Failure 1 (table contatos already exists) on 0x2205b0 when preparing 'create table contatos(nome varchar(50),telefone varchar(20))'.
我的POV:LogCat中的結果表明表已經存在,但在代碼中,我無法看到我在哪裏顯示,即時嘗試創建它,錯了,我嘗試連接到它,而不是創建它。
看起來您正嘗試重新創建一個已經存在的表。我對'SQLite'的瞭解是有限的,但經過快速搜索後查看http:// stackoverflow。com/questions/4293826/problem-with-sqlite-oncreate-in-android,你也有sqlite關鍵字'IF NOT EXISTS'來附加到'CREATE TABLE' sql字符串:'CREATE TABLE IF NOT EXISTS contatos ... '。另外,瞭解你如何創建/填充sqlite數據庫會很有幫助。 – Luksprog
由我的main.java onCreate時,但我知道它的創建和填充好嗎,因爲我只有「添加聯繫人」,但現在改變聯繫人,如果您閱讀代碼將顯示如果EditTexts值爲空白,它將添加一個新的聯繫。 –