このマルチセレクトリストビューは初めてです。チェックボックスのチェック状態をlistviewに保存して、ユーザーがアプリケーションを閉じてからもう一度開くと、選択したチェックボックスが選択されたままになるようにします。これを行う方法はありますか?私はそれを探して、それがSharedPreferenceを使用して行うことができることがわかったが、私はそれを使用する方法についての詳細情報を取得していない。おかげで、リスト項目の複数選択リストビューのチェックボックスのチェック状態を保存します
public class MainActivity extends AppCompatActivity {
ListView myList;
Button getChoice;
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listContent);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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";
}
}
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
}
});
}
}
私のアプリケーションであなたのコードを実行すると、アプリケーションは 'SharedPreferences sharedPreferences = getSharedPreference'行のNPEでクラッシュする@Michael – CodeAssasins
とにかくそれを解決しました。ところで、あなたのコードは最高の人間です。 @マイケル。 – CodeAssasins
また、このsharedpreferenceを削除する方法はありますか? – CodeAssasins