2012-09-21 23 views
0

我有一個活動(Contato),顯示我的數據庫(banco > contatos > nome, telefone (database>table>rows))中的聯繫人的ListView。點擊聯繫人信息時,會出現一個對話框,並向我顯示信息和3按鈕,當我打Alterar時,它將我發送到另一個活動(Alterarcontato),我有2個編輯文本和1個按鈕。 因此,當我得到發送到Alterarcontato活動,我仍然想索引聯繫我點擊,所以我可以改變它的值(與db.update)。如何獲取點擊項目的索引到另一個活動?

Contato.java代碼ListView顯示對話框並且有它的索引。 具有editTexts和按鈕

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

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=?";是有點無效的,所以我要如何Contact.java獲得該指數已經得到所示在Alterarcontato.java中,當我在其中寫入一些內容並點擊按鈕時,數據庫中的值會發生變化?

謝謝。

+0

您可以通過Intent傳遞聯繫人的索引並檢索Alterarcontato活動中的索引。 – knvarma

+0

我該怎麼做?你能告訴我一個例子嗎? –

回答

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"); 
    } 
} 
+0

什麼是'包'的全部?我必須把我的包裹放進去嗎?例如:'com.example.mensagem;'?因爲它給我錯誤。 /和在Alterarcontato.java中,'index'顯示創建變量的錯誤,getExtras表示:'他的方法getExtas(String)對於Intent類型是未定義的,並且說添加一個Intent,當我單擊代碼時將更改爲: 'index =((Object)getIntent())。getExtas(「key」);' –

+0

上面的代碼是僞代碼。好的,我會更新代碼。 – knvarma

+0

private mIndex,errors'在令牌「mIndex」上的語法錯誤,VariableDeclarator在此令牌之後應該有效,但是aIn mIndex是變量嗎?並且還在onCreate(Bundle savedInstanceState)錯誤'類型Alterarcontato中的onCreate方法只能設置公共/受保護/私有中的一個' –

0

你可以嘗試給定的鏈接,我希望這可以幫助你

Contacts Contract API

+0

更好,如果有人已經知道並解釋它(如果可以解釋,這意味着你明白:D) –

+0

不通過索引,你可以從RAW_CONTACT_ID常量 – RajeshVijayakumar

+0

得到id我需要一天的時間來創建它,所以你可以很容易地明白 – RajeshVijayakumar

相關問題