한 활동에서 다른 활동으로 묶음을 전달했습니다. 여기에는 String n (길이는 최대 30), String ID 및 String color가 포함됩니다. 이 값을 ArrayList (n, ID, color)로 저장 한 다음 ArrayList를 androids 메모리에 저장해야합니다. 나는 그것을하는 최선의 방법을 찾고 있었다. 나는 데이터베이스를 시도했지만 그 순간 나를 위해 복잡하고 나는 복잡한 일이 필요하다고 생각하지 않는다. 나는 FileOutputStream (여기에 설명 된대로 : http://developer.android.com/guide/topics/data/data-storage.html#pref?) 시도했지만 그것은 나를 위해, 아마도 뭔가 잘못하고있어 작동하지 않습니다. 배열의 arraylist를 실제로 만들 필요가 있을까요, 아니면 묶음의 arraylist를 사용할 수 있습니까, 아니면 다른 방법일까요? 가장 좋은 방법은 뭐야 ...? .. 모두 ...이 모든 시간 만 시도되었다arraylist를 안드로이드 메모리에 저장하는 가장 좋은 방법
감사를 도와주세요 운 .. 나는 누군가가 나에게 그에 손 줄 수 바라고 코드를 게시하지 해요 :
public class MainActivity extends Activity
{
String gotNotes;
String n;
String gotDOW;
String gotID;
public String clrs;
public String id;
public String nts;
String gotHour;
String gotColor;
TextView notes;
public static String FILENAME = "allevents";
String[] newevent;
String[] events;
SharedPreferences sharedPref;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button settings = (Button)findViewById(R.id.settings);
Bundle gotPackage = getIntent().getExtras();
if (gotPackage != null){
gotNotes = gotPackage.getString("AddedNote");
if (gotNotes.equals(" "))
{
n = "Empty";
}
else
{
n = gotNotes;
}
//gotDOW = gotPackage.getString("Day");
//gotHour = gotPackage.getInt("Hour");
gotID = gotPackage.getString("ID");
gotColor = gotPackage.getString("color");
initialize();
}
else{}
settings.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(),Settings.class);
startActivityForResult(i,0);
}
});
}
private void initialize()
{
// TODO Auto-generated method stub
String[] newevent = {n, gotID, gotColor};
ArrayList<String[]> events = new ArrayList<String[]>();
events.add(newevent);
SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("yourKey", events.toString());
editor.commit();
String allData = sharedPref.getString("yourKey", null);
String[] playlists = allData.split(",");
/* for (int number=0;number<events.lastIndexOf(sharedPref);number++)
{
notes = (TextView)findViewById(getResources().getIdentifier(playlists[number], getString(0), allData));
notes.setText(number+1);
}*/
notes = (TextView)findViewById(getResources().getIdentifier(gotID, "id",getPackageName()));
notes.setText(n);
notes.setGravity(Gravity.CENTER_HORIZONTAL);
notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
if (gotColor.equals("Blue")){
notes.setBackgroundColor(Color.rgb(99, 184, 255));}else
if(gotColor.equals("Green")){
notes.setBackgroundColor(Color.rgb(189, 252, 201));}else
if(gotColor.equals("Yellow")){
notes.setBackgroundColor(Color.rgb(238, 233, 191));}else
if(gotColor.equals("Grey")){
notes.setBackgroundColor(Color.LTGRAY);}else
if(gotColor.equals("Aqua")){
notes.setBackgroundColor(Color.rgb(151, 255, 255));}else
if(gotColor.equals("White")){}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
문자열의 최대 길이가 약 30이 될 것이므로, Serdar가 이미 설명한 것처럼 'SharedPreferences'에 저장해야합니다. 유스 케이스에 대해 데이터베이스가 복잡하고 불필요합니다. – Enigma