猿问

两行ListView不显示内容

我目前的两行 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 的屏幕截图,您可以在其中看到列表分隔符,但没有文本或数据:

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



素胚勾勒不出你
浏览 119回答 1
1回答

慕仙森

我已将代码更改为以下内容:ArrayList<HashMap<String, String>> listItems = new ArrayList<>();HashMap<String, String> listItemData;for (int i=0; i<results.size(); i++) {&nbsp; &nbsp; if (results.size() != 0) {&nbsp; &nbsp; &nbsp; &nbsp; listItemData = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; String resultStr = results.get(i).toString();&nbsp; &nbsp; &nbsp; &nbsp; String[] resultStrVar = resultStr.split(",");&nbsp; &nbsp; &nbsp; &nbsp; listItemData.put("disease_name", resultStrVar[0]);&nbsp; &nbsp; &nbsp; &nbsp; listItemData.put("confidence", resultStrVar[1]);&nbsp; &nbsp; &nbsp; &nbsp; listItems.add(listItemData);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; listItemData = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; listItemData.put("disease_name", "No predictions found");&nbsp; &nbsp; &nbsp; &nbsp; listItemData.put("confidence", "Kindly shot again");&nbsp; &nbsp; &nbsp; &nbsp; listItems.add(listItemData);&nbsp; &nbsp; }}SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,&nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_list_item_2,&nbsp; &nbsp; &nbsp; &nbsp; new String[] {"disease_name", "confidence"},&nbsp; &nbsp; &nbsp; &nbsp; new int[] {android.R.id.text1, android.R.id.text2 });listView.setAdapter(adapter);
随时随地看视频慕课网APP

相关分类

Java
我要回答