2012-05-04 87 views
0

嗨,我有一個列表視圖,我想這樣做,當你按下一個項目它將你的選擇存儲在共享偏好。這樣做的目的是讓我下次打開應用程序時跳過選擇過程。保存Android的首選項

所以我想知道的是如何將此功能併入我的代碼?

這是到目前爲止我的代碼:

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

在你活動的onCreate():

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

在你onListItemClick():

preferences.edit().putInt("KEY", position).commit(); 
在您的項目

無處不在:

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

(-1是默認值,表示當給定的鍵沒有值時,返回該值)