2012-10-15 3 views
3

도움이 필요합니다. 나는 새로운 안드로이드 개발자입니다. 공유 환경 설정 편집기에서 이미지를 저장할 수 없습니다. 내 목록을 이미지로 저장하고 싶습니다. 내 PersonalInfo.java공유 환경 설정 편집기를 사용하여 저장하는 방법

public class PersonalInfo { 
    private String name = ""; 
    private String mobile = ""; 
    private int image; 

    public void SetName(String name) { 
     this.name = name; 
    } 

    public String GetName() { 
     return this.name; 
    } 

    public void SetMobile(String mobile) { 
     this.mobile = mobile; 
    } 

    public String GetMobile() { 
     return this.mobile; 
    } 

    public void SetImage(int dataImage) { 
     this.image = dataImage; 
    } 

    public int GetImage() { 
     return this.image; 
    } 

가 어떻게이 문제를 해결할 수

public class Main extends ListActivity { 

    ArrayList<PersonalInfo> newList = null; 
    private Button btnSave = null; 

    private EditText txtName = null; 
    private EditText txtMobile = null; 
    private CustomListAdapter newAdpt = null; 
    private int i = 0; 
    private ImageView images; 
    public static String filename = "MySharedString"; 
    SharedPreferences someData; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     someData = getSharedPreferences(filename, 0); 
     newList = new ArrayList<PersonalInfo>(); 

     newAdpt = new CustomListAdapter(this, R.layout.list_item, newList); 
     setListAdapter(this.newAdpt); 

     txtName = (EditText) findViewById(R.id.txtName); 
     txtMobile = (EditText) findViewById(R.id.txtMobile); 
     images = (ImageView)findViewById(R.id.imageView1); 

     btnSave = (Button) findViewById(R.id.btnSave); 

     btnSave.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       newList = new ArrayList<PersonalInfo>(); 
       PersonalInfo info = new PersonalInfo(); 
       String dataTxtName = txtName.getText().toString(); 
       String dataTxtMobile = txtMobile.getText().toString(); 
       int dataImage = images.getId(); 
       SharedPreferences.Editor editor = someData.edit(); 

       editor.putString("sharedStringName", dataTxtName); 
       editor.putString("sharedStringMobile", dataTxtMobile); 
       editor.putInt("sharedIntImage", dataImage); 

       info.SetName(dataTxtName); 
       info.SetMobile(dataTxtMobile); 
       info.SetImage(dataImage); 
       newList.add(info); 


       if (newList != null && newList.size() > 0) { 
        newAdpt.notifyDataSetChanged(); 
        newAdpt.add(newList.get(0)); 
        i++; 

       } 

       newAdpt.notifyDataSetChanged(); 

      } 

은 다음과 같습니다

은 여기 내 Main.java입니까?

+0

이미지는 무엇이며 무엇을 시도 했습니까? – njzk2

+1

SharedPreferences 안에 개체를 저장하려고하면 작동하지 않습니다. SharedPreferences 내부에만 기본 유형을 저장할 수 있습니다. 예를 들어 SharedPreferences 개체에 이미지의 경로를 쓴 다음 이미지를 SD 카드에 일반 파일로 쓸 수 있습니다. PersonalInfo 클래스를 저장하려고하기 때문에 그것이 직렬화 가능하게 만들고 오브젝트 스트림에 파일로 작성해야합니다. – Darwind

+0

내 listrow에서 텍스트 만 보냅니다. 이미지는 전송되지 않습니다. – user1303250

답변

3

SharedPreferences을 수정 한 후에 editor.commit();을 추가하십시오.

+2

doc : http://developer.android.com/guide/topics/도 참조하십시오. data/data-storage.html # pref – shkschneider

2

SharedPreferences를 변경할 때마다 editor.commit()을 추가해야합니다.

+0

내 목록 행에 텍스트 만 보냅니다. 이미지가 전송되지 않습니다. – user1303250

+0

Darwind 님의 댓글을 읽어주세요. –