-2

应用简介:添加联系人/编辑联系人

- Contato.java//显示联系人的ListView,当itemClicked显示信息(姓名/电话)和3按钮(确定/更改/删除)的对话框时,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 中的结果表明该表已经存在,但是在代码中我看不到我在哪里显示我正在尝试创建它,错误,我尝试连接到它而不是创建它。

4

2 回答 2

1

您在这里不需要 whereArgs,因为您在 where 子句本身中附加了参数。只需提供 null 来代替 whereArgs -

db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);

但使用论点总是更好。它可以防止 sql 注入并且还负责转义特殊字符。在你的情况下 -

db.update("contatos", dataToInsert, "nome=? and telefone=?", whereArgs);

另外,您的 whereArgs 是错误的。它应该是 -

String[] whereArgs = new String[] {nomeant, foneant};

来自用户的字体Mukesh Soni

于 2012-10-01T16:35:26.713 回答
0

还使用 whereArgs 允许一些 SQLite 优化,代码中的另一个问题是您不检查更新和插入操作的返回值

db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);  

db.insert("contatos", null, valor); 

如果您的操作成功,您应该检查更新和插入的返回值,以检查操作是否真的成功。

您还应该查看这个关于 Android 上 SQLite 的好教程作为一个很好的起点。

于 2012-10-01T22:00:14.520 回答