1

このコードを目的の c に再作成する方法はありますか? 始めたばかりで、助けが必要です。

String name = #;// Will often vary.

SharedPreferences userData = this.getSharedPreferences(name + "userdata", MODE_PRIVATE);
    Editor edit = userData.edit();
    edit.clear();
    edit.putFloat("rating", Rating.getRating());
    edit.putString("good", txtGood.getText().toString().trim());
    edit.putString("improve", txtImprove.getText().toString().trim());
    edit.commit();
    Log.d(TAG, "Saving Data");

The advantage of this code is that is makes a new SharedPreference everytime the method is called. With my experience using NSUserDefaults, it was only able to make 1 batch of data.

4

2 に答える 2

0
- (NSString*)GetFilePath:(NSString*)filename
{
     NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString* file = [[NSString alloc]initWithFormat:@"%@.plist",filename];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:file];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]) //4
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"]; //5

        [fileManager copyItemAtPath:bundle toPath:path error:&error]; //6
    }  
    return path;
}

任意のメソッドから次のように呼び出します。

NSMutableDictionary* TempDict = [[NSMutableDictionary alloc]initWithContentsOfFile:[self GetFilePath:@"FileName"]];

Plist は、あらゆる種類のデータを保存できる共有設定のようなものです。配列、文字列、ブール値など、それに制限はありません。詳細については、これをご覧ください。

于 2013-07-24T04:42:53.420 に答える