内部Arraylist <hashmap>在添加android?</hashmap>后最后显示null

时间:2014-09-01 14:27:56

标签: android arraylist hashmap

我从数据库访问数据并存储在arraylist中但最后我显示完整列表显示为null

  

输出:

     

09-01 17:26:49.550:E / data(16620):----&gt; 15

     

09-01 17:26:49.550:&gt; E / data(16620):----&gt; [[],[],[],[],[],[],[],   [],[],[],[],[],[],[],[]]

public static ArrayList<ArrayList<HashMap<String, String>>> testdata(SQLiteDatabase db)
{
         int count=0;
         int k=0;
         ArrayList<ArrayList<HashMap<String, String>>> data =
                 new ArrayList<ArrayList<HashMap<String,String>>>();
         ArrayList<HashMap<String, String>> odata =
                 new ArrayList<HashMap<String,String>>();

         String Query=
          "SELECT * FROM myfiles "+
          "WHERE (( Datetime('2014-09-01 09:50:15') >= startdate " +
            "AND Datetime('2014-09-01 09:50:15') <= enddate ) " +
            "AND ( " +
                "('09:50:15' >= strftime('%H:%M:%S',timer_from) " +
                "AND '09:50:15' <= strftime('%H:%M:%S',timer_to )) " +
                "OR ( strftime('%H:%M:%S',timer_from) = '00:00:00' " +
                    "OR '00:00:00' = strftime('%H:%M:%S',timer_to )))) " +
            "AND ( Monday = 1 OR Everyday = 1) AND download = 1 " +
            "AND playlist_id = 24 AND user_id='83' " +
          "ORDER BY position_id ASC, subposition_id ASC";

        Cursor cursor = db.rawQuery(Query, null);
        if(cursor.getCount()<=0)
        {
            cursor.close();
            return data;
        }          
        else
        {
             if (cursor.moveToFirst()) 
             {
                 do 
                 {
                     HashMap<String, String> map = new HashMap<String, String>();
                     String currenttype=cursor.getString(9);
                     String nexttype = null;
                     if(count<cursor.getCount())
                     {   
                         if(cursor.getPosition()==cursor.getCount()-1)
                         {   

                             nexttype="image";
                         }  
                         else
                         {
                             cursor.moveToNext(); 
                             nexttype=cursor.getString(9);
                             cursor.moveToPrevious(); 
                         }

                     }
                     map.put(DB_Constant.MYFILES.FILE_ID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_ID)));
                     map.put(DB_Constant.MYFILES.FILE_TYPE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_TYPE)));
                     map.put(DB_Constant.MYFILES.FILE_SUBTYPE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_SUBTYPE)));
                     map.put(DB_Constant.MYFILES.FILE_PATH, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_PATH)));
                     map.put(DB_Constant.MYFILES.USERID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.USERID)));
                     map.put(DB_Constant.MYFILES.FILE_SECOND, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_SECOND)));
                     map.put(DB_Constant.MYFILES.FILE_DOWNLOAD, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_DOWNLOAD)));
                     map.put(DB_Constant.MYFILES.FILE_STARTTIME, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_STARTTIME)));
                     map.put(DB_Constant.MYFILES.FILE_ENDTIME, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_ENDTIME)));
                     map.put(DB_Constant.MYFILES.FILE_TIMERFROM, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_TIMERFROM)));
                     map.put(DB_Constant.MYFILES.FILE_TIMERTO, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_TIMERTO)));
                     map.put(DB_Constant.MYFILES.FILE_STUDIOFORMAT, cursor.getString(cursor.getColumnIndex(DB_Constant.MYFILES.FILE_STUDIOFORMAT)));
                     odata.add(map);

                     if(currenttype.equalsIgnoreCase("image") || 
                        currenttype.equalsIgnoreCase("video"))
                     {
                         data.add(odata);
                         odata.clear();
                     }
                     else if(nexttype.equalsIgnoreCase("image") || 
                        nexttype.equalsIgnoreCase("video"))
                     {
                         data.add(odata);
                         odata.clear();
                     }
                     count++;
                 } while (cursor.moveToNext());
             }
         }   
        Log.e("data","---->"+data.size());
        Log.e("data","---->"+data);
        cursor.close();
        return data;
  }

2 个答案:

答案 0 :(得分:2)

当您向Map,List或Set添加元素时,会添加原始对象,不会创建任何副本,因此如果您修改它,则会在集合中对其进行修改。简单的例子:

class Foo {
  public int bar;
}

在另一段代码中使用那个简单的类......

List<Foo> myList= new ArrayList<>();

Foo f= new Foo(); //we create a Foo object, and f is a variable that references it
f.bar=9; 
myList.add(f);  //we add the object to the list

现在我们有两个对此对象的引用:fmyList.get(0)。如果你这样做:

f.bar=15;

然后

System.out.println(myList.get(0))

会在屏幕上显示15

在您的代码中,您正在重用相同的对象。而是调用clear(),创建一个新对象:

odata = new ArrayList<HashMap<String,String>>();

总之:您可以重用变量,但在这种情况下,您无法重用引用的对象。

答案 1 :(得分:0)

您已成功将哈希地图添加到列表中。 计数也显示15 。唯一的问题是,您无法正确存储在哈希映射上或正确显示