varsha valanju varsha valanju - 2 months ago 12
Android Question

add ArrayList<object> to listview -android

in my android project,
i am getting a list of data in arraylist

ArrayList<Items> item = db.getAllMenu();

but now i want to add this data into listview,
i tried as,

ListView lv=(ListView)findViewById(;
String[] lv_arr = {};
lv_arr = (String[]) item.toArray();
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, lv_arr));

but its giving error.because i am trying to convert arratlist to string..
anyone plz help me, how to convert the arraylist to string[]

here are my some files... (getter and setter methods)

public class Items {
//private variables

String _name;
// Empty constructor
public Items(){

// constructor
public Items(String name){

this._name = name;


// getting name
public String getName(){
return this._name;
// setting name
public void setName(String name){
this._name = name;


and i am using this code to get data from database

public ArrayList<Items> getAllMenu() {
ArrayList<Items> passList = new ArrayList<Items>();
// Select All Query
String selectQuery = "SELECT * FROM " + CATEGOTY_TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Items menu = new Items();


// Adding category to list
} while (cursor.moveToNext());
// return category list
return passList;


Wrong. Item class cannot convert to a String class. You need to convert each 'Item' to 'String' object.


Say your class Item

class Item{
public String itemName;

In your code change

String[] lv_arr = new String[items.size()];

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

lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, lv_arr));