我已经尝试了一段时间才能让这个工作起作用但无济于事。我试图从我的DetailActivity中检索数据并将其返回到我的MainActivity。任何人都可以提供帮助。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(LOG_TAG, "KJHVLERHVRHOGVINORWUI " + MY_CHILD_ACTIVITY + " " + resultCode +" "+Activity.RESULT_OK);
switch(requestCode) {
case (MY_CHILD_ACTIVITY) : {
if (resultCode == Activity.RESULT_OK) {
System.out.println("LOG_TAG"+ "MainActivity.EXTRA_SEARCH" + data.getStringExtra(MainActivity.EXTRA_SEARCH));
}
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
artistsAlbums = new ArrayList<>();
tempAlbums = new ArrayList<>();
final RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(END_POINT)
.build();
service = adapter.create(APIService.class);
EditText tview = (EditText) findViewById(R.id.artist_field);
tview.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.v("LOG_TAG", s + " onTextChanged() " + " start: " + start + " count: " + count + " after: " + after);
}
@Override
public void afterTextChanged(Editable s) { //aftertextchanged{
Log.v("LOG_TAG", s + " afterTextChanged()");
if (listViewRefresh) {
if (mAdapter != null) {
//mAdapter.clear();
//mAdapter.notifyDataSetChanged();
artistsAlbums.clear();
displayItems();
}
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
name = s.toString();
Log.v("LOG_TAG", s.length() + " onTextChanged() " + " start: " + start + " before: " + before + " count:" + count);
//listViewRefresh = (start == 0) && (before == 1) && (count == 0);
listViewRefresh = name.length() == 0;
//======================================A R T I S T S=====================================m============
service.searchArtists(name, new Callback<ArtistsPager>() {
@Override
public void success(ArtistsPager artistsPager, Response response) {
Image image;
if (artistsPager.artists.items != null) { //for .. 6
artistsAlbums.clear(); //this line doesn't work!!
//artistsAlbums = new ArrayList<ArtistsAlbum>(); //this line works but is wrong!!
for (int i = 0; i < artistsPager.artists.items.size(); i++) { //for .. 1
try { // try .. 1
mName = artistsPager.artists.items.get(i).name; //Breakpoint here!!
mId = artistsPager.artists.items.get(i).id;
int size = artistsPager.artists.items.get(i).images.size();
if (size == 0) {
//------------------------------------------
album = new ArtistsAlbum();
album.setName(mName);
album.setId(mId);
artistsAlbums.add(album);
//------------------------------------------
} else {
for (int j = 0; j < size; j++) {
if ((300 <= artistsPager.artists.items.get(i).images.get(j).width) &&
(650 >= artistsPager.artists.items.get(i).images.get(j).width)){
mUrl = artistsPager.artists.items.get(i).images.get(j).url;
album = new ArtistsAlbum();
album.setUrl(mUrl);
album.setName(mName);
album.setId(mId);
artistsAlbums.add(album);
break;
}
}
}
} catch (IndexOutOfBoundsException e) { //try .. 1
e.printStackTrace();
}
}// for .. 1
if (!listViewRefresh )
displayItems();
else {
artistsAlbums.clear();
displayItems();
}
//displayItems(artistsAlbums);
Log.v(LOG_TAG, "======================================= TOP ================================================");
for (ArtistsAlbum obj : artistsAlbums)
System.out.println("LOG_TAG" + "name: " + obj.getName() + " id: " + obj.getId() + " Url: " + obj.getUrl());
Log.v(LOG_TAG, "====================================== BOTTOM ==============================================");
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, final View view,
int position, long id) {
final ArtistsAlbum item = (ArtistsAlbum) parent.getItemAtPosition(position);
// System.out.println("LOG_TAG" + "name: " + item.getName() + " id: " + item.getId() + " Url: " + item.getUrl());
//sendMessage(item.getId());
intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra(EXTRA_MESSAGE, item.getId());
intent.putExtra(EXTRA_SEARCH, name);
startActivityForResult(intent, MY_CHILD_ACTIVITY);
}
});
} //for..6
}
@Override
public void failure(RetrofitError error) {
}
});
} //onTextChanged
});
}
在DetailActivity中,我在onPause()回调函数中有我的SetResult():
@Override
protected void onPause () {
super.onPause();
Log.v(LOG_TAG, "onPause()");
Intent in = getIntent();
in.putExtra(MainActivity.EXTRA_SEARCH, searchStr);
setResult(Activity.RESULT_OK, in);
Log.v(LOG_TAG, "Activity.RESULT_OK" + in);
finish();
}
为了完整起见,我提供了AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.richard.webapitest" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.richard.webapitest.MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.richard.webapitest.MainActivity" />
</activity>
</application>
</manifest>
非常感谢
附录: 我的Log Cat在运行后如下:
07-27 08:40:49.091 2080-2080/com.example.richard.webapitest V/LOG_TAG﹕ onTextChanged() start: 0 count: 0 after: 1
07-27 08:40:49.091 2080-2080/com.example.richard.webapitest V/LOG_TAG﹕ 1 onTextChanged() start: 0 before: 0 count:1
07-27 08:40:49.127 2080-2080/com.example.richard.webapitest V/LOG_TAG﹕ e afterTextChanged()
07-27 08:40:49.515 2080-2294/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 318K, 4% free 9195K/9540K, paused 7ms, total 8ms
07-27 08:40:49.519 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest I/dalvikvm﹕ Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to resolve static method 17524: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest I/dalvikvm﹕ Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest W/dalvikvm﹕ VFY: unable to resolve static method 17523: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
07-27 08:40:49.523 2080-2294/com.example.richard.webapitest D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
07-27 08:40:49.767 2080-2080/com.example.richard.webapitest V/MainActivity﹕ in displayItems0
07-27 08:40:49.767 2080-2080/com.example.richard.webapitest V/MainActivity﹕ ======================================= TOP ================================================
07-27 08:40:49.767 2080-2080/com.example.richard.webapitest V/MainActivity﹕ ====================================== BOTTOM ==============================================
07-27 08:40:50.255 2080-2304/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 390K, 5% free 9319K/9736K, paused 5ms, total 5ms
07-27 08:40:50.275 2080-2304/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 25K, 4% free 9352K/9736K, paused 6ms, total 6ms
07-27 08:40:50.275 2080-2304/com.example.richard.webapitest I/dalvikvm-heap﹕ Grow heap (frag case) to 10.036MB for 921612-byte allocation
07-27 08:40:50.283 2080-2094/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 10252K/10640K, paused 7ms, total 7ms
07-27 08:40:50.951 2080-2314/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 75K, 4% free 10257K/10640K, paused 7ms, total 7ms
07-27 08:40:50.959 2080-2314/com.example.richard.webapitest I/dalvikvm-heap﹕ Grow heap (frag case) to 11.604MB for 1638412-byte allocation
07-27 08:40:50.967 2080-2314/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 0K, 4% free 11857K/12244K, paused 7ms, total 7ms
07-27 08:40:51.431 2080-2098/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 75K, 4% free 11859K/12244K, paused 6ms, total 6ms
07-27 08:40:51.435 2080-2098/com.example.richard.webapitest I/dalvikvm-heap﹕ Grow heap (frag case) to 12.535MB for 974412-byte allocation
07-27 08:40:51.443 2080-2098/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 0K, 3% free 12810K/13196K, paused 7ms, total 7ms
07-27 08:40:51.723 2080-2099/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 75K, 3% free 12811K/13196K, paused 6ms, total 6ms
07-27 08:40:51.727 2080-2099/com.example.richard.webapitest I/dalvikvm-heap﹕ Grow heap (frag case) to 13.668MB for 1188552-byte allocation
07-27 08:40:51.743 2080-2099/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 0K, 3% free 13972K/14360K, paused 6ms, total 6ms
07-27 08:40:51.975 2080-2295/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 149K, 3% free 15041K/15448K, paused 7ms, total 7ms
07-27 08:40:52.187 2080-2295/com.example.richard.webapitest D/dalvikvm﹕ GC_FOR_ALLOC freed 74K, 3% free 16641K/17052K, paused 5ms, total 5ms
07-27 08:40:59.359 2080-2080/com.example.richard.webapitest W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
07-27 08:41:05.459 2080-2080/com.example.richard.webapitest V/DetailActivity﹕ onPause()
07-27 08:41:05.459 2080-2080/com.example.richard.webapitest V/DetailActivity﹕ Activity.RESULT_OKIntent { cmp=com.example.richard.webapitest/.DetailActivity (has extras) }
07-27 08:41:05.539 2080-2080/com.example.richard.webapitest W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
答案 0 :(得分:0)
@Override
protected void onPause()
{
Log.v(LOG_TAG, "onPause()");
Intent data = new Intent();
data.putExtra(MainActivity.EXTRA_SEARCH, searchStr);
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
Log.v(LOG_TAG, "Activity.RESULT_OK" + in);
super.onPause();
}