我正试图用适配器来膨胀我的recyclerview活动

kkbh8khc  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(261)

构建或在logcat中不会出现错误,但当应用程序运行时,不会显示任何内容。
以下是我的活动xml:

<LinearLayout
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"
android:orientation="vertical"
tools:context=".ui.courses.EngActivity"
>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/recyclerview"
    >

</androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

我的布局要膨胀了

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <ImageView
        android:id="@+id/college_icon"
        android:layout_width="165dp"
        android:layout_height="125dp"
        android:scaleType="centerCrop"
        android:src="@drawable/iitb" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:layout_toRightOf="@+id/college_icon"
        android:layout_toEndOf="@+id/college_icon"
        android:text="Indian Institute of Technology Bombay-IITB"
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold" />

</RelativeLayout>

</androidx.cardview.widget.CardView>

主要类别:

public class EngActivity extends AppCompatActivity {

RecyclerView recyclerView;
MyAdapter adapter;

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

    recyclerView=findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter= new MyAdapter(this,getMyList());
    recyclerView.setAdapter(adapter);
}

private ArrayList<Model> getMyList(){
    ArrayList<Model> models= new ArrayList<>();
    Model m;
    m = new Model();
    m.setTitle("Indian Institute of Technology Bombay-IITB");
    m.setDescription("Mumbai, Maharashtra");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹211,400 per year");
    m.setImg(R.drawable.iitb);

    m=new Model();
    m.setTitle("Indian Institute of Technology Delhi-IITD");
    m.setDescription("New Delhi, Delhi NCR");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹224,900 per year");
    m.setImg(R.drawable.iitd);

    m=new Model();
    m.setTitle("Birla Institute of Technology and Science-BITS");
    m.setDescription("Pilani, Rajasthan");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹423,475 per year");
    m.setImg(R.drawable.bits);

    m=new Model();
    m.setTitle("Bennett University");
    m.setDescription("Greater Noida, Uttar Pradesh");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹425,000 per year");
    m.setImg(R.drawable.bu);

    return models;
}
}

用于在回收器视图中添加项的适配器类:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

Context c;
ArrayList<Model> models;

public MyAdapter(Context c, ArrayList<Model> models) {
    this.c = c;
    this.models = models;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row,null);
    return new MyHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {

    holder.mTitle.setText(models.get(position).getTitle());
    holder.mDes.setText(models.get(position).getDescription());
    holder.mCourse.setText(models.get(position).getCourse());
    holder.mFee.setText(models.get(position).getFees());
    holder.mImageView.setImageResource(models.get(position).getImg());
}

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

在engactivity java类中膨胀之后,它只是在构建中显示空活动,如何解决这个问题?

dtcbnfnu

dtcbnfnu1#

在活动类的getmylist()方法中,模型对象不会添加到列表中,从而生成一个大小为零的列表。将模型添加到返回列表,并通知适配器。

private ArrayList<Model> getMyList(){
    ArrayList<Model> models= new ArrayList<>();
    Model m;
    m = new Model();
    m.setTitle("Indian Institute of Technology Bombay-IITB");
    m.setDescription("Mumbai, Maharashtra");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹211,400 per year");
    m.setImg(R.drawable.iitb);
    models.add(m);

    m=new Model();
    m.setTitle("Indian Institute of Technology Delhi-IITD");
    m.setDescription("New Delhi, Delhi NCR");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹224,900 per year");
    m.setImg(R.drawable.iitd);
    models.add(m);

    m=new Model();
    m.setTitle("Birla Institute of Technology and Science-BITS");
    m.setDescription("Pilani, Rajasthan");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹423,475 per year");
    m.setImg(R.drawable.bits);

    m=new Model();
    m.setTitle("Bennett University");
    m.setDescription("Greater Noida, Uttar Pradesh");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹425,000 per year");
    m.setImg(R.drawable.bu);
    models.add(m);

    return models;
  }
pcrecxhr

pcrecxhr2#

你忘了包括arraylist add 方法。
add(object o):此方法将指定的元素追加到此列表的末尾。

ArrayList<Model> models= new ArrayList<>();
    Model m;
    m = new Model();
    m.setTitle("Indian Institute of Technology Bombay-IITB");
    m.setDescription("Mumbai, Maharashtra");
    m.setCourse("BE/B.Tech - Bachelor of Technology");
    m.setFees("₹211,400 per year");
    m.setImg(R.drawable.iitb);

     models.add(new Model(m));

   m=new Model();
   m.setTitle("Indian Institute of Technology Delhi-IITD");
   m.setDescription("New Delhi, Delhi NCR");
   m.setCourse("BE/B.Tech - Bachelor of Technology");
   m.setFees("₹224,900 per year");
   m.setImg(R.drawable.iitd);

    models.add(new Model(m));

相关问题