Android 回收器适配器中的getItem计数返回0

Android 回收器适配器中的getItem计数返回0,android,android-recyclerview,Android,Android Recyclerview,我正在使用数据绑定处理recyclerView。在适配器类中,getItemcount()返回0,而ArrayList大小不是0。我将我的代码发布在下面适配器类和viewmodel类的位置。我使用回调函数调用viewmodel类。在日志中,适配器类的addAll()中的cat arraylist.size不是0,而是在同一个类getitemcount()中,将arraylist.size()显示为0 适配器类 import android.util.Log; import com.codest

我正在使用数据绑定处理recyclerView。在适配器类中,getItemcount()返回0,而ArrayList大小不是0。我将我的代码发布在下面适配器类和viewmodel类的位置。我使用回调函数调用viewmodel类。在日志中,适配器类的addAll()中的cat arraylist.size不是0,而是在同一个类getitemcount()中,将arraylist.size()显示为0

适配器类

import android.util.Log;

import com.codestrela.quizzer.R;
import com.codestrela.quizzer.base.recycler.RecyclerBaseAdapter;
import com.codestrela.quizzer.viewmodel.RowMatchingOptionViewModel;

import java.util.ArrayList;

public class MatchingOptionAdapter extends RecyclerBaseAdapter {
    ArrayList<RowMatchingOptionViewModel> arrayList;
    private static final String TAG = "MatchingOptionAdapter";

    public MatchingOptionAdapter(ArrayList<RowMatchingOptionViewModel> arrayList) {
        this.arrayList = arrayList;
    }

    public void addAll(ArrayList<RowMatchingOptionViewModel> viewModels) {
        this.arrayList.addAll(viewModels);
        Log.e(TAG, "addAll: "+arrayList.size());
        notifyDataSetChanged();
    }

    public void add(RowMatchingOptionViewModel rowMatchingOptionViewModel) {
        this.arrayList.add(rowMatchingOptionViewModel);
        notifyDataSetChanged();
    }

    @Override
    protected Object getObjForPosition(int position) {
        return this.arrayList.get(position);
    }

    @Override
    protected int getLayoutIdForPosition(int position) {
        return R.layout.row_matching_option;
    }

    @Override
    public int getItemCount() {
        Log.e(TAG, "getItemCount: "+arrayList.size() );
        return arrayList.size();
    }
}
导入android.util.Log;
导入com.codestrela.quizzer.R;
导入com.codestrela.quizzer.base.recycler.recyclerbasedapter;
导入com.codestrela.quizzer.viewmodel.rowmachingoptionviewmodel;
导入java.util.ArrayList;
公共类匹配optionAdapter扩展了RecyclerBaseAdapter{
ArrayList ArrayList;
私有静态最终字符串TAG=“MatchingOptionAdapter”;
公共匹配选项适配器(ArrayList ArrayList){
this.arrayList=arrayList;
}
public void addAll(ArrayList视图模型){
this.arrayList.addAll(viewModels);
Log.e(标记“addAll:+arrayList.size());
notifyDataSetChanged();
}
公共无效添加(RowMatchingOptionViewModel RowMatchingOptionViewModel){
this.arrayList.add(rowMatchingOptionViewModel);
notifyDataSetChanged();
}
@凌驾
受保护对象getObjForPosition(int位置){
返回此.arrayList.get(位置);
}
@凌驾
受保护的内部GetLayoutForPosition(内部位置){
返回R.layout.row\u匹配选项;
}
@凌驾
public int getItemCount(){
Log.e(标记“getItemCount:+arrayList.size());
返回arrayList.size();
}
}
ViewModel类

package com.codestrela.quizzer.viewmodel;

import android.util.Log;

import com.codestrela.quizzer.adapters.MatchingOptionAdapter;
import com.codestrela.quizzer.data.CallbackListener;
import com.codestrela.quizzer.fragment.DialogMatchingOptionFragment;
import com.codestrela.quizzer.fragment.MatchingQuestionFragment;

import java.util.ArrayList;

public class MatchingQuestionViewModel {
MatchingQuestionFragment matchingQuestionFragment;
DialogMatchingOptionFragment dialogMatchingOptionFragment;
    ArrayList<String> leftOptionList,rightOptionList;
    ArrayList<RowMatchingOptionViewModel> arrayList;
    public MatchingOptionAdapter matchAdapter;
    private static final String TAG = "MatchingQuestionViewMod";


    public MatchingQuestionViewModel(MatchingQuestionFragment matchingQuestionFragment, ArrayList<String> leftOptionList,ArrayList<String> rigthOptionList) {
        this.matchingQuestionFragment = matchingQuestionFragment;
        this.leftOptionList=leftOptionList;
        this.rightOptionList=rigthOptionList;
        arrayList=new ArrayList<>();
        matchAdapter=new MatchingOptionAdapter(new ArrayList<RowMatchingOptionViewModel>());
        for (int i = 0; i < leftOptionList.size(); i++) {
            RowMatchingOptionViewModel viewModel = new RowMatchingOptionViewModel();
            viewModel.leftOption.set(leftOptionList.get(i));
            viewModel.rightOption.set(rightOptionList.get(i));
            viewModel.optionNo.set("" + i + ". ");
               arrayList.add(viewModel);
          //  matchAdapter.add(viewModel);
        }
        matchAdapter.addAll(arrayList);
    }

   public void listUpdate(ArrayList<String> leftOptionList,ArrayList<String> rightOptionList,MatchingOptionAdapter adapter) {

       Log.e(TAG, "listUpdate: "+arrayList.size());
   //    adapter.addAll(arrayList);


    }
}
package com.codestrela.quizzer.viewmodel;
导入android.util.Log;
导入com.codestrela.quizzer.adapters.MatchingOptionAdapter;
导入com.codestrela.quizzer.data.CallbackListener;
导入com.codestrela.quizzer.fragment.DialogMatchingOptionFragment;
导入com.codestrela.quizzer.fragment.MatchingQuestionFragment;
导入java.util.ArrayList;
公共类匹配问题视图模型{
匹配问题片段匹配问题片段;
DialogMatchingOptionFragment DialogMatchingOptionFragment;
ArrayList leftOptionList,rightOptionList;
ArrayList ArrayList;
公共匹配选项适配器匹配适配器;
私有静态最终字符串标记=“MatchingQuestionViewMod”;
公共匹配问题视图模型(匹配问题片段匹配问题片段,ArrayList leftOptionList,ArrayList RightHostOptionList){
this.matchingQuestionFragment=matchingQuestionFragment;
this.leftOptionList=leftOptionList;
this.rightOptionList=rightOptionList;
arrayList=新的arrayList();
matchAdapter=new MatchingOptionaAdapter(new ArrayList());
对于(int i=0;i
XML文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="vm"
            type="com.codestrela.quizzer.viewmodel.MatchingQuestionViewModel" />
    </data>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/questionText"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:paddingLeft="8dp"
                android:layout_marginTop="12dp"
                android:hint="Enter your question"
                android:textColorHint="@color/colorPrimaryDark"
                android:textColor="#1B1A1A"
                android:background="@drawable/edittext_border"
                android:layout_margin="14dp" />
            <androidx.cardview.widget.CardView
                android:id="@+id/addAttachment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:cardCornerRadius="4dp"
                android:layout_marginTop="8dp"
                android:layout_gravity="center">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_margin="8dp">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_baseline_attach_file_24" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Add Attachment"
                        android:textSize="14dp"
                        android:textColor="@color/colorPrimaryDark" />
                </LinearLayout>


            </androidx.cardview.widget.CardView>
            <RelativeLayout
                android:id="@+id/attacment"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:visibility="gone"
                android:layout_marginTop="8dp"
                >
                <ImageView
                    android:id="@+id/imageAttached"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:visibility="invisible" />
                <LinearLayout
                    android:id="@+id/audioAttached"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_centerInParent="true"
                    android:visibility="invisible">

                    <SeekBar
                        android:id="@+id/audioSeekbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="8dp" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">

                        <Button
                            android:id="@+id/playAudio"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text="play" />
                        <!--                    <Button-->
                        <!--                        android:id="@+id/pauseAudio"-->
                        <!--                        android:layout_width="wrap_content"-->
                        <!--                        android:layout_height="wrap_content"-->
                        <!--                        android:layout_gravity="center"-->
                        <!--                        android:text="pause"-->
                        <!--                        />-->
                    </RelativeLayout>

                </LinearLayout>
                <androidx.cardview.widget.CardView
                    android:id="@+id/noAttachment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="8dp"
                    android:visibility="visible"
                    app:cardCornerRadius="4dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Added Attachments"
                        android:textSize="24dp"
                        android:layout_gravity="center" />


                </androidx.cardview.widget.CardView>

                <VideoView
                    android:id="@+id/videoView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="invisible"
                    android:layout_centerInParent="true"
                    android:layout_centerVertical="true" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:orientation="vertical">
                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    setRecyclerViewAdapter="@{vm.matchAdapter}"/>

                <Button
                    android:id="@+id/addMatchingOption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/ic_baseline_add_24"
                    android:text="Add Option"
                    app:strokeColor="@color/colorPrimaryDark"
                    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                    android:layout_margin="4dp"
                    android:layout_gravity="center" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    app:cardCornerRadius="8dp"
                    android:padding="8dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="No option selected"
                        android:visibility="invisible"
                        android:textSize="18sp"
                        android:layout_gravity="center" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textSize="24sp"
                        />
                </androidx.cardview.widget.CardView>
            </RelativeLayout>

            <Button
                android:id="@+id/showAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Show answer"
                android:layout_marginBottom="8dp" />

            <Button
                android:id="@+id/submitQuestion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="submit question" />
        </LinearLayout>
    </ScrollView>
</layout>

主要片段

import android.app.Dialog;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.codestrela.quizzer.R;
import com.codestrela.quizzer.adapters.MatchingOptionAdapter;
import com.codestrela.quizzer.base.activity.BaseActivity;
import com.codestrela.quizzer.data.CallbackListener;
import com.codestrela.quizzer.databinding.FragmentMatchingQuestionBinding;
import com.codestrela.quizzer.viewmodel.CreateQuestionViewmodel;
import com.codestrela.quizzer.viewmodel.MatchingQuestionViewModel;
import com.codestrela.quizzer.viewmodel.RowMatchingOptionViewModel;

import java.util.ArrayList;

public class MatchingQuestionFragment extends Fragment implements CallbackListener {
    FragmentMatchingQuestionBinding binding;
    MatchingQuestionViewModel vm;
    ArrayList<String> leftOptionList;
    FragmentManager fm;
    DialogMatchingOptionFragment dialogMatchingOptionFragment;
    ArrayList<String> rightOptionList;
    private static final String TAG = "MatchingQuestionFragmen";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getContext(), "joa", Toast.LENGTH_SHORT).show();
        dialogMatchingOptionFragment=new DialogMatchingOptionFragment(this);
        leftOptionList=new ArrayList<>();
        rightOptionList=new ArrayList<>();
        vm=new MatchingQuestionViewModel(this,rightOptionList,leftOptionList);

        fm=getActivity().getSupportFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_matching_question, container, false);
        binding.setVm(vm);
        Button addOption=(Button) binding.getRoot().findViewById(R.id.addMatchingOption);
        addOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog dialogFrg = dialogMatchingOptionFragment.getDialog();
                Bundle bundle = new Bundle();
                int optionName = rightOptionList.size() + 1;
                String optionN = String.valueOf(optionName);
                bundle.putString("optionName", optionN);
                if (dialogFrg != null && dialogFrg.isShowing()) {

                } else {
                    dialogMatchingOptionFragment.setArguments(bundle);
                    dialogMatchingOptionFragment.show(fm, "fma");
                }
            }
        });

        ((BaseActivity)getActivity()).setToolbarVisibility(true);
        return binding.getRoot();
    }
    public static void addFragment(BaseActivity activity, Fragment fragment) {
        activity.replaceFragment(fragment, true);
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getActivity(), "lsj", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onDismiss() {

        vm = new MatchingQuestionViewModel(this,leftOptionList, rightOptionList);

    }
}
导入android.app.Dialog;
导入android.os.Bundle;
导入androidx.databinding.DataBindingUtil;
导入androidx.fragment.app.fragment;
导入androidx.fragment.app.FragmentManager;
导入android.text.InputFilter;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.Toast;
导入com.codestrela.quizzer.R;
导入com.codestrela.quizzer.adapters.MatchingOptionAdapter;
导入com.codestrela.quizzer.base.activity.BaseActivity;
导入com.codestrela.quizzer.data.CallbackListener;
导入com.codestrela.quizzer.databinding.FragmentMatchingQuestionBinding;
导入com.codestrela.quizzer.viewmodel.CreateQuestionViewmodel;
导入com.codestrela.quizzer.viewmodel.MatchingQuestionViewModel;
导入com.codestrela.quizzer.viewmodel.rowmachingoptionviewmodel;
导入java.util.ArrayList;
公共类MatchingQuestionFragment扩展片段实现CallbackListener{
碎片匹配问题绑定;
匹配问题视图模型;
ArrayList leftOptionList;
碎片管理器fm;
DialogMatchingOptionFragment DialogMatchingOptionFragment;
ArrayList rightOptionList;
私有静态最终字符串TAG=“MatchingQuestionFragmen”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Toast.makeText(getContext(),“joa”,Toast.LENGTH_SHORT.show();
dialogMatchingOptionFragment=新的dialogMatchingOptionFragment(此);
leftOptionList=新的ArrayList();
rightOptionList=新建ArrayList();
vm=新匹配问题视图模型(此,右选项列表,左选项列表);