Reputation: 131
I have some Lists consist of some Apk files' informations:
static ContentAndDAO contentAndDao = new ContentAndDAO();
public static void main(String[] args)
{
int manifestNum;
long contentId = 111111;
long devFileId = 222222;
List<DevFile> fileList;
List<DevSupport> supports = null;
List<ContentDev> contentList = new ArrayList<ContentDev>();
ContentDevDAO contentDevDao = new ContentDevDAO();
DevFileDAO devFileDao = new DevFileDAO();
ManifestMethods manifestMethods = new ManifestMethods();
DevFile apkFile = null;
try
{
manifestNum = 1;
File dir = new File("C:\\Users\\lenovo 01\\Desktop\\basari\\buulkcontent\\klasorlenen");
String[] extensions = new String[] {"apk"};
List<File> files = (List<File>) FileUtils.listFiles(dir, extensions, true);
Collections.sort(files);
for(File file : files)
{
apkFile = new DevFile();
fileList = new ArrayList<DevFile>();
if(file.getName().contains(".apk"))
{
FileInputStream fis = new FileInputStream(new File(file.getAbsolutePath()));
String apkMd5 = DigestUtils.md5Hex(fis);
fis.close();
System.out.println(file);
System.out.println(file.length());
System.out.println(apkMd5.toUpperCase());
System.out.println(contentId);
apkFile.setByteSize(file.length());
apkFile.setUrl("/file/getContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setThumbnailUrl("/file/getContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setDeleteUrl("/file/deleteContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setFileHash(apkMd5.toUpperCase());
apkFile.setFilePath("content/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + ".apk");
apkFile.setFileName(manifestMethods.getApplicationName(manifestNum).replaceAll(" ", "-") + ".apk");
apkFile.setName(manifestMethods.getApplicationName(manifestNum).replaceAll(" ", "-"));
apkFile.setPackageVersion(manifestMethods.getVersionName(manifestNum));
apkFile.setPackageName(manifestMethods.getPackageName(manifestNum));
apkFile.setPackageVersionCode(manifestMethods.getVersionCode(manifestNum));
apkFile.setSdkVersion(manifestMethods.getSdkVersion(manifestNum));
contentId++;
devFileId++;
manifestNum++;
}
}
for(int y = 1; y <= 53; y++)
{
manifestNum = 1;
fileList = new ArrayList<DevFile>();
DevFile file = new DevFile();
ContentDev content = new ContentDev();
/* some DevFile file addings */
fileList.add(file);
content.setDevFiles(fileList);
contentList.add(content);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
When I print the information one by one to the console, its showing just as I want. But in the List, its always showing only the last added apk file's package name, byte size, hash number etc. Of course I don't want that. What is wrong?
Note: Please don't mind the lack of legibility and modularity of code. I'm new to the object oriented structure.
Upvotes: 0
Views: 51
Reputation: 394106
You are creating a new list in each iteration of your loop :
for(int y = 1; y <= 53; y++)
{
fileList = new ArrayList<DevFile>();
DevFile file = new DevFile();
ContentDev content = new ContentDev();
/* some DevFile file addings */
fileList.add(file);
...
This means only the last file will be in that list at the end.
Change it to :
fileList = new ArrayList<DevFile>();
for(int y = 1; y <= 53; y++)
{
DevFile file = new DevFile();
ContentDev content = new ContentDev();
/* some DevFile file addings */
fileList.add(file);
...
In addition, I see that you create instances of DevFile
in another loop, but never do anything with them. Shouldn't they be added to the List?
Upvotes: 2