简体   繁体   中英

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

I currently have a ListView populated by SQLite and I have implemented an OnItemClickListener to list items. I want to know how to retrieve values from a Hashmap specific to the item the user clicks in the ListView, then open a new activity and populate the retrieved data into EditTexts. Any help would be appreciated!

EDIT

This is what i'm guessing:

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

Then in my next activity in the onCreate have the following:

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

Following the HUGE help in the comments from Filipe (Thanks again) the following is the solution to the problem:

In my first activity I have the following in my onItemClick for my ListView:

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

In my second activity I then used this piece of code in the 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"));
        }

You could retrieve the data for the parent adapterview, something like { parent.getItem(position) } and send that through the intent (instead of retrieving all the data from the controller). On the next activity you'd iterate through the hashmap items and set them to the apropriate EditTexts.

Edit: On your public void onItemClick(...) you should probably use:

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

And on the next activity:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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