0

我尝试从数据库中检索所有数据并放入 hasmap 的数组列表中,但我只得到表的最后一条记录。这是我的代码:

databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();
Cursor c = myDbHelper.selectAll();
HashMap<String, String> map = new HashMap<String, String>();
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
if(c!=null && c.moveToFirst())
        {
                do
            {
                map.put(KEY_ACODE, c.getString(5).toString());
                map.put(KEY_FNUM, c.getString(6).toString());
                map.put(KEY_STOP_1, c.getString(8).toString());
                map.put(KEY_FTIME, c.getString(7).toString());
                map.put(KEY_REMARK, c.getString(10).toString());
                map.put(KEY_REM_COM, c.getString(11).toString());
                dataList.add(map);
            }while(c.moveToNext());
        }

最后,我的 dataList(map) 只检索最后一条记录......

任何人都可以帮助我吗..

4

2 回答 2

1

将此行移动如下:HashMap<String, String> map = new HashMap<String, String>();

databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();

Cursor c = myDbHelper.selectAll();
HashMap<String, String> map;
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
if(c!=null ){
     c.moveToFirst();

             while(  cur.isAfterLast() == false )
            {
                map = new HashMap<String, String>(); // <---- moved here

                map.put(KEY_ACODE, c.getString(5).toString());
                map.put(KEY_FNUM, c.getString(6).toString());
                map.put(KEY_STOP_1, c.getString(8).toString());
                map.put(KEY_FTIME, c.getString(7).toString());
                map.put(KEY_REMARK, c.getString(10).toString());
                map.put(KEY_REM_COM, c.getString(11).toString());
                dataList.add(map);
                c.moveToNext();
            }
        }
于 2012-10-25T06:32:22.277 回答
1

像这样更改您的代码

databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();
Cursor c = myDbHelper.selectAll();
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
while(c.moveToNext());
{
     HashMap<String, String> map = new HashMap<String, String>();
     map.put(KEY_ACODE, c.getString(5).toString());
     map.put(KEY_FNUM, c.getString(6).toString());
     map.put(KEY_STOP_1, c.getString(8).toString());
     map.put(KEY_FTIME, c.getString(7).toString());
     map.put(KEY_REMARK, c.getString(10).toString());
     map.put(KEY_REM_COM, c.getString(11).toString());
     dataList.add(map);
}
于 2012-10-25T06:34:23.150 回答