我正在做一个项目,我希望在ListView中显示机场列表。从本地数据库检索机场名称。 实际上我从sqliteopenhelper类
中获取数据库中的所有数据public List<AirportList> getAllAirportLists()
{
List<AirportList> airportList = new ArrayList<AirportList>();
String selectQuery = "SELECT * FROM " + TABLE_AIRPORT;
Log.i("query", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do{
AirportList airport = new AirportList();
airport.setId(Integer.parseInt(cursor.getString(0)));
airport.setName(cursor.getString(1));
}while(cursor.moveToNext());
}
return airportList;
}
和iam new to android任何人都可以在主类中调用这个方法,并在listview中显示机场列表。
答案 0 :(得分:0)
首先,在上述方法中,您需要在“airportList”中添加机场对象,如:
`public List getAllAirportLists(){
List<AirportList> airportList = new ArrayList<AirportList>();
String selectQuery = "SELECT * FROM " + TABLE_AIRPORT;
Log.i("query", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do{
AirportList airport = new AirportList();
airport.setId(Integer.parseInt(cursor.getString(0)));
airport.setName(cursor.getString(1));
airportList.add(airport);
}while(cursor.moveToNext());
}
return airportList;
}`
然后在主类的listView中分配此列表。像:
listView = (ListView) findViewById(R.id.listView); listView.setAdapter(new AppAdapter(airportList));
class AppAdapter extends ArrayAdapter<AirportList> {
AppAdapter(List<AirportList> itemList) {
super(YourAcitivity.this, R.layout.airport_item_list, itemList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = newView(parent);
}
bindView(position, convertView);
return convertView;
}
private View newView(ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.airport_item_list, parent,
false);
}
private void bindView(int position, View row) {
}
}
答案 1 :(得分:-1)