如何在ListView

时间:2016-01-18 22:59:16

标签: android listview checkbox sharedpreferences

我已经用了两天的时间尝试了我在这里找到的不同解决方案但是对我来说有用。我想要做的是“非常简单”。

我有一个带有复选框的自定义列表视图。如果启用或禁用复选框,我想使用共享偏好保存。然后,如果用户选择一个复选框,则必须在返回到屏幕时启用。

这是我的代码

这是我的班级:

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, "");
    }


}

0 个答案:

没有答案