如何获得一个点击项目的索引到另一个活动? [英] How to get the index of a clicked item into another activity?

查看:153
本文介绍了如何获得一个点击项目的索引到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动(Contato)显示我的数据库中的联系人我有一个ListView (银行> contatos>诺姆, telefone(数据库>表>行))。当点击一个联系人信息的对话框出现,并显示我的信息和3个按钮(OK / Alterar /删除)时,我打Alterar它把我送到另一个活动(Alterarcontato ),我有2个编辑文本和1按钮。
所以,当我得到发送到Alterarcontato活动我还是希望有澳联系我点击这样我就可以改变它的值指数(以 db.update )。

Contato.java code的ListView,显示该对话框,有它的索引。

 的ListView用户=(ListView控件)findViewById(R.id.lvShowContatos);
    //字符串=简单值|||的String [] =多个值/列
    的String [] =坎波斯新的String [] {诺姆,telefone};    名单=新的ArrayList<串GT;();
    C = db.query(contatos,坎波斯,NULL,NULL,NULL,NULL,NULL);
    c.moveToFirst();
    如果(c.getCount()大于0){
        而(真){
           list.add(c.getString(c.getColumnIndex(诺姆))的toString());
            如果中断(c.moveToNext()!);
        }
    }    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
            android.R.layout.simple_list_item_1,清单);    user.setAdapter(适配器);    user.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            章=位置;
            c.moveToPosition(REG);
            字符串诺姆= c.getString(c.getColumnIndex(诺姆));
            串telefone = c.getString(c.getColumnIndex(telefone));
            ShowMessage(诺姆,telefone);
        }
    });

这有editTexts Alterarcontato.java code和按钮,然后更改值。

 的EditText nomeA =(EditText上)findViewById(R.id.etNomeAlter);
            的EditText telefoneA =(EditText上)findViewById(R.id.etTelefoneAlter);            。最后弦乐nomeB = nomeA.getText()的toString();
            。最后弦乐telefoneB = telefoneA.getText()的toString();            字符串,其中=ID =?;
            的String [] = whereArgs {诺姆,telefone};            ContentValues​​ dataToInsert =新ContentValues​​();
            dataToInsert.put(诺姆,nomeB);
            dataToInsert.put(telefone,telefoneB);            db.update(contatos,dataToInsert,其中,whereArgs);

不过,如图Contato.java codeI没有为接触任何ID,因此字符串,其中=ID =?; 是有点儿无效的,所以我如何从Contact.java得到该指数已经到所示Alterarcontato.java得到所以当我把一些写作,并按下按钮,值更改数据库中的?

感谢您。


解决方案

  setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        章=位置;
        c.moveToPosition(REG);
        字符串诺姆= c.getString(c.getColumnIndex(诺姆));
        串telefone = c.getString(c.getColumnIndex(telefone));
        ShowMessage(诺姆,telefone);        ///上述方法将显示与联络信息右侧的对话框..?因此,从对话框中,您正在启动编辑的信息的活动。在启动活动中,你必须通过索引。如下图所示:           意向意图=新意图(getApplicationContext(),Alterarcontato.class);
           //该指数是包含您的对话框中选定联系人的指标变量。
       intent.putExtra(钥匙,指数);
           startActivity(意向);
    < - Alterarcontato.java - >公共类Alterarcontato延伸活动{    私人整数MINDEX;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
          super.onCreate(savedInstanceState);
          MINDEX = getIntent()getExtras()调用getInt(密钥)。;
    }
 }

I have a activity (Contato) that shows a ListView of the contacts i have in my database(banco > contatos > nome, telefone (database>table>rows)). When a Contact Info is clicked a Dialog comes up and shows me the info and 3 button (OK/Alterar/Delete) when i hit Alterar it sends me to another activity(Alterarcontato) which i have 2 Edit Texts and 1 Button. So when i get send to the Alterarcontato activity i still want to have the index o the Contact i clicked so I can change it's values ( with db.update).

Contato.java code ListView that shows the dialog and has it's index.

    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 code that has the editTexts and button to then alter the values.

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

But as shown in Contato.java code i don't have any ID for the contacts, so the String where = "id=?"; is kinda of invalid, so how do i get the index already from Contact.java to get shown in the Alterarcontato.java so when i put some writing in it and hit the button, the values change in the database?

Thank you.

解决方案

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

这篇关于如何获得一个点击项目的索引到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆