2

だから私はシリアル化のシーンにかなり慣れていません、そしてハッシュテーブルをシリアル化してそれをファイルに保存することが可能かどうかわかりません...しかしこれは私がこれまでに試したことです..何らかの理由でそれは行きますTryブロックを実行する代わりに、コードのキャッチ部分に?

public void addDataIntoFlashCardFile(Context context, Hashtable<Integer, ArrayList<Deck>> data) {
        try {
            FileOutputStream fos = context.openFileOutput(
                    FLASHCARDS_FILENAME, Context.MODE_PRIVATE
                            | Context.MODE_APPEND);
            ObjectOutputStream osw = new ObjectOutputStream(fos);
            osw.writeObject(data);

    } catch (FileNotFoundException e) {
        // catch errors opening file
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "calles", Toast.LENGTH_SHORT).show();
    }
    }

ここで私はファイルからそれを読み取ろうとします(そもそもファイルに書き込まないため、機能しません)

try {

            Hashtable<Integer, ArrayList<Deck>> temp = new Hashtable<Integer, ArrayList<Deck>>();
            FileInputStream myIn = context.openFileInput(FLASHCARDS_FILENAME);
            ObjectInputStream IS = new ObjectInputStream(myIn);
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
            try {

                temp = (Hashtable<Integer, ArrayList<Deck>>)IS.readObject();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            IS.close();

            //testing purposes
            for (int i = 0; i < temp.size(); i++) {
                for (int p = 0; p < temp.get(i).size(); p++) {
                    for (int q = 0; q < temp.get(i).get(p).getDeck().size(); q++) {
                    Toast.makeText(context, temp.get(i).get(p).getDeck().get(q).getQuestion(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
        }
    }
4

1 に答える 1

1

実際にJSONObjectを使用できます。

ステップ1:JSONObjectを作成します。

JSONObject myAwesomeObject = new JSONObject();
JSONArray myAwesomeArray = new JSONArray();

ステップ2:HashMapを反復処理し、JSONObjectに追加します

for (Entry<Integer, ArrayList<Deck>> entry : map.entrySet())
{
    ArrayList<Deck> decks = temp.get(entry);
    JSONObject JSONDeck = new JSONObject();
    for (Deck deck : decks){
        JSONDeck.add("deck", deck.getWhateverDataDeckContains());
    }
    myAwesomeArray.add("deck", JSONDeck);
}
myAwesomeObject.add("deck_collection", myAwesomeArray);

ステップ3:テーブルを含む文字列を取得します。

String myAwesomeContents = myAwesomeObject.toString();

ステップ4:プレーンテキストとしてファイルに挿入します。

デシリアライズするには、JSONObjectを繰り返し処理し、新しいテーブルに入力するだけです。

ステップ1:ファイルから文字列を取得します。

手順2:その文字列を含むJSONObjectの新しいインスタンスを作成します。

JSONObject deserialized = new JSONObject(stringContainingYourDataInJSON);

ステップ3:単純なデータを取り戻す:

ArrayList<Deck> decksArray = new ArrayList<Deck>();
JSONArray decks = deserialized.getJSONArray("deck_collection");
for (int i=0; i<decks.length(); i++){
    JSONObject deck = decks.get(i);
    String name = deck.getString("name");
    int length = deck.getInt("length");
    // etc
    Deck deckPojo = new Deck();
    deckPojo.setWhatever(whateverParamsYouWantToSet);
    decksArray.add(deckPojo);
 }
于 2012-11-07T08:47:51.807 に答える