Well, I have a database and with Cursor, I am putting data from database in ArrayLists. With my code, I can only show one ArrayList's data in ListView's Item. But I want to put the first ArrayList in Item and the second ArrayList in SubItem. What do I need to do? (I can give more information or code if you need)
I already tried similar questions' answers but they didn't help me.
myDB = new DatabaseHelper(this);
ArrayList<String> theNames = new ArrayList<>();
ArrayList<Integer> theYear = new ArrayList<>();
Cursor data = myDB.getListContents();
if (data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
} else {
while(data.moveToNext()){
theNames.add(data.getString(1));
theYear.add(data.getInt(2));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theNames);
listView.setAdapter(listAdapter);
}
}
I expect to see the first ArrayList's data in ListView's Item and the second ArrayList's data in ListView's SubItem.
The way it is usually done, is that you would create a custom class (say, Person) with two attributes, name and date (birthdate? whatever). Then, when you are processing your cursor, for every row in the database, create an instance of your class, and populate it with the name and date from the cursor. Then, add that class instance to your ArrayList. Then, you would need to create a custom Adapter which extends BaseAdapter. You would pass the ArrayList in the constructor of that Adapter. Then, in the Adapter code, you override the getView method, which passes a position, and you get the person instance out of the ArrayList and you can then access the name and the date for that person. It's more work, but it is not difficult. Just search for building a custom Adapter, and building a custom class to see specifically how to do it.