Java 我的项目需要在日记账分录回收器中显示卡片,但失败了
我的项目需要显示日记账分录的卡片,我已经制作了适当的适配器和类,但是当需要时,分录列表无法显示在回收器中 入口适配器类Java 我的项目需要在日记账分录回收器中显示卡片,但失败了,java,android,Java,Android,我的项目需要显示日记账分录的卡片,我已经制作了适当的适配器和类,但是当需要时,分录列表无法显示在回收器中 入口适配器类 public class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.EntryViewHolder> { private View.OnClickListener onClickListener; public void add(Entry c) { entries.add(c);
public class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.EntryViewHolder> {
private View.OnClickListener onClickListener;
public void add(Entry c) {
entries.add(c);
notifyItemChanged(entries.size() - 1);
}
public static class EntryViewHolder extends RecyclerView.ViewHolder {
public TextView edtDate;
public TextView tEntry;
public ImageView imgAvatar;
public Entry entry;
public CardView cardView;
public EntryViewHolder(@NonNull View view) {
super(view);
edtDate = view.findViewById(R.id.edtDate);
tEntry = view.findViewById(R.id.txtEntry);
imgAvatar = view.findViewById(R.id.imgView);
}
public void setEntry(Entry entry)
{
this.entry = entry;
edtDate.setText(entry.dob);
tEntry.setText(entry.getsEntry());
switch (entry.getType()) {
case 0 : { cardView.setCardBackgroundColor(0);}
break;
case 1 : { cardView.setCardBackgroundColor(90);}
break;
case 2 : { cardView.setCardBackgroundColor(180);}
break;
case 3 : { cardView.setCardBackgroundColor(270);}
break;
}
}
}
private final List<Entry> entries;
public EntryAdapter(List<Entry> entries, View.OnClickListener onClickListener) {
this.entries = entries;
this.onClickListener = onClickListener;
}
public EntryAdapter(List<Entry> entries) {
this.entries = entries;
}
@NonNull
@Override
public EntryAdapter.EntryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.activity_entry_view,
parent, false);
EntryViewHolder evh = new EntryViewHolder(view);
return evh;
}
@Override
public void onBindViewHolder(@NonNull EntryAdapter.EntryViewHolder holder, int position) {
Entry entry = entries.get(position);
holder.setEntry(entry);
holder.itemView.setOnClickListener(onClickListener);
}
公共类EntryAdapter扩展了RecyclerView.Adapter{
private View.OnClickListener OnClickListener;
公共作废添加(条目c){
条目。添加(c);
notifyItemChanged(entries.size()-1);
}
公共静态类EntryViewHolder扩展了RecyclerView.ViewHolder{
公共文本查看日期;
公共文本视图;
公共图像视图imgAvatar;
公众进入;
公共卡视图卡视图;
公共入口视图持有者(@NonNull视图){
超级(视图);
edtDate=view.findViewById(R.id.edtDate);
tEntry=view.findviewbyd(R.id.txentry);
imgAvatar=view.findViewById(R.id.imgView);
}
公共作废设置条目(条目)
{
this.entry=entry;
edtDate.setText(entry.dob);
tEntry.setText(entry.getsEntry());
开关(entry.getType()){
案例0:{cardView.setCardBackgroundColor(0);}
打破
案例1:{cardView.setCardBackgroundColor(90);}
打破
案例2:{cardView.setCardBackgroundColor(180);}
打破
案例3:{cardView.setCardBackgroundColor(270);}
打破
}
}
}
私人最终名单条目;
公共入口适配器(列表项,View.OnClickListener OnClickListener){
this.entries=条目;
this.onClickListener=onClickListener;
}
公共入口适配器(列表项){
this.entries=条目;
}
@非空
@凌驾
public EntryAdapter.EntryViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图=平面布局器
.from(parent.getContext())
.充气(R.布局.活动\入口\视图,
父母,假);
EntryViewHolder evh=新的EntryViewHolder(视图);
返回evh;
}
@凌驾
public void onBindViewHolder(@NonNull EntryAdapter.EntryViewHolder,int位置){
Entry=entries.get(位置);
持有者。设置条目(条目);
holder.itemView.setOnClickListener(onClickListener);
}
主要活动课
private EntryAdapter adapter;
private List<Entry> entries = new ArrayList<>();;
Pet pet = Pet.getPet();
public static final int Add_Entry = 1;
public static final int Edit_Entry = 2;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
File mFolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Entry gumbo = new Entry("21 of Jan","A gumbo",3);
entries.add(gumbo);
//adapter.add(gumbo);
entries = populate();
adapter = new EntryAdapter(entries);
RecyclerView.LayoutManager layoutManager;
//layoutManager = new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL);
layoutManager = new LinearLayoutManager(getApplicationContext());
//start.setLayoutManager(layoutManager);
//start.setAdapter(adapter);
RecyclerView lstEntries = findViewById(R.id.recEntries);
lstEntries.setLayoutManager(layoutManager);
lstEntries.setAdapter(adapter);
System.out.print(adapter);
//Pet activity button
FloatingActionButton petbtn = (FloatingActionButton)findViewById(R.id.fabPets);
lstEntries.addItemDecoration(new EqualSpaceItemDecorator(15));
petbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PetDetails.class));
}
});
}
专用入口适配器;
私有列表项=新的ArrayList();;
Pet=Pet.getPet();
公共静态最终整数添加项=1;
公共静态最终整数编辑_条目=2;
专用静态最终整数请求\外部\存储=1;
私有静态字符串[]权限\存储={
Manifest.permission.READ_外部存储,
Manifest.permission.WRITE\u外部存储
};
文件文件夹;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
条目秋葵=新条目(“1月21日”,“秋葵”,3);
添加(秋葵);
//添加(秋葵);
条目=填充();
适配器=新入口适配器(条目);
RecyclerView.LayoutManager LayoutManager;
//layoutManager=新的StaggedGridLayoutManager(1,LinearLayoutManager.VERTICAL);
layoutManager=新的LinearLayoutManager(getApplicationContext());
//start.setLayoutManager(layoutManager);
//setAdapter(适配器);
RecyclerView lstEntries=findViewById(R.id.recEntries);
lstEntries.setLayoutManager(layoutManager);
lstEntries.setAdapter(适配器);
系统输出打印(适配器);
//宠物活动按钮
FloatingActionButton petbtn=(FloatingActionButton)findViewById(R.id.fabPets);
lstEntries.addItemDecoration(新EqualSpaceItemDecorator(15));
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(MainActivity.this、PetDetails.class));
}
});
}
我试了又试,但这个该死的东西不会显示。entry类接受2个字符串变量和一个int变量
这是卡片xml
<androidx.cardview.widget.CardView 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="wrap_content"
app:cardBackgroundColor="#2196F3"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/edtDate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.322"
app:srcCompat="@drawable/appointment" />
<EditText
android:id="@+id/edtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgView" />
<TextView
android:id="@+id/txtEntry"
android:layout_width="208dp"
android:layout_height="29dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtDate"
app:layout_constraintVertical_bias="0.06" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>