包含对象列表的Android类

时间:2015-02-13 19:36:05

标签: java android

我是Android和Java的新手。 我创建了一个包含材料列表的类,用户可以从另一个Dialog活动中添加新材料。 当用户从Dialog活动中添加新材料时,他会丢失旧材料。 显然这发生了,因为每次app调用活动接收者时,活动再次实例化该类(这是问题?)。 这是从Dialog

接收数据的活动代码
package com.ddz.diarioscolastico;

import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivitySetOrario extends ActionBarActivity {

TextView textView;
DataBase DB = new DataBase();

@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");                
            Materia materia;
            materia = new Materia();
            materia.setMateria(result);
            materia.setColor(getResources().getColor(R.color.verde));
            DB.getMaterie().add(materia);

            LinearLayout mLayout = (LinearLayout) findViewById(R.id.contenitorematerie);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
            params.setMargins(30, 0, 0, 0);

            for(int i=0; i< DB.getMaterie().size(); i++)
            {
                textView = new TextView(this);
                textView.setText(DB.getMaterie().get(i).getMateria());
                textView.setBackgroundColor(DB.getMaterie().get(i).getColor());
                textView.setLayoutParams(params);
                mLayout.addView(textView);
            }

        } 

        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
        //FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        //ObjectOutputStream os = new ObjectOutputStream(fos);
        //os.writeObject(this);
        //os.close();
        //fos.close();
        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(){
    //StartActivityForResult perchè mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
    ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}
}

DataBase DB = new DataBase();每次调用这个新数据库,我都会丢失旧数据!

这是问题吗? 我尝试使用全局变量,但我不知道我需要在哪里实例化该类只有一次!

package com.ddz.diarioscolastico;

import android.app.Application;

class MyApp extends Application {

  public DataBase DB = new DataBase();

   public DataBase getState(){
     return DB;
   }
   public void setState(){
       DB = new DataBase();
   }
 }

抱歉英文不好,如果您需要更多解释,请告诉我们!

1 个答案:

答案 0 :(得分:0)

您可以使用static修饰符将其存储在其他类中,也可以使用SharedPreferences或某些数据库。