android - 从 RecyclerView 检索数据
问题描述
我RecyclerView
在应用程序中使用自定义适配器。我想处理现有的菜单RecyclerView
项。像普通变量一样访问它们,以便我可以在我的类适配器之外应用特定条件或在文本消息中发送它们等等。
如何访问RecyclerView
项目?
代码类矩阵:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
addItem();
Button button = findViewById(R.id.get_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
I want when I press the button
The items are called
*/
}
});
}
public void addItem() {
List<Shops> shops = new ArrayList<>();
String[] cloth = {"Silk", "Cotton", "Linen"};
String[] wood = {"Sandal", "Jawi", "Pine"};
String[] metal = {"Window", "door", "roof"};
RecyclerView recyclerView = findViewById(R.id.list_shop);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < cloth.length; i++) {
Shops shopsList = new Shops(cloth[i], wood[i], metal[i]);
shops.add(shopsList);
}
ShopsAdapter adapter = new ShopsAdapter(shops);
recyclerView.setAdapter(adapter);
}
}
代码类列表
public class Shops {
String cloth, wood, metal;
public Shops(String cloth, String wood, String metal) {
this.cloth = cloth;
this.wood = wood;
this.metal = metal;
}
}
代码类适配器
public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.ShopsHolder> {
private List<Shops> shopsList;
public ShopsAdapter(List<Shops> shops) {
this.shopsList = shops;
}
@Override
public ShopsHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.template_recycler_view, viewGroup, false);
ShopsHolder holder = new ShopsHolder(row);
return holder;
}
@Override
public void onBindViewHolder(ShopsHolder holder, int i) {
Shops shops = shopsList.get(i);
holder.cloth.setText(shops.cloth);
holder.wood.setText(shops.wood);
holder.metal.setText(shops.metal);
}
@Override
public int getItemCount() {
return shopsList.size();
}
class ShopsHolder extends RecyclerView.ViewHolder {
private TextView cloth, wood, metal;
public ShopsHolder(View itemView) {
super(itemView);
cloth = itemView.findViewById(R.id.cloth);
wood = itemView.findViewById(R.id.wood);
metal = itemView.findViewById(R.id.metal);
}
}
}
如果您需要 XML 代码,我会在另一个回复中提供。
解决方案
像这样使用 getters() 和 setters () 创建模型/ POJO 类
public class Shops {
private String cloth;
private String wood;
private String metal;
public Shops(String cloth, String wood, String metal) {
this.cloth = cloth;
this.wood = wood;
this.metal = metal;
}
public String getCloth() {
return cloth;
}
public void setCloth(String cloth) {
this.cloth = cloth;
}
public String getWood() {
return wood;
}
public void setWood(String wood) {
this.wood = wood;
}
public String getMetal() {
return metal;
}
public void setMetal(String metal) {
this.metal = metal;
}
}
现在全局声明 ArrayList 并向其中添加项目。当您单击按钮时,使用索引号简单地访问它们。您可以为此运行一个 for 循环。
public class MainActivity extends AppCompatActivity {
private List<Shops> shops;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
shops= new ArrayList<>();
addItem();
Button button = findViewById(R.id.get_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
I want when I press the button
The items are called
*/
shops.get(1).getCloth();
shops.get(1).getWood();
shops.get(1).getMetal();
// do whatever you want with them
}
});
}
public void addItem() {
String[] cloth = {"Silk", "Cotton", "Linen"};
String[] wood = {"Sandal", "Jawi", "Pine"};
String[] metal = {"Window", "door", "roof"};
RecyclerView recyclerView = findViewById(R.id.list_shop);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < cloth.length; i++) {
Shops shopsList = new Shops(cloth[i], wood[i], metal[i]);
shops.add(shopsList);
}
推荐阅读
- javascript - 不保存到本地存储
- python - 我发现冒号的无意义用法:在 python 中,它似乎没有做任何事情,但它不返回语法错误
- python - 传递一个 Jenkins 变量来表现命令行参数
- python - 如何在 Pandas 中设置合并金额的误差范围
- ios - 在 iPhone X 中隐藏标签栏并将视图底部扩展至底部边缘
- asp.net - 无法将模型从 ASP.NET MVC 中的视图传递回控制器。收到时模型为空
- r - 我的闪亮应用程序无法在闪亮的 io 服务器上正确部署
- dynamic-programming - 我如何记住我的回溯算法并使用动态编程?
- javascript - SyntaxError 不断出现
- bixby - 当话语显示为未学习时,如何在新 UI 中强制学习?