繁体   English   中英

ListView到New Activity,然后使用Hashmap中的SQLite数据填充EditText

[英]ListView to New Activity then Populate EditText's with SQLite data from Hashmap

我目前有一个由SQLite填充的ListView,并且已经实现了一个OnItemClickListener来列出项目。 我想知道如何从Hashmap中检索特定于用户在ListView中单击的项目的值,然后打开一个新活动并将检索到的数据填充到EditTexts中。 任何帮助,将不胜感激!

编辑

这就是我的猜测:

    public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            ArrayList<HashMap<String, String>> scanList = this.controller.getAllRecs();
            Intent intent = new Intent (parent.getContext(), Record.class);
            intent.putExtra("key", scanList);
        }

然后在onCreate的下一个活动中,有以下内容:

String value = getIntent().getExtras().getString("key");
        ET1.setText(value);

在Filipe的评论中提供巨大帮助之后(再次感谢),以下是该问题的解决方案:

在我的第一个活动中,我的ListView的onItemClick中包含以下内容:

public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        HashMap<String, String> hashmap = (HashMap)parent.getItemAtPosition(position);
        Intent intent = new Intent (parent.getContext(), SECONDACTIVITY.class);
        intent.putExtra("key", hashmap);
        startActivityForResult(intent, 0);
    }
}

在第二个活动中,我在onCreate中使用了这段代码:

Bundle bundle = getIntent().getExtras();
        if(bundle!=null) {
            HashMap<String, String> vals = (HashMap)bundle.getSerializable("key");
            et1.setText(vals.get("value1"));
            et2.setText(vals.get("value2"));
        }

您可以检索父adapterview的数据,类似于{parent.getItem(position)},然后通过意图将其发送(而不是从控制器中检索所有数据)。 在下一个活动中,您将遍历hashmap项并将它们设置为适当的EditTexts。

编辑:在您的public void onItemClick(...)您可能应该使用:

HashMap<String, String> yourHashMap = parent.getItemAtPosition(position); Intent intent = new Intent (parent.getContext(), Record.class); intent.putSerializable("key", yourHashMap);

在下一个活动中:

Bundle bundle = getIntent().getExtras(); if(bundle!=null) { HashMap<String, String> vals = (HashMap)bundle.getSerializable("key"); ((TextView)findViewById(R.id.txt1)).setText(vals.get("value1")); ((TextView)findViewById(R.id.txt2)).setText(vals.get("value2")); ((TextView)findViewById(R.id.txt3)).setText(vals.get("value3")); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM