我的recycleview无法正常运作。你能帮我吗?

时间:2019-08-20 14:10:30

标签: java android android-recyclerview

我真的需要一些帮助。我是android studio的新手。我在使用recyclerview时遇到问题,它不会显示某些物品,但是没有错误。

适配器:

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ClassViewHolder> {
       private ArrayList<ExampleClass> qClassList;

       public static class ClassViewHolder extends RecyclerView.ViewHolder {
       public TextView code,name,year;
       public ClassViewHolder(View view){
              super(view);
              code = view.findViewById(R.id.code);
              name = view.findViewById(R.id.name);
              year = view.findViewById(R.id.year);

              }
       }
       public ClassAdapter(ArrayList<ExampleClass> classList){
       qClassList = classList;

       }

       @NonNull
       @Override
       public ClassViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
              View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_classes, viewGroup, false);
              ClassViewHolder cvh = new ClassViewHolder(v);
              return cvh;
       }

       @Override
       public void onBindViewHolder(ClassViewHolder classViewHolder, int i) 
       {
               ExampleClass exampleClass = qClassList.get(i);
               classViewHolder.code.setText(exampleClass.getqCode());
               classViewHolder.name.setText(exampleClass.getqName());
               classViewHolder.year.setText(exampleClass.getqYear());
               }

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

ClassActivity:

     public class ClassActivity extends AppCompatActivity {
     private ArrayList<ExampleClass> exampleClassArrayList;

     private RecyclerView recyclerView;
     private RecyclerView.Adapter adapter;
     private RecyclerView.LayoutManager layoutManager;

     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_classes);
         ArrayList<ExampleClass> exampleClasses = new ArrayList<>();

         exampleClasses.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));

         createClassList();
         buildRecycleView();

         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
         fab.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 // Click action
                 Intent intent = new Intent(ClassActivity.this, CreateClassActivity.class);
                 startActivity(intent);
             }
         });
     }


     public void createClassList(){
         exampleClassArrayList = new ArrayList<>();

     }

     public void buildRecycleView(){
         recyclerView = findViewById(R.id.recyclerView);
         layoutManager = new LinearLayoutManager(this);
         adapter = new ClassAdapter(exampleClassArrayList);
         recyclerView.setLayoutManager(layoutManager);
         recyclerView.setAdapter(adapter);
    }
}

activity_class.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">

     <android.support.v7.widget.RecyclerView
         android:id="@+id/recyclerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@android:color/darker_gray"
         android:padding="4dp"
         android:scrollbars="vertical"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.0"
         app:layout_constraintStart_toStartOf="parent"
         tools:layout_editor_absoluteY="0dp" />

     <android.support.design.widget.FloatingActionButton
         android:id="@+id/fab"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/recyclerView"
         android:layout_alignParentEnd="true"
         android:layout_marginEnd="27dp"
         android:layout_marginBottom="32dp"
         android:src="@android:drawable/ic_input_add" />

 </RelativeLayout>

我已经搜索了一些类似于我的问题,但没有成功。我想知道是否有人可以告诉我我在这里犯了什么错误。我真的很感激。很抱歉给您带来不便。

2 个答案:

答案 0 :(得分:0)

在createClassList()方法中,从列表中创建新实例,同时在onCreate列表中添加新项,删除调用createClassList()正常工作

答案 1 :(得分:0)

选择以下代码行:

 ArrayList<ExampleClass> exampleClasses = new ArrayList<>(); // remove this line No need
          createClassList(); //call this method
         exampleClassArrayList.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));

此通话后

buildRecycleView()

您要添加其他列表(exampleClasses),并使用不同的适配器列表(exampleClassArrayList)。