【问题标题】:How to save and read all names user entered?如何保存和读取用户输入的所有名称?
【发布时间】:2015-06-22 02:41:41
【问题描述】:

我正在尝试制作一个应用程序,它允许用户在按下按钮后在对话框中输入任何名称,然后保存该名称以及用户输入的所有其他名称,所以当他再次打开应用程序时,他会得到所有这些在另一个之下命名。 当然,我不想在输入新名称后覆盖旧名称,我显然想在旧名称中添加新名称。 我以前一直在使用 SharedPreferences,但我不知道如何制作这样的东西。 我的意思是在游戏中保存一个值(例如高分)并稍后阅读很容易,但这对我来说似乎很难。 如果我能自己解决它,我真的不会寻求帮助,但我已经尝试了 5 个小时但没有成功。 请帮我。 提前致谢!

编辑: 我的代码:

    package cannon.gaming.mymarks;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends ActionBarActivity {
    SharedPreferences subjectData;
    String subjectname = "MySharedSubjects";
    TextView textSubject;
    ArrayList<String> lista = new ArrayList<String>();
    List<String> list = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
        textSubject = (TextView) findViewById(R.id.textSubject);
        subjectData = getSharedPreferences(subjectname, 0);
        final String mysubjects = subjectData.getString("MySharedSubjects", "0");
        File a = new File("/data/data/cannon.gaming.mymarks/shared_prefs/MySharedSubjects.xml");
        if(a.exists()){
            List<String> list = Arrays.asList(TextUtils.split(mysubjects, ","));
            textSubject.append("\n" + list);
        }else{
            SharedPreferences.Editor editor = subjectData.edit();
            editor.putString("MySharedSubjects", mysubjects);
            editor.commit();
        }
        buttonAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                showInputDialog();
            }
        });
    }

    protected void showInputDialog() {

        // get prompts.xml view
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        View promptView = layoutInflater.inflate(R.layout.activity_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        alertDialogBuilder.setView(promptView);

        final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
        // setup a dialog window
        alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        lista.add(String.valueOf(editText));
                        SharedPreferences.Editor editorr = subjectData.edit();
                        editorr.putString("MySharedSubjects", TextUtils.join(",", lista));
                        editorr.commit();
                        textSubject.append("\n" + editText.getText());
                        /*textSubject.append("\n" + editText.getText());
                        String subject = String.valueOf(editText);
                        SharedPreferences.Editor editorr = subjectData.edit();
                        editorr.putString("MySharedSubjects", subject + ",");
                        editorr.commit();*/
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create an alert dialog
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 你试过的代码在哪里?
  • 等一下,我加一下
  • 你去,我添加了它
  • 您可能想要存储项目列表。看看这个线程:stackoverflow.com/questions/6598331/…
  • 你考虑过 SQLlite 吗?

标签: java android android-studio


【解决方案1】:

您应该考虑迁移到 SQLite。 This 可以帮助您继续前进。

【讨论】:

    【解决方案2】:

    或者您可以将 SQLite 与基于 ORM 的库一起使用。 一些受欢迎的包括:

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多