Android 如何在ListView的复选框中设置SharedReferences

Android 如何在ListView的复选框中设置SharedReferences,android,listview,checkbox,sharedpreferences,Android,Listview,Checkbox,Sharedpreferences,我已经用了两天的时间尝试了我在这里找到的不同的解决方案,但任何一种都适合我。我想做的是“非常简单” 我有一个带有复选框的自定义列表视图。如果复选框处于启用或禁用状态,我希望与SharedReferences一起保存。然后,如果用户选择一个复选框,则返回屏幕时必须启用该复选框 这是我的密码 这是我的班级: public class NewspaperList extends AppCompatActivity { private MyCustomAdapter dataAdapter =

我已经用了两天的时间尝试了我在这里找到的不同的解决方案,但任何一种都适合我。我想做的是“非常简单”

我有一个带有复选框的自定义列表视图。如果复选框处于启用或禁用状态,我希望与SharedReferences一起保存。然后,如果用户选择一个复选框,则返回屏幕时必须启用该复选框

这是我的密码

这是我的班级:

public class NewspaperList extends AppCompatActivity {

    private MyCustomAdapter dataAdapter = null;
    private WebView browser;
    private static SharedPreferences preferences;


    public static final String MARCA = "periodico.marca";
    public static final String DIARIO_AS = "periodico.as";

    private String marca;
    private String as;


    CheckBox cb;
    TextView newspaperName;
    CheckBox checkbox;
    ArrayList<Country> countryList;

    private ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newspaper_listview);

        //Generate list View from ArrayList
        displayListView();


    }

    private void displayListView() {

        //Array list of countries
        countryList = new ArrayList<Country>();
        Country country = new Country("Marca","http://www.marca.com/futbol", false);
        countryList.add(country);
        country = new Country("As","http://as.com/futbol/", false);
        countryList.add(country);




        AdView mAdView = (AdView) findViewById(R.id.adViewList);
        AdRequest adRequest = new AdRequest.Builder().build();

        mAdView.loadAd(adRequest);

        //create an ArrayAdaptar from the String Array
        dataAdapter = new MyCustomAdapter(this, R.layout.newspaper_list_row, countryList);
        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);



        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // When clicked, show a toast with the TextView text
                Country country = (Country) parent.getItemAtPosition(position);

                if (country.getName().equals("Marca")) {
                    Intent myIntent = new Intent(NewspaperList.this, Webview.class);
                    myIntent.putExtra("newspaper_url", country.getUrl());
                    startActivity(myIntent);
                } else {
                }
            }
        });

    }

    private class MyCustomAdapter extends ArrayAdapter<Country> {

        private ArrayList<Country> countryList;

        public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.newspaper_list_row, null);

                 newspaperName = (TextView) convertView.findViewById(R.id.newspaperName);
                 checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);

                Country country = countryList.get(position);
                checkbox.setTag(country.getName());


                preferences = PreferenceManager.getDefaultSharedPreferences(NewspaperList.this);
                marca = preferences.getString(MARCA, "");

                mCheckBoxes.add(checkbox);


                    if (checkbox.getTag().equals(marca)) {
                        checkbox.setChecked(true);
                    }else if (checkbox.getTag().equals(marca)){
                        checkbox.setChecked(true);
                    }


                checkbox.setOnClickListener( new View.OnClickListener() {
                    public void onClick(View v) {

                        CheckBox cb = (CheckBox) v;
                        Country country = (Country) cb.getTag();

                        if (((CheckBox)v).isChecked()) {
                            for (int i = 0; i < mCheckBoxes.size(); i++) {

                                if (mCheckBoxes.get(i) == v){
                                    addPreferencesAndDataBase(country.getName());
                                }else{

                                }
                            }
                        }
                        else {
                        }


                    }

                });
            }
            else {
            }

            Country country = countryList.get(position);
            newspaperName.setText(country.getName());
            checkbox.setChecked(country.isSelected());
            checkbox.setTag(country);

            return convertView;

        }

    }


    public void addPreferencesAndDataBase(String name){

        SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = data.edit();

        if(name.equals("Marca")){
            Toast.makeText(getApplicationContext(), name , Toast.LENGTH_SHORT).show();
            editor.putString(MARCA, name);
            editor.commit();
        }else if(name.equals("As")){
            Toast.makeText(getApplicationContext(), name  , Toast.LENGTH_SHORT).show();
            editor.putString(as, name);
            editor.commit();
        }
    }


    public void getAllPreferences(){
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        marca = preferences.getString(MARCA, "");
        as = preferences.getString(DIARIO_AS, "");
    }


}
公共类新闻纸列表扩展了AppCompative活动{
私有MyCustomAdapter dataAdapter=null;
私有网络视图浏览器;
私有静态SharedReferences首选项;
公共静态最终字符串MARCA=“periodico.MARCA”;
公共静态最终字符串DIARIO_AS=“periodico.AS”;
私人字符串marca;
私有字符串为;
复选框cb;
TextView newspaperName;
复选框;
ArrayList国家列表;
private ArrayList mcheckbox=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.Paper_listview);
//从ArrayList生成列表视图
displayListView();
}
私有void displayListView(){
//国家列表
countryList=新的ArrayList();
国家/地区=新国家(“Marca”http://www.marca.com/futbol“,假);
国家列表。添加(国家);
国家/地区=新国家/地区(“As”http://as.com/futbol/“,假);
国家列表。添加(国家);
AdView mAdView=(AdView)findViewById(R.id.adViewList);
AdRequest AdRequest=新建AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//从字符串数组创建ArrayAdaptar
dataAdapter=新的MyCustomAdapter(此,R.layout.paper\u list\u行,countryList);
ListView ListView=(ListView)findViewById(R.id.listView1);
//将适配器分配给ListView
setAdapter(dataAdapter);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//单击后,显示带有文本视图文本的祝酒词
Country Country=(国家)父项.getItemAtPosition(位置);
if(country.getName().equals(“Marca”)){
Intent myIntent=newintent(NewspaperList.this,Webview.class);
myIntent.putExtra(“paper_url”,country.getUrl());
星触觉(myIntent);
}否则{
}
}
});
}
私有类MyCustomAdapter扩展了ArrayAdapter{
私有ArrayList国家列表;
公共MyCustomAdapter(上下文上下文,int textViewResourceId,ArrayList countryList){
super(上下文、textViewResourceId、countryList);
this.countryList=新的ArrayList();
this.countryList.addAll(countryList);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
Log.v(“ConvertView”,String.valueOf(position));
if(convertView==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
convertView=vi.充气(R.版面.报纸列表行,空);
newspaperName=(TextView)convertView.findViewById(R.id.newspaperName);
复选框=(复选框)convertView.findViewById(R.id.checkBox1);
Country=countryList.get(位置);
setTag(country.getName());
preferences=PreferenceManager.GetDefaultSharedReferences(NewspaperList.this);
marca=preferences.getString(marca,“”);
mcheckbox.add(复选框);
if(checkbox.getTag().equals(marca)){
checkbox.setChecked(true);
}else if(checkbox.getTag().equals(marca)){
checkbox.setChecked(true);
}
checkbox.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
复选框cb=(复选框)v;
Country Country=(Country)cb.getTag();
如果(((复选框)v).isChecked()){
对于(int i=0;i
如何