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

这是我的MainActivity

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();
        }
    }

    private void loadEmails(){
        Log.d(TAG, "loadEmails: initBitmaps Called");
        for(int i = 0; i < emails.size(); i++) {
            mImageurls.add("@drawable/e1.jpg");
            mNames.add(emails.get(i).getAuthor());
            mHeaders.add(emails.get(i).getTitle());
            mContent.add(emails.get(i).getBody());
        }
        initRecyclerView();
    }

    public static ArrayList<AssignmentEmail> getEmails(){
        return emails;
    }

    public void sortEmails(ArrayList<AssignmentEmail> arrayOfEmails){
        Comparator<AssignmentEmail> compareByAuthor = (AssignmentEmail o1, AssignmentEmail o2) -> o1.getAuthor().compareTo( o2.getAuthor());
        Collections.sort(arrayOfEmails, compareByAuthor);
        mImageurls.clear();
        mNames.clear();
        mHeaders.clear();
        mContent.clear();
        for(int i = 0; i < arrayOfEmails.size(); i++) {
            mImageurls.add("@drawable/e1.jpg");
            mNames.add(arrayOfEmails.get(i).getAuthor());
            mHeaders.add(arrayOfEmails.get(i).getTitle());
            mContent.add(arrayOfEmails.get(i).getBody());
        }
        adapter = new RecyclerViewAdapdter(mImageurls,mNames,mHeaders,mContent,this);
        adapter.notifyDataSetChanged();
    }

    private void initRecyclerView(){
        Log.d(TAG, "initRecyclerView: init RecyclerView");
        recyclerView  = findViewById(R.id.recyclerview);
        adapter = new RecyclerViewAdapdter(mImageurls,mNames,mHeaders,mContent,this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate activity menu items.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        if (id == R.id.action_settings){
            Intent intent = new Intent(getApplicationContext(), Settings.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


这是我的RecyclerViewAdapter

package com.example.assignment_1;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import de.hdodenhof.circleimageview.CircleImageView;

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(ArrayList<String> mImages, ArrayList<String> mEmailTitle, ArrayList<String> mEmailHeader, ArrayList<String> mEmailContent, Context mContext) {
        this.mImages = mImages;
        this.mEmailTitle = mEmailTitle;
        this.mEmailHeader = mEmailHeader;
        this.mEmailContent = mEmailContent;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: Called");

        Glide.with(mContext)
                .asBitmap()
                .load(mImages.get(position))
                .into(holder.Image);

        holder.email_from.setText(mEmailTitle.get(position));
        holder.email_header.setText(mEmailHeader.get(position));
        holder.email_contents.setText(mEmailContent.get(position));
        holder.Email_interface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.d(TAG, "onClick: Email Clicked");
                //Toast.makeText(mContext, mEmailTitle.get(position),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext.getApplicationContext(), LayoutItemsActivity.class);
                intent.putExtra("EmailFrom",mEmailTitle.get(position));
                intent.putExtra("EmailHeader",mEmailHeader.get(position));
                intent.putExtra(("EmailContents"),mEmailContent.get(position));
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mEmailTitle.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        CircleImageView Image;
        TextView email_from;
        TextView email_header;
        TextView email_contents;
        RelativeLayout Email_interface;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            Image = itemView.findViewById(R.id.image);
            email_from = itemView.findViewById(R.id.EmailFrom);
            email_header = itemView.findViewById(R.id.EmailHeader);
            email_contents = itemView.findViewById(R.id.EmailContents);
            Email_interface = itemView.findViewById(R.id.email_interface);
        }
    }
}



这是我的按钮所在的位置


package com.example.assignment_1;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class Settings extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Button resetButton = findViewById(R.id.button2);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity mainActivity = new MainActivity();
                ArrayList<AssignmentEmail> emailArray = mainActivity.getEmails();
                mainActivity.sortEmails(emailArray);
            }
        });
    }

}

最佳答案

如果您仍想从设置活动中刷新回收站视图,请使用此库。

https://github.com/greenrobot/EventBus

实施链接

https://stackoverflow.com/a/40987755/6452937

您无法在提到的活动之间进行交流。

07-24 09:49