如何将点击的项目的索引转换为另一个活动?

时间:2012-09-21 17:33:28

标签: android database listview

我有一个活动(Contato),其中显示了我的数据库(banco > contatos > nome, telefone (database>table>rows))中的联系人列表视图。当点击一个联系人信息时,会出现一个对话框并显示信息和3按钮(OK/Alterar/Delete)当我点击Alterar时,它会将我发送到另一个活动(Alterarcontato),我有2个编辑文本和1个按钮。 因此,当我收到发送到Alterarcontato活动时,我仍然希望索引联系我点击,以便我可以更改它的值(使用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);
        }
    });

Alterarcontato.java代码,具有editTexts和按钮,然后更改值。

              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所以当我在其中写入一些内容并按下按钮时,数据库中的值会发生变化吗?

谢谢。

3 个答案:

答案 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");
    }
 }

答案 1 :(得分:0)

您可以尝试给定的链接,我希望这可以帮助您

Contacts Contract API

答案 2 :(得分:0)

查看this description on the Android Developers'网站。它提供了良好的逐步说明。基本上,您必须创建Intent并使用putExtra()方法传递数据。

编辑:另请参阅this answer to a similar question,了解如何使用onListItemClick()获取ListActivity中的行ID。