0

我正在做一个项目,我想在 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;

    }

我是 android 新手,任何人都可以提供一个想法,以便在主类中调用此方法并在列表视图中显示机场列表。

4

2 回答 2

0

首先,在上面的方法中,您需要在“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));
        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) {

        }
    }
于 2013-04-01T10:48:27.367 回答
-1

嘿首先检查以下教程

简单列表视图简单列表

自定义列表视图自定义列表视图

在创建适配器之前从数据获取列表并添加到适配器。

于 2013-04-01T10:38:49.250 回答