Checked values(names) from listview to another activity

In my app I have some names(from mysql database) in listview there are 100 names corresponding with checkbox. Once I click on noe or more checkbox then corresponding name should be displayed in next activity listview. How to do this? If anybody have codes please provide me. I would be appreciated your help.. Thanks in advance..

Upvotes: 0

Views: 913

Answers (1)

Tomislav
Tomislav

Reputation: 3211

Lets say you've created your List with simple adapter like this:

ListAdapter adapter = new SimpleAdapter(MyActivity.this,arraylist,R.layout.list_item,new String[]{"name"},new int[]{R.id.txtName});
            MyActivity.this.setListAdapter(adapter);

To pass "name" to second activity you can do this:

        final ListView lv = MyActivity.this.getListView();      
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?>parent, View view, int position, long id){
                HashMap<String, String>hm = (HashMap<String, String>)lv.getItemAtPosition(position);                    
                String message=hm.get("name").toString();
                Intent in = new Intent(getApplicationContext(), SecondActivity.class);
                in.putExtra("nameToSend", message);

                startActivity(in);

            }
        });

Then in second activity you can catch name like this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
                Intent in = getIntent();
        String name = in.getStringExtra("nameToSend");
....



    }

Upvotes: 1

Related Questions