0

(Contato)データベースにある連絡先の ListView を表示するアクティビティがあります(banco > contatos > nome, telefone (database>table>rows))。連絡先情報をクリックするとダイアログが表示され、(OK/Alterar/Delete)Alterar を押すと情報と 3 つのボタンが表示され、2 つの編集テキストと 1 つのボタンがある別のアクティビティ (Alterarcontato) に送られます。そのため、Alterarcontato アクティビティに送信するときに、クリックした連絡先のインデックスを保持したいので、その値を変更できます ( with db.update)。

ダイアログを表示し、そのインデックスを持つ Contato.java コード ListView。

    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);
        }
    });

値を変更するための editTexts とボタンを持つ Alterarcontato.java コード。

              EditText nomeA = (EditText) findViewById(R.id.etNomeAlter);
            EditText telefoneA = (EditText) findViewById(R.id.etTelefoneAlter);

            final String nomeB = nomeA.getText().toString();
            final String telefoneB = telefoneA.getText().toString();

            String where = "id=?";
            String[] whereArgs = {"nome", "telefone"};

            ContentValues dataToInsert = new ContentValues();                          
            dataToInsert.put("nome", nomeB);
            dataToInsert.put("telefone", telefoneB);

            db.update("contatos", dataToInsert, where, whereArgs);

しかし、Contato.javaコードに示されているように、私は連絡先のIDを持っていませんString where = "id=?";.何か書いてボタンを押すと、データベース内の値が変化しますか?

ありがとうございました。

4

3 に答える 3

1
    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);

        /// The above method will show the dialog with contact info right..? SO from the dialog you are launching the activity to edit the info. While starting the activity you have to pass the index. Like below :

           Intent intent = new Intent(getApplicationContext(), Alterarcontato.class);
           // the index is the variable which contains the index of the selected contact in your dialog.
       intent.putExtra("key", index);
           startActivity(intent);


    <-- Alterarcontato.java -->

public class Alterarcontato extends Activity {

    private Integer mIndex;


    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mIndex = getIntent().getExtras().getInt("key");
    }
 }
于 2012-09-21T18:03:54.507 に答える
0

あなたは与えられたリンクを試すことができます、私はこれがあなたを助けるかもしれないことを願っています

連絡先契約API

于 2012-09-21T17:40:51.297 に答える
0

Android Developersのサイトでこの説明をご覧ください。それは良いステップバイステップの指示を提供します. 基本的に、Intentを作成し、putExtra() メソッドを使用してデータを渡す必要があります。

編集: また、onListItemClick() を使用して ListActivity で行 ID を取得することに関する同様の質問に対するこの回答もご覧ください。

于 2012-09-21T18:46:36.987 に答える