从 Android 中的 StartActivityForResult 返回一个类
Posted
技术标签:
【中文标题】从 Android 中的 StartActivityForResult 返回一个类【英文标题】:Returning a class from StartActivityForResult in Android 【发布时间】:2016-03-27 03:23:11 【问题描述】:我开始了一项活动,以从手机上的所有联系人列表中获取联系人。现在我的问题是如何从列表视图中正确返回选定的检查,直接返回到我的 Person 类。我一般不完全理解意图和活动的概念,所以请多多包涵。
这是我从 MainActivity.java 启动 Activity 的方式
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)
这是它调用的类,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();
我完全不知道如何返回我作为一个人想要的值,所以我可以简单地做
hashsetname.add(human)
非常感谢任何帮助,即使是一般格式,或者任何 java 提示。谢谢
【问题讨论】:
我该怎么做?并使用它会返回一个人? Intent in = new Intent(activity.this, activity.class); in.putExtra("key", value); setResult(RESULT_OK, in);结束();你可以像我提到的那样传递你的价值观。并在个人活动 onActivityResult() 中获取您的值 【参考方案1】:试试这个,
首先将 Person 类设为 Serializable,使用
public class Person implements Serializable
现在像这样设置 ivalue,
Intent returnIntent = new Intent();
returnIntent.putExtra("result", human);
setResult(Activity.RESULT_OK, returnIntent);
finish();
在 onActivityResult 方法中从 Intent 中获取值,
@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)
【讨论】:
谢谢,但它给了我一个人为错误和我的错误?我想替换的东西是我想念的还是什么?即使阅读了 .putExtra 方法,我也不太了解它。public void registerDoneClick(View view) Intent returnIntent = new Intent(); for (Person allcontacts : list) if(allcontacts.isChecked()) Person human = new Person(allcontacts.getName(), allcontacts.getPhone()); returnIntent.putExtra("result", human); // How do I return human with this activity? or is there another method? setResult(Activity.RESULT_OK, returnIntent); finish();
有效,但是当我在 onActivityResult 上返回它时,我没有定义
就行 Person myObject = (Person) i.getParcelableExtra("result");它无法解析 i,i 是什么?
让我们continue this discussion in chat。【参考方案2】:
Intent i= new Intent();
i.putExtra("whatever",yourResult);
setResult(Activity.RESULT_OK, i);
finish();
【讨论】:
【参考方案3】:在调用finish();
之前添加这两行
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
【讨论】:
以上是关于从 Android 中的 StartActivityForResult 返回一个类的主要内容,如果未能解决你的问题,请参考以下文章