0

I have a TableView with 60 TextView that the user can touch and input data inside them with another Dialog Activity. The DialogActivity set text and color of the touched TextView. The problem is that when I touch another TextView I lose the previous touched TextView. Can I use the SharedPreference for store step by step all the touched TextView? For Example if I set the TextView at position 1:1 (row:columm) and I want set the TextView 2:2 how can I save step by step all the TextView for don't lose their inputed data?

Some java of the Activity that call the DialogActivity

public class ActivitySetOrario extends ActionBarActivity {

//Static perch� cosi non perdo i dati inseriti in precedenza!
static DataBase DB = new DataBase();
static int clickedTextViewId; // Declare TextView as class level member field


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_orario);

}//Fine oncreate

//Prende indietro la materia aggiunta dall'ActivityAddMateria
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 1) {
        if (resultCode == RESULT_OK) {

            String result = data.getStringExtra("result"); //Take the materia from Dialog
            int color = data.getIntExtra("color", 1); //Take the color from Dialog

            Materia materia;
            materia = new Materia();
            materia.setMateria(result); //put the name materia into class materia
            materia.setColor(color); //put the color materia into class materia
            DB.getMaterie().add(materia);
            // Now use mTextView  here
            //View view = findViewById(clickedTextViewId);
            TextView clickedtextView = (TextView) findViewById(clickedTextViewId); //(TextView) view;
            if(clickedTextViewId == clickedtextView.getId()) {
                clickedtextView.setText(result);
                clickedtextView.setBackgroundColor(color);
            }
        }

        if (resultCode == RESULT_CANCELED) {
            //Nessuna materia inserita
        }
    }
}//onActivityResult

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_set_orario, 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.

    switch (item.getItemId()) {
    case R.id.draw_orario:
        //addMateria();
        return true;
    case R.id.save_data_orario:
        //SERIALIZZO I DATI CHE DOVRA PRENDERE ActivityOrario
        backToOrario();
        return true;     
    case R.id.exit_orario:
        //Torno alla schermata orario annullo ogni modifica NON SERIALIZZO
        backToOrario();
        finish();
        return true;     
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

//Torna alla ActivityOrario
public void backToOrario(){
    Intent myIntent = new Intent(ActivitySetOrario.this, ActivityOrario.class);
    startActivity(myIntent);
}

public void addMateria(View v){
    //To get ID of your TextView do this
    clickedTextViewId = v.getId();
    //StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
    ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}
}

Code of DialogActivty

public class ActivityAddMateria extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_materia);

    final Button exit_button = (Button) findViewById(R.id.exit_dialog_materia);
    exit_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //No input
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            //Exit from Dialog
            finish();
        }
    });

    final Button accept_button = (Button) findViewById(R.id.add_materia);
    accept_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Find EditText for take data
            EditText nome_materia = (EditText)findViewById(R.id.nome_materia);
            //Put result into variable result that is send back
            String result = nome_materia.getText().toString();
            int color = getResources().getColor(R.color.rosso);
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",result).putExtra("color",color);
            setResult(RESULT_OK,returnIntent);
            // Exit to Dialog
            finish();
        }
    });

}
}

It's good sharedpreferences or it's better another way?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

SharedPreferences is fine for small amounts of data, otherwise you would need to look into sqllite for heavy data storage.

for your question take a look at this post: How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Matchbox2093
  • 986
  • 2
  • 12
  • 40
  • But can i use the SharedPreferences for store only 3 or 4 TextView and after serialize them to a file? I want use SharedPreference for make a temp of a TExtView inputed and after store them with Sqlite or file or something with an ok button. Or max 30 TextView inputed and stored into SharedPreference it's an heavy data storage? –  Mar 23 '15 at 15:37
  • if its over 30 it would be best to go with SQLlite, shared preferences is an easy method for data light applications (1-10 textviews) , however in your case, its best go with sqllite from the beginning as transferring sharedpreferences to sqllite can cause more harm than good. take a look at this discussion and decide which is best for your app in the long run http://stackoverflow.com/questions/6276358/pros-and-cons-of-sqlite-and-shared-preferences – Matchbox2093 Mar 23 '15 at 15:42
  • But SharedPreference are the same to create a file and store the results into it? Sorry but i'm new to android and java, maybe my question is so stupid! –  Mar 23 '15 at 16:46
  • Did you refer to the link i had attached in the above comment? yes it does store in a file of course, its just which one is best suited for your application. simply putting it from my point of view....if i have small data and want it quickly i use sharedpreferences due to keys assigned to it. However if i have a huge amount of data and want it quickly i use sqllite, as its designed for huge data and querying, making it quicker than iterating through shared preferences for the correct key. – Matchbox2093 Mar 23 '15 at 16:53
  • Ah ok perfect. Ok my app require a dynamic input to this table with 60 textview. So the user can insert only 3 TextView or all 60. The best way it's the sqlite, fast for small amounts of data AND for heavy storage. Thank you! –  Mar 23 '15 at 16:59