如何在 ListView 中显示 HashMap 的项和子项

huangapple go评论72阅读模式
英文:

How to display item and subitem from HashMap in ListView

问题

我想要从HashMap中显示项目和子项目,项目将是字符串(借款人姓名),子项目为双精度(借款人贷款)。这是我的第一个Android应用程序,所以请理解:)

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="669dp">
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/editButton"
            android:layout_width="244dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dodaj" />

        <Button
            android:id="@+id/sumButton"
            android:layout_width="202dp"
            android:layout_height="wrap_content"
            android:text="Suma dlugow" />

    </LinearLayout>
</LinearLayout>

</RelativeLayout>

后端代码:

listView = findViewById(R.id.listView);

Map<String, Double> borrowers = new HashMap<>();
ArrayList<String> arrayList = new ArrayList<>();

borrowers.put("John", 300.0);
arrayList.add(String.valueOf(borrowers));

ArrayAdapter<Borrower> arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);

应用程序仅显示一个项目,它看起来像 {John=300.0},我希望它看起来像:

John
300.0

或者只是

John    300
英文:

I would like to display item and subitem from HashMap, item would be String (borrower name), subitem double(borrower loan). It's my first android app so be understanding 如何在 ListView 中显示 HashMap 的项和子项

It's my layout

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;

android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;center&quot;
android:orientation=&quot;vertical&quot;&gt;

&lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;ListView
        android:id=&quot;@+id/listView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;669dp&quot;&gt;

    &lt;/ListView&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;horizontal&quot;&gt;

        &lt;Button
            android:id=&quot;@+id/editButton&quot;
            android:layout_width=&quot;244dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_weight=&quot;1&quot;
            android:text=&quot;Dodaj&quot; /&gt;

        &lt;Button
            android:id=&quot;@+id/sumButton&quot;
            android:layout_width=&quot;202dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;Suma dlugow&quot; /&gt;

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

</RelativeLayout>

And backend

    listView = findViewById(R.id.listView);

    Map&lt;String, Double&gt; borrowers = new HashMap&lt;&gt;();
    ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();

    borrowers.put(&quot;John&quot;, 300.0);
    arrayList.add(String.valueOf(borrowers));

    ArrayAdapter&lt;Borrower&gt; arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(arrayAdapter);

And app display only one item and it's look like {John=300.0}, i want to look like:

John
300.0

or just John \t 300

答案1

得分: 1

你当前的代码只是获取整个映射的String值。您需要遍历映射以获取键和值,然后构造新的String并将其添加到您的List中。

for (Map.Entry<String, Double> entry : borrowers.entrySet()) {
    String key = entry.getKey();
    Double value = entry.getValue();
    arrayList.add(key + "\t" + value);
}
英文:

Your current code is just grabbing the String value of the entire map. You need to iterate over the map to grab the key and value and then construct your new String and add it to your List.

for (Map.Entry&lt;String, Double&gt; entry : borrowers.entrySet()) {
    String key = entry.getKey();
    Double value = entry.getValue();
    arrayList.add(key + &quot;\t&quot; + value);
}

huangapple
  • 本文由 发表于 2020年4月3日 22:35:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61014245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定