1

我想将 ArrayList 的元素添加到 Recycleview。但是当我运行模拟器时,回收器中没有任何内容。

MainActivity.java

private FragmentManager fragmentManager = getSupportFragmentManager();
private FragmentHome fragmentHome = new FragmentHome();
private FragmentNotice fragmentNotice = new FragmentNotice();
private FragmentCampaign fragmentCampaign = new FragmentCampaign();
private FragmentIndividual fragmentIndividual = new FragmentIndividual();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    BottomNavigationView bottomNavigationView = findViewById(R.id.navigationView);
    transaction.replace(R.id.frameLayout, fragmentHome).commitAllowingStateLoss();
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch(item.getItemId()){
                case R.id.action_home:
                    transaction.replace(R.id.frameLayout, fragmentHome).commitAllowingStateLoss();
                    break;
                case R.id.action_notice:
                    transaction.replace(R.id.frameLayout, fragmentNotice).commitAllowingStateLoss();
                    break;
                case R.id.action_campaign:
                    transaction.replace(R.id.frameLayout, fragmentCampaign).commitAllowingStateLoss();
                    break;
                case R.id.action_private:
                    transaction.replace(R.id.frameLayout, fragmentIndividual).commitAllowingStateLoss();
                    break;
            }
            return true;
        }
    });

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

    if (user == null) {
        startMyActivity(LoginActivity.class);
    } else {
        // 회원가입 or 로그인
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        DocumentReference docRef = db.collection("users").document(user.getUid());
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        if (document.exists()) {
                            Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                        } else {
                            Log.d(TAG, "No such document");
                            startMyActivity(MemberInfoActivity.class);
                        }
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }
}


View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logoutBtn:
                FirebaseAuth.getInstance().signOut();
                startMyActivity(LoginActivity.class);
                finish();
                break;
        }
    }
};

private void startMyActivity(Class c) {
    Intent intent = new Intent(this, c);
    startActivity(intent);
}
}

MainAdapter.java

private ArrayList<String> mDataset;
private Activity activity;

static class GalleryViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    GalleryViewHolder(CardView v) {
        super(v);
        cardView = v;
    }
}

public MainAdapter(Activity activity, ArrayList<String> myDataset) {
    this.mDataset = myDataset;
    this.activity = activity;
    System.out.println(myDataset);
}

@NonNull
@Override
public MainAdapter.GalleryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post, parent, false);
    final GalleryViewHolder galleryViewHolder = new GalleryViewHolder(cardView);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return galleryViewHolder;
}

@Override
public void onBindViewHolder(@NonNull final GalleryViewHolder holder, int position) {
    CardView cardView = holder.cardView;
    TextView textView = cardView.findViewById(R.id.textView);
    textView.setText(mDataset.get(position));
}

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

FragmentCampaign.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_campaign, container, false);

    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("test text1");
    arrayList.add("test text2");
    arrayList.add("test text3");

    RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    RecyclerView.Adapter mAdapter = new MainAdapter(getActivity(), arrayList);
    recyclerView.setAdapter(mAdapter);

    return inflater.inflate(R.layout.fragment_campaign, container, false);
}
}
4

0 回答 0