2012-05-04 6 views
0

안녕하세요, 저는 listview를 가지고 있고 항목을 누를 때 선택 사항을 공유 환경 설정에 저장하도록하고 싶습니다. 이 목적은 다음에 앱을 열 때 선택 과정을 건너 뜁니다.안드로이드 환경 설정 저장

그래서 내가 알고 싶은 것은 어떻게이 코드를이 코드에 통합시키는 것입니까? 액티비티의에서 onCreate에서

public class SelectTeamActivity extends ListActivity { 
     public String fulldata = null; 
     public String position = null; 
     public String divisionName= null; 
     public List<String> teamsList = null; 
     public String divName = null; 

    protected void loadData() { 
     fulldata = getIntent().getStringExtra("fullData"); 
     position = getIntent().getStringExtra("itemIndex"); 

     Log.v("lc", "selectteamActivity:" + fulldata); 
     Log.v("lc", "position:" + position); 

     int positionInt = Integer.parseInt(position); 

     try{ 
      JSONObject obj = new JSONObject(fulldata); 
      teamsList = new ArrayList<String>(); 
      JSONObject objData = obj.getJSONObject("data"); 

      JSONArray teamInfoArray = objData.getJSONArray("structure"); 

      for(int r = 0; r < teamInfoArray.length(); r++){ 
       JSONObject teamFeedStructureDict = teamInfoArray.getJSONObject(r); 
       JSONArray teamStructureArray = 
         (JSONArray) teamFeedStructureDict.get("divisions"); 

       JSONObject teamFeedDivisionsDictionary = 
         teamStructureArray.getJSONObject(positionInt); 
       divName = teamFeedDivisionsDictionary.getString("name"); 

       JSONArray teamNamesArray = 
          (JSONArray) teamFeedDivisionsDictionary.get("teams"); 

       for(int t = 0; t < teamNamesArray.length(); t++){ 
        JSONObject teamNamesDict = teamNamesArray.getJSONObject(t); 
        teamsList.add(teamNamesDict.getString("name")); 
       } 
      } 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.selectact); 
     loadData(); 

     TextView headerText = (TextView) findViewById(R.id.header_text); 
     headerText.setText(divName); 
     TextView redHeaderText = (TextView) findViewById(R.id.redheadertext); 
     redHeaderText.setText("Please select your team:"); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.single_item, 
                teamsList)); 

     ListView list = getListView(); 

     list.setTextFilterEnabled(true); 
    } 

    @Override 
    protected void onListItemClick (ListView l, View v, int position, long id) { 
     Intent intent = new Intent(this, HomeActivity.class); 
    String curPos = Integer.toString(position); 

     //or just use the position: 
     intent.putExtra("itemIndex", curPos); 
     intent.putExtra("fullData", fulldata); //or just the part you want 
     startActivity(intent); 
    } 
} 

답변

3

() : 당신의 onListItemClick()에서

SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE); 

: 프로젝트의 모든 곳

preferences.edit().putInt("KEY", position).commit(); 

여기

내 지금까지 코드입니다 :

int position = preferences.getInt("KEY", -1); 

(-1은 주어진 키에 값이없는 경우이 값을 반환하는 기본값입니다.)

관련 문제