Android studio RecycleView适配器和主代码因未知错误而无法正常工作,您能发现差异吗
大家好,我是一个新的使用android studio的人,我正在做我的学校项目。我需要使用RecycleVie魔杖,我试过制作,但没有成功。 我使用一个对象类缩放任务,它有3个属性要显示在布局上,但我不知道我的错误在哪里。显示为问题的行以粗体显示。如果有人能帮助我,我会很高兴的强> 我的对象类:Android studio RecycleView适配器和主代码因未知错误而无法正常工作,您能发现差异吗,android-studio,android-recyclerview,Android Studio,Android Recyclerview,大家好,我是一个新的使用android studio的人,我正在做我的学校项目。我需要使用RecycleVie魔杖,我试过制作,但没有成功。 我使用一个对象类缩放任务,它有3个属性要显示在布局上,但我不知道我的错误在哪里。显示为问题的行以粗体显示。如果有人能帮助我,我会很高兴的 我的对象类: public class Task { private String material; private String day; private String month; public Task (St
public class Task {
private String material;
private String day;
private String month;
public Task (String material,String day,String month)
{
this.material = material;
this.day = day;
this.month = month;
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
}
适配器代码:
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {
private Context mCtx;
private List<Task> tList;
// data is passed into the constructor
public HomeRecyclerViewAdapter(Context mCtx, List<Task> tList) {
this.mCtx = mCtx;
this.tList = tList;
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvText, tvDateDay, tvDateMonth;
public ViewHolder(View itemView) {
super(itemView);
tvText = itemView.findViewById(R.id.tvText);
tvDateDay = itemView.findViewById(R.id.tvDateDay);
tvDateMonth = itemView.findViewById(R.id.tvDateMonth);
}
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.home_recyclerview_row, null);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Task task = tList.get(position);
**holder.tvText.setText(task.getMaterial());**
}
// allows clicks events to be caught
@Override
public int getItemCount() {
return tList.size();
}
公共类HomeRecycleServiceAdapter扩展了RecycleView.Adapter{
私有上下文mCtx;
私人名单;
//数据被传递到构造函数中
公共HomeRecycleServiceAdapter(上下文mCtx,列表tList){
this.mCtx=mCtx;
this.tList=tList;
}
//在屏幕外滚动视图时存储和回收视图
公共类ViewHolder扩展了RecyclerView.ViewHolder{
text查看tvText、tvDateDay、tvDateMonth;
公共视图持有者(视图项视图){
超级(项目视图);
tvText=itemView.findViewById(R.id.tvText);
tvDateDay=itemView.findviewbyd(R.id.tvDateDay);
tvDateMonth=itemView.findviewbyd(R.id.tvDateMonth);
}
}
//根据需要从xml扩展行布局
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
//膨胀并返回我们的视图持有者
LayoutFlater充气机=LayoutFlater.from(mCtx);
视图=充气机。充气(R.layout.home\u recyclerview\u row,空);
返回新的ViewHolder(视图);
}
//将数据绑定到每行的TextView
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
Task Task=tList.get(位置);
**holder.tvText.setText(task.getMaterial())**
}
//允许捕获单击事件
@凌驾
public int getItemCount(){
返回tList.size();
}
}
以及主要代码:
public class HomeScreen_activity extends AppCompatActivity implements View.OnClickListener {
List<Task> tList;
RecyclerView homercy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home_screen_layout);
homercy = (RecyclerView) findViewById(R.id.homercy);
homercy.setHasFixedSize(true);
homercy.setLayoutManager(new LinearLayoutManager(this));
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.homercy);
tList = new ArrayList<Task>();
Task t1 = new Task("test","12","05");
tList.add(t1);
**HomeRecyclerViewAdapter adapter = new HomeRecyclerViewAdapter(this,tList);**
recyclerView.setAdapter(adapter);
}
公共类主屏幕\u活动扩展了AppCompatActivity实现了View.OnClickListener{
名单;
回收视图homercy;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(右布局、主屏幕布局);
homercy=(RecyclerView)findViewById(R.id.homercy);
homercy.setHasFixedSize(true);
homercy.setLayoutManager(新的LinearLayoutManager(本));
//设置RecyclerView
RecyclerView RecyclerView=findViewById(R.id.homercy);
tList=newarraylist();
任务t1=新任务(“测试”、“12”、“05”);
tList.add(t1);
**HomeRecycleServiceAdapter=新的HomeRecycleServiceAdapter(此,tList)**
recyclerView.setAdapter(适配器);
}
也许在onCreateViewHolder()中,您必须这样做:
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recyclerview_row, parent, false);
return new ViewHolder(view);
}
请定义“不工作”的含义。是否收到错误?行中有错误:“HomeRecycleServiceAdapter=new HomeRecycleServiceAdapter(this,tList);”(来自主代码)和-“holder.tvText.setText(task.getMaterial());”(来自适配器代码)行中有错误:“HomeRecycleServiceAdapter=new HomeRecycleServiceAdapter(this,tList);”(来自主代码)和“-holder.tvText.setText(task.getMaterial());”(来自适配器代码)-我不知道为什么。。。