2

I'm trying to make an app which will let user enter any name in a dialog after pressing a button and then save that name, and all other names user enters, so when he opens the app again, he gets all those names one below another. Of course, i don't want overwriting old names after entering new ones, i want to add new names to old names obviously. I've been working with SharedPreferences before, but i have no idea how to make something like this. I mean it's easy to save one value like high score in games and read it later, but this seems hard to me. I really wouldn't ask for help if i could solve it myself, but i'm trying 5 hours already without success. Please help me. Thanks in advance!

EDIT: My code:

    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);
    }
}

2 Answers2

0

You should think about moving to SQLite. This can help you move on.

Shubham A.
  • 2,446
  • 4
  • 36
  • 68
0

Or you could use SQLite with ORM based libraries. Some of the popular ones include:

Malvin
  • 692
  • 5
  • 8