OSDN Git Service

xml形式のデータインポート時にお知らせ表示することにした。
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / holders / MeMoMaDataFileHolder.java
1 package jp.sourceforge.gokigen.memoma.holders;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5
6 import android.content.Context;
7 import android.util.Log;
8 import android.widget.ArrayAdapter;
9
10 /**
11  *    めもまのデータファイル名を保持するクラス (ArrayAdapterを拡張)
12  * 
13  * @author MRSa
14  *
15  */
16 public class MeMoMaDataFileHolder extends ArrayAdapter<String> implements FilenameFilter
17 {
18         private final String TAG = toString();
19         private final Context context;
20         private final String fileExtension;
21
22         /**
23          *    コンストラクタ
24          * 
25          */
26     public MeMoMaDataFileHolder(Context context, int textViewRscId, String extension)
27         {
28                 super(context, textViewRscId);
29                 this.context = context;
30                 fileExtension = extension;
31         }
32
33         /**
34      *    ファイル一覧を生成する。
35      * 
36      */
37     public int updateFileList(String currentFileName)
38     {
39                 int matchedIndex = 0;
40         int outputIndex = -1;
41         clear();
42         String[] dirFileList = context.fileList();
43                 try
44                 {
45                         for (String fileName : dirFileList)
46                         {
47                                 int position = fileName.indexOf(fileExtension);
48                                 if (position >= 0)
49                                 {
50                                         String fileBaseName = fileName.substring(0, position);
51                                         if (fileBaseName.contentEquals(currentFileName))
52                                         {
53                                                 // 選択したインデックスを設定する。
54                                                 outputIndex = matchedIndex;
55                                         }
56                                         add(fileBaseName);
57                                         matchedIndex++;
58                                 }
59                         }
60                 }
61                 catch (Exception e)
62                 {
63                         e.printStackTrace();
64                 }
65
66                 if (getCount() == 0)
67                 {
68                         add("No Title");
69                         outputIndex = 0;
70                 }
71         System.gc();
72         
73         Log.v(TAG, "::::::: "  + " (" + currentFileName + ") : " + outputIndex + " <" + getCount() + ">");
74         return (outputIndex);
75     }
76
77     /**
78      *    受け付けるファイル名のフィルタを応答する。
79      *    (指定された拡張子を持つなファイルだけ抽出する。)
80      */
81     public boolean accept(File dir, String filename)
82     {
83         return (filename.endsWith(fileExtension));
84     }
85 }