gpt4 book ai didi

java - 我如何将两个字符串数组放入 ListView 中具有多个对象类型的数组列表中

转载 作者:行者123 更新时间:2023-12-01 09:44:03 24 4
gpt4 key购买 nike

给定一个定义了某些字段的 Java 对象,例如 User 类:

public class User {
public String Largetext;
public String Mediumtext;

public User(String Largetext, String Mediumtext) {
this.Largetext = Largetext;
this.Mediumtext = Mediumtext;
}

ListView 是:

String[] excercise1 = {"Wide-Grip Pull-Up", "Wide-Grip Pull-Down", "T-Bar Row", "Seated Cable Row", "Close Grip Row", "One Arm Dumble Row", "Dead Lift"};
String[] Detail = {"3 Set of 8-12 rep","4 set of 12-15 rep","3 set of 12,10,8 rep","3 set of 10-12 rep","4 set of 10-15 rep","3 set of 10-12 rep","4 set of 10,8,8,6"};
ArrayList<User> arraylist = new ArrayList<>();
final int[] imgs1 = {R.drawable.pullup_la, R.drawable.pulldown_la, R.drawable.tbar_la, R.drawable.seated_la, R.drawable.bend_la, R.drawable.onearm_la, R.drawable.dead_la};
ListAdapter saruadapter = new Backcoutomadapter(this, arraylist, imgs1);
ListView sarulistview = (ListView) findViewById(R.id.SarulistView);
sarulistview.setAdapter(saruadapter);

现在我如何将这两个字符串 excercise1 和 Detail 数组合并到这个数组列表中。所以我可以像这样在 Backcoutomadapter 中使用这个数组列表:

public class Backcoutomadapter extends ArrayAdapter<User> {
private int[] imgs1;
private String[] detail;

public Backcoutomadapter(Context context, ArrayList<User> excercise1, int[] imgs) {
super(context, backcustom_row, excercise1);
this.imgs1 = imgs;
}

提前致谢。

最佳答案

数组exercise1和Detail的项具有一对一的关系,即。 exercise1 位置 0 的项目与 Detail 位置 0 的项目相关,对吧?因此,您可以编写一个 for 循环,直到数组的长度,从数组中取出字符串,然后插入到数组列表中:

for(int i=0;i<exercise1.length();i++){
User user = new User(exercise1[i], Detail[i]);
arraylist.add(user);
}

然后将此数组列表传递给 Backcoutomadapter:

ListAdapter saruadapter = new Backcoutomadapter(this, arraylist, imgs1);

重写并实现 getView 方法以使用 ListView 中的字符串:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
................
}

关于java - 我如何将两个字符串数组放入 ListView 中具有多个对象类型的数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com