英文:
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
It's my layout
<?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>
And backend
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);
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<String, Double> entry : borrowers.entrySet()) {
String key = entry.getKey();
Double value = entry.getValue();
arrayList.add(key + "\t" + value);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论