0

please help me understand why my ArrayAdapter is not working properly, I mean it dosen't show the List of Strings that I gave as a parameter. I receive the flowing message in emulator"unfortunately 'program name' has stopped". but when I give a regular String [] array it show the items properly. here is the "problematic" code sections.

//this method is in Class DatabaseAccess, it return a record from db as List<String> 
public List<String> getQuestionRecord() {
    List<String> list = new ArrayList<>();
    int counter = 1;
    while (!cursor.isAfterLast() && (counter < 8)) {
        list.add(cursor.getString(counter++));
    }
    if (!cursor.isAfterLast()){
        cursor.moveToNext();
    }else{
        closeCursor();
        return null;
    }
    return list;
}

// this method called from the MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listView);
    databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    databaseAccess.getCursorReady();
    List<String> questionRecord = databaseAccess.getQuestionRecord();
    if (questionRecord != null){
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,questionRecord);
        listView.setAdapter(adapter);
    }else
        databaseAccess.close();
}
  • can you also post error log/stacktrace – Vishnu Prasad Aug 01 '17 at 02:55
  • Your adapter is not able to handle a `List` but is able to handle a `String[]` array, so to just make it work add a conversion below `List questionRecord = databaseAccess.getQuestionRecord();`, like `String[] questionRecAsArray = new String[questionRecord.size()];` and then `questionRecAsArray = questionRecord.toArray(questionRecAsArray);`. Then you add the `questionRecAsArray` to your adapter if the `questionRecord != null` – deHaar Aug 01 '17 at 05:58
  • I think that the problem is with the ListView object. it seems that it can't handle long String arrays like the one I passed to the adapter. the string array contain 8 strings and each string is a fairly long sentence. I guess the ListView object is running out of space .... – Sami Kassoum Aug 01 '17 at 07:47
  • I figured it out, the variable of type List named questionRecord , has a null value in the last item. when I removed the null the code worked perfectly. so android ListView can't handle null String value inside an array , I don't know why is this. – Sami Kassoum Aug 01 '17 at 08:04

0 Answers0