How to open new activity from button of simple list view

You need a custom adapter. Most samples/tutorials show how to make a complete adapter but you can do it by just override getView() with you current adapter code. So change:

    ListAdapter adapter = new SimpleAdapter(JsonParsing.this, contactList,
            R.layout.list_item, new String[]{"id", "name","email","mobile","address","gender"},
            new int[]{R.id.id,R.id.name,R.id.email, R.id.mobile,R.id.address,R.id.gender});

to

    ListAdapter adapter = new SimpleAdapter(JsonParsing.this, contactList,
            R.layout.list_item, new String[]{"id", "name","email","mobile","address","gender"},
            new int[]{R.id.id,R.id.name,R.id.email, R.id.mobile,R.id.address,R.id.gender}){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // Codes that do custom things.
            Button trans = view.findViewById(R.id.trans);
            trans.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(JsonParsing.this, MainActivity.class);
                    HashMap<String, String> contact = (HashMap<String, String>) getItem(position);
                    intent.putExtra("id", contact.get("id"));
                    intent.putExtra("name", contact.get("name"));
                    intent.putExtra("email", contact.get("email"));
                    intent.putExtra("mobile", contact.get("mobile"));
                    intent.putExtra("address", contact.get("address"));
                    intent.putExtra("gender", contact.get("gender"));
                    startActivity(intent);
                }
            });

            return view;
        }
    };

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top