作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一个定义了某些字段的 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/
我是一名优秀的程序员,十分优秀!