我正在基于recyclerview创建一个项目。我创建了一个Adapter类,其中是ViewHolder,我还创建了名为ListItems的Model类。在我尝试运行应用程序之后执行其他所有操作后,它会显示所有的cardView,但不会显示任何文本。这些是我所拥有的不同类和布局的代码。
这是我称为homeCardAdapter
的AdapterClasspublic class homeCardAdapter extends RecyclerView.Adapter<homeCardAdapter.ViewHolder> {
private List<ListItems> listItems;
private Context context;
public homeCardAdapter(List<ListItems> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_home, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItems listItem = listItems.get(position);
holder.cardHead.setText(listItem.getTitle());
holder.cardDesc.setText(listItem.getDesc());
holder.pay.setText(listItem.getPay());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView cardHead, cardDesc, pay;
public ViewHolder(View itemView) {
super(itemView);
cardHead = itemView.findViewById(R.id.companyName);
cardDesc = itemView.findViewById(R.id.cardDescription);
pay = itemView.findViewById(R.id.pay);
}
}
}
这是我的Model类的代码,名为ListItems
public class ListItems {
String title, description, pay;
public ListItems(String title, String description, String pay) {
}
public String getTitle() {
return title;
}
public String getPay() {
return pay;
}
public String getDesc() {
return description;
}
}
这是我将其命名为Welcome
的Java Activitypublic class Welcome extends AppCompatActivity {
private static final String TAG = "Welcome";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItems> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Log.d(TAG, "onCreate: starting");
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
for (int i = 0 ; i < 10; i++){
ListItems listItem = new ListItems(
"Port Organization" + (i+1),
"Lorem Ipsum The Description Will Go over Here So As for case" + (i),
"250" + (i) + "/ Week"
);
listItems.add(listItem);
}
adapter = new homeCardAdapter(listItems,this);
recyclerView.setAdapter(adapter);
}
}
这是我在RecyclerView中使用的CardView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:text="$450 / Week"
android:textColor="#FFF"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:textSize="18sp"/>
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Company Name"
android:layout_below="@id/pay"
android:textSize="22sp"
android:textColor="#000"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
/>
<TextView
android:id="@+id/cardDescription"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:text="Lorem Ipsum Some Text Goes Over Here"
android:layout_below="@id/companyName"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
请建议我编辑,以便我可以将文本存入我的卡片。
答案 0 :(得分:1)
您没有在构造函数中初始化值,像这样的构造函数使用
public ListItems(String title, String description, String pay) {
this.title = title;
this.description= description;
this.pay= pay;
}