从 RecyclerView 检索数据
Retrieve data from RecyclerView
我在使用自定义适配器的应用程序中使用 RecyclerView
。
我想处理现有菜单 RecyclerView
项。像普通变量一样访问它们,以便我可以应用特定条件或将它们发送到我的 Class 适配器之外的文本消息中等等。
如何访问 RecyclerView
项?
代码class矩阵:
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);
}
}
代码Class列表
public class Shops {
String cloth, wood, metal;
public Shops(String cloth, String wood, String metal) {
this.cloth = cloth;
this.wood = wood;
this.metal = metal;
}
}
代码Class适配器
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代码,我会在另一个回复里放。
谢谢。
经过反复实验。
我能够得出一个结果。
它正在使用 For loob
在你里面 addItem
所以
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);
}
ShopsAdapter adapter = new ShopsAdapter(shops);
recyclerView.setAdapter(adapter);
// From here begins the solution code
for (int i = 0; i < cloth.length; i++) {
String clothMy = shops.get(i).cloth;
String woodMy = shops.get(i).wood;
String metalMy = shops.get(i).metal;
Log.i("TAG",clothMy);
Log.i("TAG",woodMy);
Log.i("TAG",metalMy);
}
// Here ends the solution code
}
像这样使用 getters() 和 setters() 创建模型/POJO class
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);
}
您的代码方式似乎可以满足您的需要,但我认为这不是一个很好的方式。与其在项目的某个位置创建一个 public 列表来缓存你想要的数据,不如让这个列表在你的适配器中私有,并通过添加一个 getItem(int pos) 通过适配器获取你想要的数据return item 通过你传递给它的 'pos' 参数,这样更安全,更专业。解决问题的方法有很多种,但哪种方法最好...
我在使用自定义适配器的应用程序中使用 RecyclerView
。
我想处理现有菜单 RecyclerView
项。像普通变量一样访问它们,以便我可以应用特定条件或将它们发送到我的 Class 适配器之外的文本消息中等等。
如何访问 RecyclerView
项?
代码class矩阵:
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);
}
}
代码Class列表
public class Shops {
String cloth, wood, metal;
public Shops(String cloth, String wood, String metal) {
this.cloth = cloth;
this.wood = wood;
this.metal = metal;
}
}
代码Class适配器
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代码,我会在另一个回复里放。
谢谢。 经过反复实验。 我能够得出一个结果。 它正在使用 For loob 在你里面 addItem 所以
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);
}
ShopsAdapter adapter = new ShopsAdapter(shops);
recyclerView.setAdapter(adapter);
// From here begins the solution code
for (int i = 0; i < cloth.length; i++) {
String clothMy = shops.get(i).cloth;
String woodMy = shops.get(i).wood;
String metalMy = shops.get(i).metal;
Log.i("TAG",clothMy);
Log.i("TAG",woodMy);
Log.i("TAG",metalMy);
}
// Here ends the solution code
}
像这样使用 getters() 和 setters() 创建模型/POJO class
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);
}
您的代码方式似乎可以满足您的需要,但我认为这不是一个很好的方式。与其在项目的某个位置创建一个 public 列表来缓存你想要的数据,不如让这个列表在你的适配器中私有,并通过添加一个 getItem(int pos) 通过适配器获取你想要的数据return item 通过你传递给它的 'pos' 参数,这样更安全,更专业。解决问题的方法有很多种,但哪种方法最好...