You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从含RecyclerView的Activity返回时,如何更新当前Activity的TextView?

How to Update TextView in ActivitySerieJson When Returning From Another Activity

Hey there! No worries about your English at all—totally get what you're trying to build. The core issue here is passing updated data back from your second Activity to ActivitySerieJson and refreshing the RecyclerView to reflect those changes. Here's a step-by-step solution using Android's modern ActivityResultLauncher (since the old startActivityForResult is deprecated):

1. Register an ActivityResultLauncher in ActivitySerieJson

First, you need a way to listen for results coming back from your second Activity. Add this code in ActivitySerieJson (adjust for Kotlin/Java as needed):

Kotlin Example

private val updateSeasonWatchRateLauncher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    // Check if we got a valid result
    if (result.resultCode == RESULT_OK) {
        result.data?.let { data ->
            // Extract the updated data from the intent
            val updatedSeason = data.getParcelableExtra<Season>("UPDATED_SEASON")
            
            updatedSeason?.let { season ->
                // Find the matching season in your data list
                val seasonIndex = yourSeasonDataList.indexOfFirst { it.number == season.number }
                if (seasonIndex != -1) {
                    // Update the data source
                    yourSeasonDataList[seasonIndex] = season
                    // Refresh only the affected RecyclerView item (more efficient than full refresh)
                    yourRecyclerViewAdapter.notifyItemChanged(seasonIndex)
                }
            }
        }
    }
}

Java Example

ActivityResultLauncher<Intent> updateSeasonWatchRateLauncher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == RESULT_OK) {
            Intent data = result.getData();
            if (data != null) {
                // Extract updated season data (make sure your Season class implements Parcelable)
                Season updatedSeason = data.getParcelableExtra("UPDATED_SEASON");
                
                if (updatedSeason != null) {
                    // Find the index of the season to update
                    int seasonIndex = -1;
                    for (int i = 0; i < yourSeasonDataList.size(); i++) {
                        if (yourSeasonDataList.get(i).getNumber() == updatedSeason.getNumber()) {
                            seasonIndex = i;
                            break;
                        }
                    }
                    
                    if (seasonIndex != -1) {
                        yourSeasonDataList.set(seasonIndex, updatedSeason);
                        yourRecyclerViewAdapter.notifyItemChanged(seasonIndex);
                    }
                }
            }
        }
    }
);

2. Launch the Second Activity with the Launcher

When the user taps a CardView in ActivitySerieJson's RecyclerView, use the launcher to start your second Activity, and pass along the current season data so the second Activity knows what to modify:

Kotlin Example (in your RecyclerView Adapter's click listener)

val intent = Intent(context, YourSecondActivity::class.java)
intent.putExtra("SELECTED_SEASON", season) // Pass the current season object
(context as ActivitySerieJson).updateSeasonWatchRateLauncher.launch(intent)

Java Example

Intent intent = new Intent(context, YourSecondActivity.class);
intent.putExtra("SELECTED_SEASON", season);
((ActivitySerieJson) context).updateSeasonWatchRateLauncher.launch(intent);

3. Pass Updated Data Back From the Second Activity

Once the user makes changes (like updating the watch completion rate) in the second Activity, send the updated data back to ActivitySerieJson before finishing:

Kotlin Example

// When the user saves their changes (e.g., button click)
val updatedSeason = Season(
    number = selectedSeason.number,
    thumbnailUrl = selectedSeason.thumbnailUrl,
    watchRate = newCompletionRate // The updated value from user input
)

val resultIntent = Intent()
resultIntent.putExtra("UPDATED_SEASON", updatedSeason)
setResult(RESULT_OK, resultIntent)
finish()

Java Example

// When saving changes
Season updatedSeason = new Season(
    selectedSeason.getNumber(),
    selectedSeason.getThumbnailUrl(),
    newCompletionRate
);

Intent resultIntent = new Intent();
resultIntent.putExtra("UPDATED_SEASON", updatedSeason);
setResult(RESULT_OK, resultIntent);
finish();

Important Notes

  • Parcelable Requirement: For passing custom objects like Season between Activities, make sure your Season class implements Parcelable (Kotlin can use @Parcelize for simplicity, Java requires writing the parcel logic manually).
  • Efficient Refresh: Use notifyItemChanged(seasonIndex) instead of notifyDataSetChanged()—this only refreshes the specific CardView that changed, which is better for performance.
  • LiveData Alternative: If you're using LiveData for your data source, updating the LiveData will automatically trigger a RecyclerView refresh, so you won't need to call notify methods manually.

内容的提问来源于stack exchange,提问作者user9462254

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:26:59