当从另一个活动中按下排序按钮时,Android 中的 RecyclerView 会变成空白

好的,我正在尝试对从 XML 开始初始化的 recyclerView 进行排序。当我启动应用程序时,我可以看到电子邮件,也可以单击它们。排序按钮位于“设置”活动中,但每当我回到 RecyclerView 所在的主要活动时,单击该按钮后,我的 recyclerView 就会变成空白。

这是我的主要活动

package com.example.assignment_1;


import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;


import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;


import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.Comparator;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";


    //Variables

    private ArrayList<String> mNames = new ArrayList<>();

    private ArrayList<String> mImageurls = new ArrayList<>();

    private ArrayList<String> mHeaders = new ArrayList<>();

    private ArrayList<String> mContent = new ArrayList<>();

    static ArrayList<AssignmentEmail> emails = new ArrayList<>();

    private RecyclerViewAdapdter adapter;

    private RecyclerView recyclerView;

    private Context mContext;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        EmailProvider emailProvider = new EmailProvider();

        Context context = getApplicationContext();

        if(emails.isEmpty() == true){

            emails = emailProvider.getEmails(context);

            loadEmails();

        }

    }


小唯快跑啊
浏览 124回答 2
2回答

慕沐林林

从你的MainActivity调用中Settings使用startActivityForResult()方法例如:&nbsp; &nbsp; if (id == R.id.action_settings){&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(getApplicationContext(), Settings.class);&nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent,1);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }在您Settings Activity设置要返回的数据MainActivity。如果您不想返回,请不要设置。resetButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent returnIntent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setResult(Activity.RESULT_OK,returnIntent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });现在您main activity必须为该onActivityResult()方法编写以下代码。@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; if (requestCode == 1) {&nbsp; &nbsp; &nbsp; &nbsp; if(resultCode == Activity.RESULT_OK){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here you can sorting your recyclerview&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sortEmails(getEmails());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (resultCode == Activity.RESULT_CANCELED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Write your code if there's no result&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}其他方式如果您不想在单击setting activity后终止resetbutton,则可以使用shared preference.例子public class SharedPrefManager{&nbsp; private static SessionManager jInstance;&nbsp; private final SharedPreferences prefs;&nbsp; private final SharedPreferences.Editor editor;&nbsp; // private constructor to force use of&nbsp;&nbsp; // getInstance() to create Singleton object&nbsp;&nbsp; private SharedPrefManager(Context context) {&nbsp; &nbsp; &nbsp; prefs = context.getSharedPreferences("Your_Preference_name", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; editor = prefs.edit();&nbsp; }&nbsp; // create the instance when it is accessed for the first time&nbsp; public static synchronized SharedPrefManager getInstance(Context context) {&nbsp; &nbsp; &nbsp; if (jInstance != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return jInstance;&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jInstance = new SharedPrefManager(context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return jInstance;&nbsp; &nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp;&nbsp; public void isSortedByEmail(boolean emailSorting){&nbsp; &nbsp; &nbsp; editor.putString("emailSorting",emailSorting);&nbsp; &nbsp; &nbsp; editor.apply();&nbsp; }&nbsp;&nbsp; public boolean isSortedByEmail(){&nbsp; &nbsp; &nbsp; return prefs.getBoolean("emailSorting",false);&nbsp; }}在你的setting activity你必须像下面这样做resetButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; SharedPrefManager.getInstance(context).isSortedByEmail(true);&nbsp; &nbsp; }});而在你的main activity内心onStart()@Overrideprotected void onStart(){&nbsp; &nbsp;super.onStart();&nbsp; &nbsp;if(SharedPrefManager.getInstance(context).isSortedByEmail()){&nbsp; &nbsp; &nbsp;// here you can sorting your recyclerview&nbsp; &nbsp; &nbsp;sortEmails(getEmails());&nbsp; &nbsp;}}

墨色风雨

如果您仍然想从设置活动刷新回收器视图,请使用此库。https://github.com/greenrobot/EventBus实施链接https://stackoverflow.com/a/40987755/6452937您无法在您提到的活动之间进行通信。

ITMISS

您在“设置”中进行了初始化,但该参考与显示的MainActivity实际参考不同。MainActivity我发现适配器中与数据集相关的一些其他问题需要改进。Settings之后,您需要找到一种可以将过滤器信息从 发送到的方法MainActivity。有多种方法,但我建议您在您的情况下使用 SharedPreference。您Settings应该将过滤器设置放入SharedPreference并获取它们的值MainActivitypublic class Settings extends AppCompatActivity {&nbsp; &nbsp; SharedPreferences.Editor editSettings;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; editSettings = getSharedPreferences("filter", Context.MODE_PRIVATE).edit();&nbsp; &nbsp; &nbsp; &nbsp; resetButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editSettings.putBoolean("reset", true).apply();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}在进行Adapter以下更改时,public class RecyclerViewAdapdter extends RecyclerView.Adapter<RecyclerViewAdapdter.ViewHolder>&nbsp; {&nbsp; &nbsp; private static final String TAG = "RecyclerViewAdapter";&nbsp; &nbsp; private ArrayList<String> mImages = new ArrayList<>();&nbsp; &nbsp; private ArrayList<String> mEmailTitle = new ArrayList<>();&nbsp; &nbsp; private ArrayList<String> mEmailHeader = new ArrayList<>();&nbsp; &nbsp; private ArrayList<String> mEmailContent = new ArrayList<>();&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; public RecyclerViewAdapdter(Context mContext) {&nbsp; &nbsp; &nbsp; &nbsp; this.mContext = mContext;&nbsp; &nbsp; }&nbsp; &nbsp; public void submitData((ArrayList<String> mImages, ArrayList<String> mEmailTitle, ArrayList<String> mEmailHeader, ArrayList<String> mEmailContent) {&nbsp; &nbsp; &nbsp; &nbsp; this.mImages.clear();&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailTitle.clear();&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailHeader.clear();&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailContent.clear();&nbsp; &nbsp; &nbsp; &nbsp; this.mImages.addAll(mImages);&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailTitle.addAll(mEmailTitle);&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailHeader.addAll(mEmailHeader);&nbsp; &nbsp; &nbsp; &nbsp; this.mEmailContent.addAll(mEmailContent);&nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged()&nbsp; &nbsp; }&nbsp; &nbsp; ...}在MainActivity覆盖onResume()和更新列表中,当 SharedPreference 的值为 true 时,public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; ...&nbsp; &nbsp; SharedPreferences spSettings;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; spSettings = getSharedPreferences("filter", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; boolean reset = spSettings.getBoolean("reset", false);&nbsp; &nbsp; &nbsp; &nbsp; if(reset) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<AssignmentEmail> emailArray = getEmails();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sortEmails(emailArray);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void initRecyclerView(){&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; adapter = new RecyclerViewAdapdter(mContent,this);&nbsp; &nbsp; &nbsp; &nbsp; adapter.submitData(mImageurls,mNames,mHeaders);&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; public void sortEmails(ArrayList<AssignmentEmail> arrayOfEmails){&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; adapter.submitData(mImageurls,mNames,mHeaders,mContent);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java