两行ListView不显示内容

时间:2019-02-03 19:33:45

标签: java android android-listview listadapter

我的两行ListView当前有问题。两行的ListView显示列表分隔符,但是其中没有数据或文本。我可能在哪里做错了?

例如,有关其他信息,results.get(i)将显示Tungro,18.92%

ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
    HashMap<String, String> listItemData;

    for (int i=0; i<results.size(); i++) {

        if (results.size() != 0) {
            listItemData = new HashMap<String, String>();
            String resultStr = results.get(i).toString();
            String[] resultStrVar = resultStr.split(",");

            listItemData.put(resultStrVar[0], resultStrVar[1]);
            listItems.add(listItemData);

        } else {
            listItemData = new HashMap<String, String>();
            listItemData.put("No predictions found", "Kindly shot again");
            listItems.add(listItemData);
        }
    }

    SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
            android.R.layout.simple_list_item_2,
            new String[] {"First Line", "Second Line"},
            new int[] {android.R.id.text1, android.R.id.text2 });

    listView.setAdapter(adapter);

下面是ListView的屏幕截图,您可以在其中看到列表分隔符,但没有文本或数据:

enter image description here

我如何标记字符串或使用适配器有问题吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

感谢Mike M!我已将代码更改为以下内容:

ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
HashMap<String, String> listItemData;

for (int i=0; i<results.size(); i++) {

    if (results.size() != 0) {
        listItemData = new HashMap<String, String>();
        String resultStr = results.get(i).toString();
        String[] resultStrVar = resultStr.split(",");

        listItemData.put("disease_name", resultStrVar[0]);
        listItemData.put("confidence", resultStrVar[1]);
        listItems.add(listItemData);

    } else {
        listItemData = new HashMap<String, String>();
        listItemData.put("disease_name", "No predictions found");
        listItemData.put("confidence", "Kindly shot again");
        listItems.add(listItemData);
    }
}

SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
        android.R.layout.simple_list_item_2,
        new String[] {"disease_name", "confidence"},
        new int[] {android.R.id.text1, android.R.id.text2 });

listView.setAdapter(adapter);