2 回答
TA贡献2016条经验 获得超9个赞
从你的MainActivity调用中Settings使用startActivityForResult()方法
例如:
if (id == R.id.action_settings){
Intent intent = new Intent(getApplicationContext(), Settings.class);
startActivityForResult(intent,1);
return true;
}
在您Settings Activity设置要返回的数据MainActivity。如果您不想返回,请不要设置。
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
现在您main activity必须为该onActivityResult()方法编写以下代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
// here you can sorting your recyclerview
sortEmails(getEmails());
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
其他方式
如果您不想在单击setting activity后终止resetbutton,则可以使用shared preference.
例子
public class SharedPrefManager{
private static SessionManager jInstance;
private final SharedPreferences prefs;
private final SharedPreferences.Editor editor;
// private constructor to force use of
// getInstance() to create Singleton object
private SharedPrefManager(Context context) {
prefs = context.getSharedPreferences("Your_Preference_name", Context.MODE_PRIVATE);
editor = prefs.edit();
}
// create the instance when it is accessed for the first time
public static synchronized SharedPrefManager getInstance(Context context) {
if (jInstance != null) {
return jInstance;
} else {
jInstance = new SharedPrefManager(context);
return jInstance;
}
}
public void isSortedByEmail(boolean emailSorting){
editor.putString("emailSorting",emailSorting);
editor.apply();
}
public boolean isSortedByEmail(){
return prefs.getBoolean("emailSorting",false);
}
}
在你的setting activity你必须像下面这样做
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPrefManager.getInstance(context).isSortedByEmail(true);
}
});
而在你的main activity内心onStart()
@Override
protected void onStart(){
super.onStart();
if(SharedPrefManager.getInstance(context).isSortedByEmail()){
// here you can sorting your recyclerview
sortEmails(getEmails());
}
}
TA贡献1853条经验 获得超6个赞
如果您仍然想从设置活动刷新回收器视图,请使用此库。
https://github.com/greenrobot/EventBus
实施链接
https://stackoverflow.com/a/40987755/6452937
您无法在您提到的活动之间进行通信。
TA贡献1871条经验 获得超8个赞
您在“设置”中进行了初始化,但该参考与显示的MainActivity实际参考不同。MainActivity我发现适配器中与数据集相关的一些其他问题需要改进。Settings之后,您需要找到一种可以将过滤器信息从 发送到的方法MainActivity。有多种方法,但我建议您在您的情况下使用 SharedPreference。
您Settings应该将过滤器设置放入SharedPreference并获取它们的值MainActivity
public class Settings extends AppCompatActivity {
SharedPreferences.Editor editSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
editSettings = getSharedPreferences("filter", Context.MODE_PRIVATE).edit();
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editSettings.putBoolean("reset", true).apply();
}
});
}
}
在进行Adapter以下更改时,
public class RecyclerViewAdapdter extends RecyclerView.Adapter<RecyclerViewAdapdter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mImages = new ArrayList<>();
private ArrayList<String> mEmailTitle = new ArrayList<>();
private ArrayList<String> mEmailHeader = new ArrayList<>();
private ArrayList<String> mEmailContent = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapdter(Context mContext) {
this.mContext = mContext;
}
public void submitData((ArrayList<String> mImages, ArrayList<String> mEmailTitle, ArrayList<String> mEmailHeader, ArrayList<String> mEmailContent) {
this.mImages.clear();
this.mEmailTitle.clear();
this.mEmailHeader.clear();
this.mEmailContent.clear();
this.mImages.addAll(mImages);
this.mEmailTitle.addAll(mEmailTitle);
this.mEmailHeader.addAll(mEmailHeader);
this.mEmailContent.addAll(mEmailContent);
notifyDataSetChanged()
}
...
}
在MainActivity覆盖onResume()和更新列表中,当 SharedPreference 的值为 true 时,
public class MainActivity extends AppCompatActivity {
...
SharedPreferences spSettings;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spSettings = getSharedPreferences("filter", Context.MODE_PRIVATE);
...
}
@Override
protected void onResume() {
super.onResume();
boolean reset = spSettings.getBoolean("reset", false);
if(reset) {
ArrayList<AssignmentEmail> emailArray = getEmails();
sortEmails(emailArray);
}
}
private void initRecyclerView(){
...
adapter = new RecyclerViewAdapdter(mContent,this);
adapter.submitData(mImageurls,mNames,mHeaders);
...
}
public void sortEmails(ArrayList<AssignmentEmail> arrayOfEmails){
...
adapter.submitData(mImageurls,mNames,mHeaders,mContent);
}
}
添加回答
举报