java - 自定义三项ListView适配器

标签 java android xml listview

我有一个 ListView 布局,每个项目有三个值 (grade_list.xml),但我无法让我的适配器设置第三个值,因为 HashMap 仅采用两个字符串。我该如何解决这个问题?

grade_list.xml

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<!-- First Line - Header Text -->
<TextView
    android:id="@android:id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:layout_marginTop="8dp"
    android:textStyle="bold"/>
<!-- Second Line - Description -->
<TextView
    android:id="@android:id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/text1"
    android:layout_alignStart="@android:id/text1"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail" />
<!-- Third Line - Grade -->
<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:gravity="right"
    android:layout_marginTop="8dp"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/></TwoLineListItem>

我的服务器响应如下内容

Period 1 - Language Arts;Period 2 - Biology;Period 3 - Engineering~~DIVIDER~~A+;B-;A

格式为

Class;Class;Class~~DIVIDER~~Grade;Grade;Grade

然后客户端使用此代码将该数据放入 ListView

final String[] ServerResponse = input.split("~~DIVIDER~~");
final String[] Classes = ServerResponse[0].split(";");
final String[] Grades = ServerResponse[1].split(";");

ArrayList<HashMap<String, String>> item_list = new ArrayList<HashMap<String, String>>();

for (int i = 0; i < Classes.length; i++) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("Class", Classes[i]);
    item.put("Grade", Grades[i]);

    item_list.add(item);
}

String[] list_input = new String[] { "Class", "Grade" };
// PUT THE STRING VALUES FROM list_input INTO THE XML LAYOUT VALUES
int[] layout_values = new int[] { android.R.id.text1, android.R.id.text2};

// LISTVIEW LAYOUT (XML FILE)
int list_layout = grade_list;

ListView gradeListView = (ListView) findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));

我想拆分

Period 1 - Class Name 

对于两个单独的值,句点将填充第一行,类名将填充第二行,由 - 字符分隔。但是,HashMap 只能保存两个值,而不是我想要的三个。

Android 文档对此没有提供太多帮助,Google 也没有。提前致谢。

最佳答案

最好记住 HashMap 将数据链接到行 ID(两个项目)。为每个列表行创建一个 HashMap。 SimpleAdapter 执行与从每个 HashMap 加载 ListView 的代码相同的迭代。

它会是这样的:

//Setting the HashMaps

HashMap<String,String> item;
for(int i=0;i<Classes.length;i++){
    item = new HashMap<String,String>();
    item.put( "text1", Period[i]);
    item.put( "text2", Classes[i]);
    item.put( "text3", Grades[i]);
    list.add( item );
}

//...

//Setting the adapter
gradeListView.setAdapter(new new SimpleAdapter(this, item_list, R.layout.TwoLineListItem, new String[] { "text1","text2", "text3" }, new int[] {R.id.text1, R.id.text, R.id.text3}));

尽管您确实应该将 TwoLineListItem 重命名为 ThreeLineListItem 以避免将来出现混淆。

关于java - 自定义三项ListView适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711065/

相关文章:

java - 有没有办法在不使用 TimePickerDialog 的情况下获取 TimePicker 的时间?

java - Java中如何根据屏幕大小改变窗口大小?

android - 在 dao room 数据库中创建一个带有 body 的自定义函数

android - 在 android 中的相机中捕获图像后的全屏预览

xml - Camel xml 到 xml 转换 xslt 仍然是可行的方法吗?

xml - 试图在 "Graphical Layout"选项卡中查看我的 XML

c# - 只有一个对象时将 XML 转换为 Json 数组

java - 在用户回答"is"后尝试让程序返回主菜单以继续,或者如果用户回答“否”则让程序结束并显示总数消息

java - Servlet尝试运行Hadoop 2.2.0 MapReduce作业时发生异常

android - 只有创建 View 层次结构的原始线程才能触及它的 View 。 Paypal MPL Android