Search code examples
javaandroidandroid-listviewlistadapter

Two-line ListView does not show content


I currently have a problem with my two-line ListView. The two-line ListView shows the list separator, however, there is no data or text inside it. Where could I possibly have done wrong?

For additional information, the results.get(i) will show Tungro,18.92%, for example.

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);

Below is the screenshot of the ListView, where you could see the list separator but without text or data:

enter image description here

Is there something wrong with how I tokenize the string or with the adapter? Thanks a lot.


Solution

  • Thanks Mike M! I have changed my code to the following:

    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);