【问题标题】:Adding two arraylists to a custom layout?将两个数组列表添加到自定义布局?
【发布时间】:2018-03-28 11:51:08
【问题描述】:

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

带有 Listview 的两个 Arraylist:

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>

【问题讨论】:

  • 显示一些代码。
  • 数组的行数是否相同?行的大小是否相同?试着画出你想要实现的线框。
  • 是的,它们的行数相同,因此第一个数组列表将是一个类列表。第二个列表将是该特定学生的出勤记录。在上面的代码中,我刚刚硬编码每个学生旁边都有 74%
  • 添加了我想要的样子
  • 请使用自定义适配器。

标签: android listview arraylist adapter


【解决方案1】:

您必须将这两个列表都提供给您的适配器

使用构造函数或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());
}

【讨论】:

    【解决方案2】:

    像这样创建一个模型类:

    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 适配器 = new ArrayAdapter(getApplicationContext(),R.layout.percentage_layout,ListallStudentsList); listView.setAdapter(适配器);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多