将两个arraylists添加到自定义布局?

时间:2018-03-28 11:51:08

标签: android listview arraylist adapter

What I want it to look like 我有两个arraylists,我在res文件夹中创建了一个自定义布局,其中两个TextViews并排。我希望能够使用两个customlayout填充此arraylists,以便第一个列表中的第一个项目将显示在第二个列表中的第一个项目旁边。任何帮助将不胜感激。

列表视图中的两个Arraylists:

ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.getKey();
                allStudentsList.add(name);
                attendancePercentage.add("74%");
            }
            final ListView listView = findViewById(R.id.studentList);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.percentage_layout, R.id.statsResult, allStudentsList);
            listView.setAdapter(adapter);

自定义布局的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/classList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_weight="3"
    android:text="TextView" />

<TextView
    android:id="@+id/statsResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_weight="3"
    android:text="TextView" />
 </LinearLayout>

2 个答案:

答案 0 :(得分:0)

您必须向yourAdapter提供两个列表

使用构造函数或setterMethod

 public YourAdapter(List<Name> nameList,List<Percent> nameList, Context context) {
    this.nameList = nameList;
    this.nameList = nameList;
    this.context = context;
}

然后 修改你的onBindMethod

  @Override
public void onBindViewHolder(YourHolder holder, int position) {
    holder.tvName.setText(listName.getName());
    holder.tvPercent.setText(listPercent.getPercent());
}

答案 1 :(得分:0)

创建一个这样的模型类:

public class Student {

String name;
String percentage;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPercentage() {
    return percentage;
}

public void setPercentage(String percentage) {
    this.percentage = percentage;
}

}

并调用适配器,如:

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),R.layout.percentage_layout,ListallStudentsList);  listView.setAdapter(适配器);

相关问题