刷新firestorerecycleradapter而不重新启动活动

rwqw0loc  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(424)

我尝试构建的应用程序必须有一个微调器,可以动态更改recyclerview的项。此时,当微调器项被更改时,什么也不会发生,但当我打开另一个活动然后返回时,数据就会更改。如何在微调器所选项被更改时更改数据。下面是我的代码

package com.example.tutorscape;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;

import jp.wasabeef.picasso.transformations.CropCircleTransformation;

public class TutorListActivity extends BaseActivity {

    private SqliteDatabase mDatabase;
    boolean isFragmentLoaded;
    Fragment menuFragment;
    TextView title;
    ImageView menuButton;

    private static final String TAG = "SignedInActivity";

    //Firebase
    private FirebaseAuth.AuthStateListener mAuthListener;

    // widgets and UI References

    private RecyclerView mFirebaseList;
    private FirebaseFirestore firebaseFirestore;

    private FirestoreRecyclerAdapter adapter;

    @Override
    protected void onResume() {
        super.onResume();
        checkAuthenticationState();
    }

    private void checkAuthenticationState(){

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        if(user == null){
            Intent intent = new Intent(TutorListActivity.this, LoginStudent.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        }else{
            Log.d(TAG, "checkAuthenticationState: user is authenticated.");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initAddlayout(R.layout.activity_tutor_list);
        Spinner spinner =(Spinner)findViewById(R.id.spinner);
        firebaseFirestore = FirebaseFirestore.getInstance();
        mFirebaseList = findViewById(R.id.recyclerView);

        mDatabase = new SqliteDatabase(this);

        title = (TextView) findViewById(R.id.title_top);
        menuButton = (ImageView) findViewById(R.id.menu_icon);
        title.setText("Menu Activity");
        menuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFragmentLoaded) {
                    loadFragment();
                    title.setText("Profile");
                } else {
                    if (menuFragment != null) {
                        if (menuFragment.isAdded()) {
                            hideFragment();
                        }
                    }
                }
            }
        });

        Query query = firebaseFirestore.collection("Admin");

        FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                .setQuery(query, Model.class)
                .build();
        adapter = new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
            @NonNull
            @Override
            public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

                return new MyViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {

                holder.list_name.setText(model.getName());
                holder.list_title.setText(model.getTitle());

                holder.list_imageText.setText(model.getImage());
                holder.weeklyRate.setText(model.getWeeklyRate());
                holder.subject.setText(model.getSubject());
                holder.phone.setText(model.getPhone());
                holder.trial.setText(model.getTrial());

                Picasso.get()
                        .load(model.getImage())
                        //   .centerCrop()
                        // .placeholder(R.drawable.account)
                        // .resize(w, h)
                        .transform(new CropCircleTransformation())
                        .into(holder.imageView);
            }
        };

        mFirebaseList.setHasFixedSize(true);
        mFirebaseList.setLayoutManager(new LinearLayoutManager(TutorListActivity.this));
        mFirebaseList.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if(position==0){
                    Query query = firebaseFirestore.collection("Admin");
                    FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                            .setQuery(query, Model.class)
                            .build();
                    adapter = new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
                        @NonNull
                        @Override
                        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                            return new MyViewHolder(view);
                        }
                        @Override
                        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {
                            holder.list_name.setText(model.getName());
                            holder.list_title.setText(model.getTitle());

                            holder.list_imageText.setText(model.getImage());
                            holder.weeklyRate.setText(model.getWeeklyRate());
                            holder.subject.setText(model.getSubject());
                            holder.phone.setText(model.getPhone());
                            holder.trial.setText(model.getTrial());

                            Picasso.get()
                                    .load(model.getImage())
                                    //   .centerCrop()
                                    // .placeholder(R.drawable.account)
                                    // .resize(w, h)
                                    .transform(new CropCircleTransformation())
                                    .into(holder.imageView);
                        }
                    };
                    mFirebaseList.setHasFixedSize(true);
                    mFirebaseList.setLayoutManager(new LinearLayoutManager(TutorListActivity.this));
                    mFirebaseList.setAdapter(adapter);
                }else if(position==1){

                    Query query = firebaseFirestore.collection("one");
                    FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                            .setQuery(query, Model.class)
                            .build();
                    adapter = new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
                        @NonNull
                        @Override
                        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                            return new MyViewHolder(view);
                        }
                        @Override
                        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {
                            holder.list_name.setText(model.getName());
                            holder.list_title.setText(model.getTitle());

                            holder.list_imageText.setText(model.getImage());
                            holder.weeklyRate.setText(model.getWeeklyRate());
                            holder.subject.setText(model.getSubject());
                            holder.phone.setText(model.getPhone());
                            holder.trial.setText(model.getTrial());

                            Picasso.get()
                                    .load(model.getImage())
                                    //   .centerCrop()
                                    // .placeholder(R.drawable.account)
                                    // .resize(w, h)
                                    .transform(new CropCircleTransformation())
                                    .into(holder.imageView);
                        }
                    };
                    mFirebaseList.setHasFixedSize(true);
                    mFirebaseList.setLayoutManager(new LinearLayoutManager(TutorListActivity.this));
                    mFirebaseList.setAdapter(adapter);
                }else if(position==2){

                }else if (position==3){

                }else if(position==4){

                }else if(position==5){

                }else if (position==6){

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView list_name, subject, weeklyRate, trial, phone;
        private TextView list_title, list_imageText;
        private ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            subject = itemView.findViewById(R.id.textViewSubject);
            weeklyRate = itemView.findViewById(R.id.textViewWeeklyRate);
            trial = itemView.findViewById(R.id.textViewTrial);
            phone = itemView.findViewById(R.id.textViewPhone);

            list_imageText = itemView.findViewById(R.id.phoneTextView);
            list_name = itemView.findViewById(R.id.textViewName);
            list_title = itemView.findViewById(R.id.textViewTitle);
            imageView = itemView.findViewById(R.id.imageView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final String name = list_name.getText().toString();
                    final String ph_no = subject.getText().toString();
                    final String title = list_title.getText().toString();
                    final String sweekly = weeklyRate.getText().toString();
                    final String strial = trial.getText().toString();

                    final String image = list_imageText.getText().toString();

                    final String smsNumber = phone.getText().toString();

                    Contacts newContact = new Contacts(name, ph_no, title, image, sweekly, strial, smsNumber);
                    mDatabase.addContacts(newContact);

                    //  String smsNumber = "263774233406"; // E164 format without '+' sign
                    Intent sendIntent = new Intent(Intent.ACTION_SEND);
                    sendIntent.setType("text/plain");
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello Mr Russo.");
                    sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
                    sendIntent.setPackage("com.whatsapp");

                /*if (sendIntent.resolveActivity(getActivity().getPackageManager()) == null) {
                    Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
                    return;
                }
                */
                    startActivity(sendIntent);

                }
            });

        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    public void hideFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
        fragmentTransaction.remove(menuFragment);
        fragmentTransaction.commit();
        menuButton.setImageResource(R.drawable.ic_round_notes);
        isFragmentLoaded = false;
        title.setText("Main Activity");
    }

    public void loadFragment() {
        FragmentManager fm = getSupportFragmentManager();
        menuFragment = fm.findFragmentById(R.id.container);
        menuButton.setImageResource(R.drawable.my_up);
        if (menuFragment == null) {
            menuFragment = new MenuFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
            fragmentTransaction.add(R.id.container, menuFragment);
            fragmentTransaction.commit();
        }

        isFragmentLoaded = true;
    }
    @Override
    public void onBackPressed() {
        if (isFragmentLoaded=true){
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
            fragmentTransaction.remove(menuFragment);
            fragmentTransaction.commit();
            menuButton.setImageResource(R.drawable.ic_round_notes);
            isFragmentLoaded = false;
            title.setText("Main Activity");
        }else{
            super.onBackPressed();
        }
    }

    public void signOut() {
        Log.d(TAG, "signOut: signing out");
        FirebaseAuth.getInstance().signOut();

        //     SharedPreferences.Editor editor = getSharedPreferences(myPrefs, MODE_PRIVATE).edit();
        //   editor.putBoolean("isStudent", false);
        // editor.apply();

        Intent intent = new Intent(TutorListActivity.this, WelcomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();

    }

    /*
            ----------------------------- Firebase setup ---------------------------------
         */

    private void setupFirebaseAuth(){
        Log.d(TAG, "setupFirebaseAuth: started.");

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {

                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

                } else {
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                    Intent intent = new Intent(TutorListActivity.this, LoginStudent.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }
                // ...
            }
        };
    }
}
qnyhuwrf

qnyhuwrf1#

如果我正确地阅读了您的代码,您希望适配器在图像加载到以下位置后刷新ui:

Picasso.get()
        .load(model.getImage())
        //   .centerCrop()
        // .placeholder(R.drawable.account)
        // .resize(w, h)
        .transform(new CropCircleTransformation())
        .into(holder.imageView);

在这种情况下,您可以将回调传递给 into 打电话给我 adapter.notifyDataSetChanged 在那里。

.into(holder.imageView, new Callback() {
    @Override
    public void onSuccess() {
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onError(java.lang.Exception e) {
        Log.e("Image", "Error loading image", e);
    }
);
bt1cpqcv

bt1cpqcv2#

添加了adapter.startlistening();在每一个项目点击,现在它的工作…每个项目现在看起来如下

else if (position==6){

                    Query query = firebaseFirestore.collection("six");
                    FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                            .setQuery(query, Model.class)
                            .build();
                    adapter = new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
                        @NonNull
                        @Override
                        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                            return new MyViewHolder(view);
                        }
                        @Override
                        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {
                            holder.list_name.setText(model.getName());
                            holder.list_title.setText(model.getTitle());

                            holder.list_imageText.setText(model.getImage());
                            holder.weeklyRate.setText(model.getWeeklyRate());
                            holder.subject.setText(model.getSubject());
                            holder.phone.setText(model.getPhone());
                            holder.trial.setText(model.getTrial());

                            Picasso.get()
                                    .load(model.getImage())
                                    //   .centerCrop()
                                    // .placeholder(R.drawable.account)
                                    // .resize(w, h)
                                    .transform(new CropCircleTransformation())
                                    .into(holder.imageView);
                        }
                    };
                    mFirebaseList.setHasFixedSize(true);
                    mFirebaseList.setLayoutManager(new LinearLayoutManager(TutorListActivity.this));
                    mFirebaseList.setAdapter(adapter);

                    adapter.startListening();
                    adapter.notifyDataSetChanged();
                }

效果很好

相关问题