我之前没有遇到任何问题,所以我可能错过了一些愚蠢的事情。
我正在尝试启动一个辅助活动,在我的数据库中创建一个新的所有者并返回所有者的ID。
在我的第一个活动中,我正在呼叫第二个活动:
Intent new_owner = new Intent(this, AddOwnerActivity.class);
startActivityForResult(new_owner, REQUEST_NEW_OWNER, null);
overridePendingTransition(R.anim.slide_in_bottom, R.anim.abc_fade_out);
然后在我的第二个Activity中我设置结果如下:
Intent data = getIntent();
Log.v("OWNER", "DATA sending: "+new_owner.getId()); // this logs a number like 13
data.putExtra("owner_id", new_owner.getId() );
setResult(RESULT_OK,data);
onBackPressed();//this calls finish();
最后在我的第一个Activity中,我的onActivityResult看起来像这样:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_NEW_OWNER && resultCode == RESULT_OK && data!=null){
Bundle bundle = data.getExtras();
setUpOwnerSelector();
Log.v("OWNER", "DATA received: " + bundle.getInt("owner_id"));//this is ALWAYS LOGGING 0
selectOwnerWithId(bundle.getInt("owner_id"));
}
}
我可以看到我的新所有者是在数据库中成功创建的,而在我的辅助活动中,当我设置结果时,它正在记录正确的ID,但我的第一个活动是始终记录0作为额外的...
我必须要错过一些小东西......第二双眼睛会受到赞赏
第一项活动
package com.eliddell.clicktrainer.Activities;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.eliddell.clicktrainer.Data.DataSource;
import com.eliddell.clicktrainer.Data.Models.Owner;
import com.eliddell.clicktrainer.R;
import java.util.ArrayList;
public class NewDogActivity extends ActionBarActivity {
private DataSource db;
private ArrayList<Owner> owners;
private ArrayList<String> owner_selector_items;
private Spinner owner_selector;
private EditText dog_name_field;
private Button save_btn;
private Button cancel_btn;
public static final int REQUEST_NEW_OWNER =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_dog);
owner_selector = (Spinner)findViewById(R.id.owner_spinner);
save_btn = (Button)findViewById(R.id.save_btn);
cancel_btn = (Button)findViewById(R.id.cancel_btn);
db = new DataSource(this);
try{
db.open();
}catch(Exception e){}
}
@Override
public void onResume(){
super.onResume();
setUpOwnerSelector();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_NEW_OWNER && resultCode == RESULT_OK && data!=null){
Bundle bundle = data.getExtras();
setUpOwnerSelector();
Log.v("OWNER", "DATA received: " + bundle.getInt("owner_id"));
selectOwnerWithId(bundle.getInt("owner_id"));
}
}
@Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.abc_fade_in, R.anim.slide_out_bottom);
finish();
}
private void save(){
}
private void selectOwnerWithId(int id){
int position =2;
Owner o;
for(int i=0;i<owners.size();i++){
o = owners.get(i);
if(o.getId()==id){
Log.v("MASTER","OWNER FOUND");
position+=i;
}
}
owner_selector.setSelection(position);
}
private void createNewOwner(){
Intent new_owner = new Intent(this, AddOwnerActivity.class);
startActivityForResult(new_owner, REQUEST_NEW_OWNER, null);
overridePendingTransition(R.anim.slide_in_bottom, R.anim.abc_fade_out);
}
private void setUpOwnerSelector(){
owners = (ArrayList)db.getAllOwners();
owner_selector_items = new ArrayList<>();
owner_selector_items.add(getResources().getString(R.string.no_owner));
owner_selector_items.add(getResources().getString(R.string.newOwner));
for(int i=0;i<owners.size();i++){
owner_selector_items.add(owners.get(i).getLast_name()+", "+owners.get(i).getFirst_name());
}
ArrayAdapter<String> spinnerCountShoesArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, owner_selector_items);
owner_selector.setAdapter(spinnerCountShoesArrayAdapter);
owner_selector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
//TODO create new owner for result
createNewOwner();
} else if (position > 1) {
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
第二项活动
package com.eliddell.clicktrainer.Activities;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.eliddell.clicktrainer.Data.DataSource;
import com.eliddell.clicktrainer.Data.Models.Owner;
import com.eliddell.clicktrainer.R;
public class AddOwnerActivity extends ActionBarActivity {
private Owner new_owner;
private Boolean startedForResult = false;
private EditText first_name_field;
private EditText last_name_field;
private EditText phone_field;
private EditText email_field;
private Button save_btn;
private Button cancel_btn;
private DataSource db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_owner);
if(getCallingActivity()!=null){
startedForResult =true;
}
db = new DataSource(this);
try{
db.open();
}catch (Exception e){}
first_name_field = (EditText)findViewById(R.id.firstname_field);
last_name_field =(EditText)findViewById(R.id.lastname_field);
phone_field = (EditText)findViewById(R.id.telephone_field);
email_field = (EditText)findViewById(R.id.email_field);
save_btn = (Button)findViewById(R.id.save_btn);
save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
}
});
cancel_btn = (Button)findViewById(R.id.cancel_btn);
cancel_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancel();
}
});
}
@Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.abc_fade_in, R.anim.slide_out_bottom);
finish();
}
private void save(){
if(first_name_field.getText().toString().isEmpty()){
Toast.makeText(this, getResources().getString(R.string.first_name_hint), Toast.LENGTH_SHORT);
first_name_field.requestFocus();
return;
}else if(last_name_field.getText().toString().isEmpty()){
Toast.makeText(this, getResources().getString(R.string.last_name_hint), Toast.LENGTH_SHORT);
last_name_field.requestFocus();
return;
}
new_owner = db.createOwner(first_name_field.getText().toString(),
last_name_field.getText().toString(),
phone_field.getText().toString(),
email_field.getText().toString());
if(startedForResult && new_owner!=null){
Intent data = new Intent();
Log.v("OWNER", "DATA sending: "+new_owner.getId());
data.putExtra("owner_id", new_owner.getId());
setResult(RESULT_OK,data);
onBackPressed();
}
}
private void cancel(){
if(startedForResult){
setResult(RESULT_CANCELED);
}
onBackPressed();
}
}
以防万一与我的构建/目标sdk有关:
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.eliddell.clicktrainer"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:cardview-v7:21.0.+'
}