我怎样才能在listview

时间:2016-07-05 20:44:40

标签: java android listview arraylist

给定一个定义了某些字段的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数组合并到这个arraylist。所以我可以在Backcoutomadapter这个arraylist这样:

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;
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

数组练习1&amp;细节有一对一的关系,即。 exercise1的0位置的项目与Detail的位置0的项目相关,对吗?所以,你可以写一个for循环直到数组的长度,从数组中获取字符串,&amp;插入数组列表:

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

然后将这个arraylist传递给Backcoutomadapter:

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

覆盖&amp;实现getView方法以使用ListView中的字符串:

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