在我的Android項目, 我得到的數據列表數組列表中附加的ArrayList <object>到的ListView -Android
ArrayList<Items> item = db.getAllMenu();
,但現在我想這個數據添加到列表視圖, 我想作爲,
ListView lv=(ListView)findViewById(R.id.list_view_inside_nav);
String[] lv_arr = {};
lv_arr = (String[]) item.toArray();
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, lv_arr));
但它給人error.because我試圖arratlist轉換爲字符串.. 有人plz幫助我,怎麼到ArrayList轉換爲字符串[]
這裏是我的一些文件... items.java(getter和setter方法)
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;
}
}
,我使用此代碼從數據庫
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();
menu.setName(cursor.getString(0));
// Adding category to list
passList.add(menu);
} while (cursor.moveToNext());
}
// return category list
return passList;
}
使用自定義列表適配器。 –
'items'的類型是'ArrayList',你不能將它轉換爲'String []'。什麼是'Items'對象的'String'表示? –
Egor
@brijeshkumar,爲什麼? – Egor