我有一个TableView
60 TextView
,用户可以通过另一个Dialog Activity触摸并输入其中的数据。
DialogActivity
设置了所触摸的TextView
的文字和颜色。
问题是当我触摸另一个TextView
时,我失去了之前触及的TextView。
我是否可以逐步使用SharedPreference
存储所有被触及的TextView
?
例如,如果我将TextView
设置在位置1:1(行:columm)并且我想设置TextView
2:2,我如何逐步保存所有TextView
以及don #39;丢失了他们输入的数据?
调用DialogActivity的Activity的一些java
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);
}
}
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();
}
});
}
}
这是一个很好的共享偏好还是更好的另一种方式?
答案 0 :(得分:1)
SharedPreferences适用于少量数据,否则您需要查看sqllite以获取大量数据。
对于您的问题,请看一下这篇文章:How to use SharedPreferences in Android to store, fetch and edit values