动态微调器和共享首选项

发布于 2024-11-05 22:16:00 字数 2122 浏览 0 评论 0原文

在我的 android 应用程序中,我有一个按钮,它向布局添加了一个新的动态微调器。所有创建的 Spinner 都使用相同的数组。

到目前为止,我可以保存创建的微调器的数量,并在重新启动应用程序后重新创建它们。 但我真的很想将每个 Spinner 的 selectedPosition 保存在共享首选项中,这就是我陷入 ForceClose Desaster 的地方...

根据我的理解,每个 Spinner 在创建时都会获得一个 ID,因此您可以保存在此范围内的 Position首选项中的 ID。

这就是我所做的:

public void addSpinner(){

    LinearLayout AddLayout = (LinearLayout)findViewById(R.id.linearAddScroll);
    spinner = new Spinner(this);
    ArrayAdapter<?> adapt = ArrayAdapter.createFromResource(this,
            R.array.Filter, android.R.layout.simple_spinner_item);
    adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapt);
    AddLayout.addView(spinner);
    }

创建了 Spinner。

public void onClick(View v) {
            addSpinner();


            int ID = 1000+x;
            spinner.setId(ID);
            Toast.makeText(MatReporterActivity.this,"ID" + ID, 5)
            .show();
            x++;
        }

设置ID。

这就是我在 on Create 方法中所做的:

x = settings.getInt("xsave", 1);
    for(y = 1; y < x; y++){
        addSpinner();

        int ID = 1000+y;
        Spinner s = (Spinner) findViewById(ID);

        String ys= Integer.toString(ID);
        Toast.makeText(MatReporterActivity.this,"ID" +ys, 5)
        .show();
        int yf = settings.getInt(ys, 1);
        s.setSelection(yf);
        }

而这个 onStop():

SharedPreferences settings = PreferenceManager
            .getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("xsave", x);
    for(y = 1; y < x; y++){
        int ID = 1000+y;
        Spinner s2= (Spinner) findViewById(ID);
        int possS = s2.getSelectedItemPosition();
        Toast.makeText(MatReporterActivity.this, "IDStop" + ID, 5)
        .show();
        String ys= Integer.toString(ID);
        editor.putInt(ys, possS);
    }

    editor.commit();
}

我认为 onCreate 方法中存在逻辑问题,但我找不到它,我也没有在网络上找到任何帮助如何填充和保存动态创建的微调器。

所以也许有人有一个想法。 谢谢。

in my android application I have a Button which adds a new dynamic Spinner to the Layout. All of the created Spinners are using the same Array.

What is working until now, I can save the number of created Spinners and recreate them after restarting the Application.
But I really would like to save the selectedPosition of each Spinner in the sharedPreferences and this is where I'm stucking in a ForceClose Desaster...

In my understanding, every Spinner gets an ID when created so you can save the Position bounded on this ID in the preferences.

So this is what I did:

public void addSpinner(){

    LinearLayout AddLayout = (LinearLayout)findViewById(R.id.linearAddScroll);
    spinner = new Spinner(this);
    ArrayAdapter<?> adapt = ArrayAdapter.createFromResource(this,
            R.array.Filter, android.R.layout.simple_spinner_item);
    adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapt);
    AddLayout.addView(spinner);
    }

this creates the Spinner.

public void onClick(View v) {
            addSpinner();


            int ID = 1000+x;
            spinner.setId(ID);
            Toast.makeText(MatReporterActivity.this,"ID" + ID, 5)
            .show();
            x++;
        }

set the ID.

This is what I do in the on Create method:

x = settings.getInt("xsave", 1);
    for(y = 1; y < x; y++){
        addSpinner();

        int ID = 1000+y;
        Spinner s = (Spinner) findViewById(ID);

        String ys= Integer.toString(ID);
        Toast.makeText(MatReporterActivity.this,"ID" +ys, 5)
        .show();
        int yf = settings.getInt(ys, 1);
        s.setSelection(yf);
        }

And this onStop():

SharedPreferences settings = PreferenceManager
            .getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("xsave", x);
    for(y = 1; y < x; y++){
        int ID = 1000+y;
        Spinner s2= (Spinner) findViewById(ID);
        int possS = s2.getSelectedItemPosition();
        Toast.makeText(MatReporterActivity.this, "IDStop" + ID, 5)
        .show();
        String ys= Integer.toString(ID);
        editor.putInt(ys, possS);
    }

    editor.commit();
}

I think there is a logical Problem in the onCreate Method, but I'm not able to find it, also I didn't find any help in the web how to populate and save dynamically created spinners.

So maybe someone has an idea.
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-11-12 22:16:00

SharedPreferences 不是存储此类数据的好方法。您应该尝试遵循这两个步骤:

创建一个实现可序列化的类来表示您要存储的数据(您可以使用可序列化对象的列表)

public class SpinnerSave implements Serializable {
  public String ID;
  public int selection;

  public SpinnerSave(String ID, int selection){
    this.ID = ID;
    this.selection = selection;
  }
}

然后您应该将数据写入像这样的文件中

private void saveState() {
final File cache_dir = this.getCacheDir(); 
final File suspend_f = new File(cache_dir.getAbsoluteFile() + File.separator + SUSPEND_FILE);

FileOutputStream   fos  = null;
ObjectOutputStream oos  = null;
boolean            keep = true;

try {
    fos = new FileOutputStream(suspend_f);
    oos = new ObjectOutputStream(fos);

    oos.writeObject(this.gameState);
}
catch (Exception e) {
    keep = false;
    Log.e("MyAppName", "failed to suspend", e);
}
finally {
    try {
        if (oos != null)   oos.close();
        if (fos != null)   fos.close();
        if (keep == false) suspend_f.delete();
    }
    catch (Exception e) { /* do nothing */ }
}
}

SharedPreferences are not a good way to store this kind of data. You should try to follow those 2 steps :

Create a class which implements Serializable to represent the data you want to store (you might use a list of Serializable objects)

public class SpinnerSave implements Serializable {
  public String ID;
  public int selection;

  public SpinnerSave(String ID, int selection){
    this.ID = ID;
    this.selection = selection;
  }
}

Then you should write your data into a file like so

private void saveState() {
final File cache_dir = this.getCacheDir(); 
final File suspend_f = new File(cache_dir.getAbsoluteFile() + File.separator + SUSPEND_FILE);

FileOutputStream   fos  = null;
ObjectOutputStream oos  = null;
boolean            keep = true;

try {
    fos = new FileOutputStream(suspend_f);
    oos = new ObjectOutputStream(fos);

    oos.writeObject(this.gameState);
}
catch (Exception e) {
    keep = false;
    Log.e("MyAppName", "failed to suspend", e);
}
finally {
    try {
        if (oos != null)   oos.close();
        if (fos != null)   fos.close();
        if (keep == false) suspend_f.delete();
    }
    catch (Exception e) { /* do nothing */ }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文