2017-04-11 8 views
0

現在、私は現在、array.xmlに含まれる項目のリストのチェックボックスを含む新しいプロジェクトを作成しています。私は共有の環境設定を使用しており、別のアクティビティでチェックされたアイテムをプルアップできるようにしたいと考えています。私は選択を保存し、新しい活動を開くボタンを持っています。今、私はそれが第二の活動に現われるのを苦労しているだけです。私は第二の活動を始める方法がわからないので、主な活動のために私のコードを示します。チェック可能リストから共有プリファレンスを取得する

@Override 
public void onClick(View v) { 
    String selected = ""; 
    int cntChoice = myList.getCount(); 

    SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions(); 
    for(int i = 0; i < cntChoice; i++){ 
     if(sparseBooleanArray.get(i)) { 
      selected += myList.getItemAtPosition(i).toString() + "\n"; 
      System.out.println("Checking list while adding:" + myList.getItemAtPosition(i).toString()); 
      SaveSelections(); 
      Intent learnintent = new 
      Intent(MainActivity.this,UserList.class); 
      learnintent.putExtra("",selected); 
      Toast.makeText(MainActivity.this, selected, 
      Toast.LENGTH_LONG).show(); 
      startActivity(learnintent); 
     } 
    } 

    Toast.makeText(MainActivity.this, selected, 
    Toast.LENGTH_LONG).show(); 
    }}); 

    clearAll.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
     ClearSelections(); 
    } 
}); 
} 

private void SaveSelections() { 
    // save the selections in the shared preference in private mode for the user 

    SharedPreferences.Editor prefEditor = sharedpreferences.edit(); 
    String savedItems = getSavedItems(); 
    prefEditor.putString(MyPREFERENCES.toString(), savedItems); 
    prefEditor.commit(); 
} 

private String getSavedItems() { 
    String savedItems = ""; 
    int count = this.myList.getAdapter().getCount(); 
    for (int i = 0; i < count; i++) { 
     if (this.myList.isItemChecked(i)) { 
      if (savedItems.length() > 0) { 
       savedItems += "," + this.myList.getItemAtPosition(i); 
      } else { 
       savedItems += this.myList.getItemAtPosition(i); 
      } 
     } 
    } 
    return savedItems; 
} 

private void LoadSelections() { 
// if the selections were previously saved load them 

    if (sharedpreferences.contains(MyPREFERENCES.toString())) { 
     String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), ""); 
     selectedItems.addAll(Arrays.asList(savedItems.split(","))); 
     int count = this.myList.getAdapter().getCount(); 

     for (int i = 0; i < count; i++) { 
      String currentItem = (String) myList.getAdapter().getItem(i); 
      if (selectedItems.contains(currentItem)) { 
       myList.setItemChecked(i, true); 
       Toast.makeText(getApplicationContext(), "Curren Item: " + currentItem,Toast.LENGTH_LONG).show(); 
      } else { 
       myList.setItemChecked(i, false); 
      } 
     } 
    } 
} 

private void ClearSelections() { 
    // user has clicked clear button so uncheck all the items 
    int count = this.myList.getAdapter().getCount(); 
    for (int i = 0; i < count; i++) { 
     this.myList.setItemChecked(i, false); 
    } 
    // also clear the saved selections 
    SaveSelections(); 
} 
+0

あなたはシングルトンクラスを実装するのですかアンドロイドへ –

+0

申し訳ありませんが、むしろ新しい共有好みのシングルトンクラスを作りますか? – KeithB

答えて

0

@KeithB

私はシングルトン共有Preferencesクラスを書かれました。それはより多くの情報のため

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 

public class SharedPref 
{ 
    private static SharedPreferences mSharedPref; 
    public static final String NAME = "NAME"; 
    public static final String AGE = "AGE"; 
    public static final String IS_SELECT = "IS_SELECT"; 

    public static void init(Context context) 
    { 
     if(mSharedPref == null) 
      mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE); 
    } 

    public static String read(String key, String defValue) { 
     return mSharedPref.getString(key, defValue); 
    } 

    public static void write(String key, String value) { 
     SharedPreferences.Editor prefsEditor = mSharedPref.edit(); 
     prefsEditor.putString(key, value); 
     prefsEditor.commit(); 
    } 

    public static boolean read(String key, boolean defValue) { 
     return mSharedPref.getBoolean(key, defValue); 
    } 

    public static void write(String key, boolean value) { 
     SharedPreferences.Editor prefsEditor = mSharedPref.edit(); 
     prefsEditor.putBoolean(key, value); 
     prefsEditor.commit(); 
    } 

    public static Integer read(String key, int defValue) { 
     return mSharedPref.getInt(key, defValue); 
    } 

    public static void write(String key, Integer value) { 
     SharedPreferences.Editor prefsEditor = mSharedPref.edit(); 
     prefsEditor.putInt(key, value).commit(); 
    } 
} 

あなたのために有用である可能性があるリンクSharedPref Sample

関連する問題