我根据这里的一篇文章Recyclerview简单实例写了个程序:https://blog.51cto.com/u_15382132/8154044 存在错误,麻烦高手帮我看看哪里出错,谢谢!以下是我根据上面文章写的源码:
https://files.cnblogs.com/files/blogs/782609/MyRecyclerview.zip?t=1723085071&download=true
大哥,你还没写完呢,文章中第六步没看到啊?还有第三步需要单独放到一个文件中
请问第六步放哪?
我已重新新增修改了相关代码,但还是有红线提示出错
@tea2007:
把下面三个文件都复制过去就行,你也有点太菜了。
Item.java
package com.example.myrecyclerview;
public class Item {
private String title;
private String description;
public Item(String title, String description) {
this.title = title;
this.description = description;
}
// Getter and setter methods
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
ItemAdapter.java
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> itemList;
public ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}
// 创建ViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
// 绑定数据到ViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Item item = itemList.get(position);
holder.titleTextView.setText(item.getTitle());
holder.descriptionTextView.setText(item.getDescription());
}
// 获取列表项数量
@Override
public int getItemCount() {
return itemList.size();
}
// ViewHolder定义
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView descriptionTextView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
descriptionTextView = itemView.findViewById(R.id.descriptionTextView);
}
}
}
MainActivity.java
package com.example.myrecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRecyclerView();
}
private void initRecyclerView() {
// 生成页面展示的数据
List<Item> itemList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Item item = new Item("标题:" + i, "描述文本 " + i);
itemList.add(item);
}
// 再绑定到RecyclerView上
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ItemAdapter adapter = new ItemAdapter(itemList);
recyclerView.setAdapter(adapter);
}
}
@shunlibest: 谢谢朋友,能否帮我看看这个:https://q.cnblogs.com/q/149059