0

嗨,我有一个列表视图,我想要它,这样当您按下某个项目时,它会将您的选择存储在共享首选项中。这样做的目的是,下次我打开应用程序时,它会跳过选择过程。

所以我想知道的是如何将此函数合并到我的代码中?

到目前为止,这是我的代码:

    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);
    }
}
4

1 回答 1

3

在您的 Activity 的 onCreate() 中:

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

在您的 onListItemClick() 中:

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

在您的项目中无处不在:

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

(-1 是默认值,表示当给定键没有值时,返回该值)

于 2012-05-04T16:11:13.080 回答