简体   繁体   中英

Returning a class from StartActivityForResult in Android

I started a activity to grab contacts from a list of all the contacts on the phone. Now my issue is how do I return the selected checks correctly from the listview, directly into my Person class. Im not fully understanding the concept of Intents and Activities in general so please bear with me.

Here is how I start the Activity from MainActivity.java

static final int PICK_CONTACT_REQUEST = 1;
public void grabthecontacts(View view) {
    Intent intent = new Intent(getApplicationContext(), SelectContactsActivity.class);
    startActivityForResult(intent, 1);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
        }
    }
}

Here is the class it calls, Select Contacts Activity

    package com.example.android.smsapp;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class SelectContactsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_contacts);
        populateListView();
        Button SubmitButton = (Button) findViewById(R.id.submitbut);

        SubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                registerDoneClick(view);
            }
        });

    }
    ArrayList<Person> list = new ArrayList<>();
    Set<Person> checkedlist = new HashSet<>();

    public void populateListView() {
        try {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            while (phones.moveToNext()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Person allcontacts = new Person(name, phoneNumber);
                list.add(allcontacts);
            }
            phones.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }


        ListView listview2 = (ListView) findViewById(R.id.contactlistview);
        ArrayAdapter<Person> adapter = new myListAdapter();
        listview2.setAdapter(adapter);
        listview2.setItemsCanFocus(false);
        listview2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    public class myListAdapter extends ArrayAdapter<Person> {

        public myListAdapter() {
            super(SelectContactsActivity.this, R.layout.da_item, list);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false);
            }
            // Find person wot work with
            Person currentperson = list.get(position);

            // Fill the view
            TextView nameboxview = (TextView) itemView.findViewById(R.id.NameView);
            nameboxview.setText(currentperson.getName());

            TextView numberboxview = (TextView) itemView.findViewById(R.id.NumberView);
            numberboxview.setText(currentperson.getPhone());
            CheckBox cb = (CheckBox)itemView.findViewById(R.id.checkBox);
            cb.setTag(position);

            if (cb.isChecked()) {
                currentperson.setChecked(true);
            }


            return itemView;
        }
    }

    public void registerDoneClick(View view) {
        for (Person allcontacts : list) {
            if(allcontacts.isChecked()) {
                Person human = new Person(allcontacts.getName(), allcontacts.getPhone());
            }
        }
// How do I return human with this activity? or is there another method?
        finish();
    }

}

Im completely lost on how to return the values I want as a person, so I can simply do

hashsetname.add(human)

Any help even with general formatting is greatly appreciated, or any java tips too. Thank you

Try this,

First make Person class to Serializable,using

public class Person implements Serializable

now set ivalue in intent like this,

Intent returnIntent = new Intent();
returnIntent.putExtra("result", human);
setResult(Activity.RESULT_OK, returnIntent);
finish();

get value from intent in onActivityResult method,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){

            Person myObject = (Person) data.getParcelableExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
        }
    }
}

add these two lines before calling finish();

Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
Intent i= new Intent();
i.putExtra("whatever",yourResult); 
setResult(Activity.RESULT_OK, i);
finish();

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